Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 14th, 2008, 07:53 AM
Registered User
 
Join Date: Jul 2008
Location: Hyderabad, A.P., India.
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
Reply With Quote
  #2 (permalink)  
Old July 14th, 2008, 07:57 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #3 (permalink)  
Old July 21st, 2008, 07:04 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
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!

Reply With Quote
  #4 (permalink)  
Old July 21st, 2008, 07:13 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old July 21st, 2008, 07:21 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh Sam,

how can I miss that?

grrr.

Thanks!

Reply With Quote
  #6 (permalink)  
Old July 23rd, 2008, 08:15 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
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...

Reply With Quote
  #7 (permalink)  
Old July 23rd, 2008, 08:25 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #8 (permalink)  
Old July 23rd, 2008, 08:26 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #9 (permalink)  
Old July 24th, 2008, 01:21 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
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!






Reply With Quote
  #10 (permalink)  
Old September 30th, 2008, 11:16 PM
Authorized User
 
Join Date: Jun 2008
Location: , , .
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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 ravik@olemiss.edu 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



All times are GMT -4. The time now is 08:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.