Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 14th, 2008, 07:53 AM
Registered User
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How can i use xsl:copy-of in xsl

I have a XML like
<Attribute>
    <NAME>ROHAN</NAME>
    <VALUE>111</VALUE>
    <AGE>23</AGE>
</Attribute>

and i want to copy the entire Attribute node except one field i.e., AGE. How can i do it in the XSL. How to use xsl:copy-of for this or are there any more ways to do? Please let me know if knows.

 Thanks in Advance.

Sampath Kumar.B
 
Old July 14th, 2008, 07:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The standard coding pattern for this is to write an identity template which copies everything recursively:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

and then override it where you want different behaviour:

<xsl:template match="AGE"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 21st, 2008, 07:04 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

I've seen in a book how to ignore an element as below

<xsl:apply-templates select="*[not(self::element-to-ignore)]"/>

But I can't make it to use in the sample given by sampath above.

Also, if my XML is below

<values>
<field name="Sam">TP field1 value</field>
<field name="Jean">TP field2 value</field>
<field name="John">TP field3 value</field>
<field name="Tommy">TP field4 value</field>
</values>

and I don't want the //field[@name='Tommy'] to be included in the final document?

How can I do that in a simple xslt?

Thanks!

 
Old July 21st, 2008, 07:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

<xsl:apply-templates select="*[not(@name='Tommy')]"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old July 21st, 2008, 07:21 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh Sam,

how can I miss that?

grrr.

Thanks!

 
Old July 23rd, 2008, 08:15 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What if the xml looks like this

<sql>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age1</column>
<column name="field3">Value 1</column>
</row>
<row>
<column name="field1">Name2</column>
<column name="field2">Age2</column>
<column name="field3">Value 2</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age3</column>
<column name="field3">Value 1</column>
</row>
<row>
<column name="field1">Name4</column>
<column name="field2">Age4</column>
<column name="field3">Value 4</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age4</column>
<column name="field3">Value 1</column>
</row>
</sql>

And I want to ignore the ENTIRE row where it satisfies this condition //column[@name='field3']/text()='Value 4'?

I thought it's just

*[not(/row/column[@name='field3']/text()='Value 4')]

Still confused...

 
Old July 23rd, 2008, 08:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Any path expression starting with "/" selects from the root of the document, so you don't want it if you are trying to look at an individual row.

Try

<xsl:copy-of select="row[not(column[@name='field3']='Value 4')]"/>

assuming your context node is the <sql> element.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old July 23rd, 2008, 08:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The above xpath expression will match the following elements:

select all children where there not a row in the document whose column name is 'field3' and column value is 'Value 4'.

What is doesn't tell us is what you want to select children of. But I suspect you're problem is the /row - which selected all row elements in the document, not the current element.

/row[not(column[@name='field3' and text()='Value 4'])]


/- Sam Judson : Wrox Technical Editor -/
 
Old July 24th, 2008, 01:21 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael and Sam,

As always you guys are brilliant!

From this
*[not(/row/column[@name='field3']/text()='Value 4')]

To this

row[not(column[@name='field3']='Value 4')]

 or

row[not(column[@name='field3' and text()='Value 4'])]

Whoa! I am not even close. :(

The position of "not" and "row" and the absence or awkward position of "text()" in your solution worries me a lot about my career in XML.

Your solution will surely go down to my notes for sure as reference.

Thanks!






 
Old September 30th, 2008, 11:16 PM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am now stuck in removing a block of text that has prefix as

.
.
.
  <ats:MyDataItem>
    <ats:field>data1</ats:field>
    <ats:value>data1</ats:value>
    <ats:field>data2</ats:field>
    <ats:value>data2</ats:value>
  </ats:MyDataItem>
.
.
.

I want to remove <ats:MyDataItem> by using a blank template but I have no luck.

Thanks for the usual help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:copy-of Issue DKatSSA BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 8 November 19th, 2008 05:18 PM
Copy one node to another in XSL hugoscp XML 5 September 14th, 2006 09:40 AM
Slightly modified xsl:copy-of arcuza XSLT 6 April 10th, 2006 04:53 AM
xsl:copy copies everywhere [email protected] XSLT 1 November 11th, 2005 07:44 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.