 |
| 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
|
|
|
|

July 14th, 2008, 07:53 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 14th, 2008, 07:57 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 21st, 2008, 07:04 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

July 21st, 2008, 07:13 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
<xsl:apply-templates select="*[not(@name='Tommy')]"/>
/- Sam Judson : Wrox Technical Editor -/
|
|

July 21st, 2008, 07:21 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh Sam,
how can I miss that?
grrr.
Thanks!
|
|

July 23rd, 2008, 08:15 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

July 23rd, 2008, 08:25 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 23rd, 2008, 08:26 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

July 24th, 2008, 01:21 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

September 30th, 2008, 11:16 PM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
 |