Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
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 17th, 2009, 04:22 AM
Authorized User
Points: 60, Level: 1
Points: 60, Level: 1 Points: 60, Level: 1 Points: 60, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copying all the nodes except the specific node

Hi ,

input xml :

<?xmlversion="1.0"encoding="UTF-8"?>
<departments>
<employee1>
<salary>
<amount>30</amount>
</salary>
</employee1>
<employee2>
<amount>
<amount>40</amount>
</amount>
</employee2>
</departments>

xsl :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:output method="xml"></xsl:output>
<xsl:template match="*">
<
xsl:copy>
<
xsl:copy-of select="@*"></xsl:copy-of>
<
xsl:apply-templates></xsl:apply-templates>
</
xsl:copy>
</
xsl:template>
<xsl:template match="*[local-name()='amount']|">
<
xsl:element name="{name()}">
<
xsl:call-template name="AnotherTemplate">
<
xsl:with-param name="inputData" select="."></xsl:with-param>
</
xsl:call-template>
</
xsl:element>
</xsl:template>

<xsl:template name="AnotherTemplate">
<xsl:param name="inputData"></xsl:param>
<!-- do some processing-->

</xsl:stylesheet>

Problem : I have written a xsl which copies all the elements from the source tree. I dont want to copy node "amount" from the source tree. So i have defined another template which matches on the node amount.

Now the problem is my input xml has got amount as a list. so I want to write a template which matches on the node "amount whose child is not amount.

Any suggestions to write template match satisfying above condition.

Thanks,
Arun
Reply With Quote
  #2 (permalink)  
Old July 17th, 2009, 04:58 AM
Friend of Wrox
Points: 1,306, Level: 14
Points: 1,306, Level: 14 Points: 1,306, Level: 14 Points: 1,306, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: Pondicherry, India
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:
Code:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="amount">
<amount>
<xsl:call-template name="AnotherTemplate">
<xsl:with-param name="inputData" select="."></xsl:with-param>
</xsl:call-template>
</amount>
</xsl:template>

<xsl:template name="AnotherTemplate">
<xsl:param name="inputData"></xsl:param>
<!-- do some processing-->
</xsl:template>

<xsl:template match="amount[child::amount]">
<!-- do some processing-->
</xsl:template>

__________________
Rummy
Reply With Quote
  #3 (permalink)  
Old July 17th, 2009, 06:22 AM
Authorized User
Points: 60, Level: 1
Points: 60, Level: 1 Points: 60, Level: 1 Points: 60, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mrame -

I modified the xsl as u suggested and its working. Here's the modified xsl

<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="amount">
<amount>
<xsl:call-template name="AnotherTemplate">
<xsl:with-param name="inputData" select="."></xsl:with-param>
</xsl:call-template>
</amount>
</xsl:template>
<xsl:template name="AnotherTemplate">
<xsl:param name="inputData"></xsl:param>
<out>
<xsl:value-of select="$inputData"></xsl:value-of>
</out>
</xsl:template>
<xsl:template match="amount[child::amount]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

I have got few questions -

- what is the difference between two xpath statement if used in template match
<xsl:template match="amount/child::amount"> and
<xsl:template match="amount[child::amount]">

Do we have performance impact if we use the above logic to parse huge payload ?


Thanks,
Arun
Reply With Quote
  #4 (permalink)  
Old July 17th, 2009, 06:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
Originally Posted by arunprasadlv View Post
mrame -

I have got few questions -

- what is the difference between two xpath statement if used in template match
<xsl:template match="amount/child::amount"> and
<xsl:template match="amount[child::amount]">

Thanks,
Arun
amount/child::amount = amount/amount which gives elements named 'amount' that have a parent also called 'amount.

amount[child::amount] = amount[amount] which gives elements named 'amount' that have a child element also named 'amount'.
So both paths deal with elements like this
Code:
<amount><amount /></amount>
but the former matches the inner element and the latter the outer one.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #5 (permalink)  
Old July 17th, 2009, 01:32 PM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

Joe has answered you first question. As for the second:

Do we have performance impact if we use the above logic to parse huge payload ?

it's not clear what you're comparing with. Performance depends on what XSLT processor you are using, and the only reliable way to compare whether one construct is faster than another is to measure it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #6 (permalink)  
Old July 17th, 2009, 01:53 PM
Authorized User
Points: 60, Level: 1
Points: 60, Level: 1 Points: 60, Level: 1 Points: 60, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael -

I have requirement where i have to perform a specific transformation on the amount nodes,which can occur at different position in the source xml.

Instead of mapping all the elements induvidually , i am trying to write a filtering logic to copy all the element , but exculde the amount .For this node , the matching template will call another template which will do the required transformation to the amount field.

I will see if i can measure the performance with both the approaches.

One more question ,

<xsl:template match="amount[child::amount]>

the amount element has got a namespace prefix in the incoming xml.
How to use the local-name fn with child axis ?



Thanks,
Arun
Reply With Quote
  #7 (permalink)  
Old July 17th, 2009, 02:00 PM
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

Your best bet is to simply define the namespace in your XSLT and the use it:

<xsl:stylesheet ... xmlns:my="http://whaterever..."

<xsl:template match="my:amount[my:amount]">
..
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #8 (permalink)  
Old July 17th, 2009, 02:00 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
Originally Posted by arunprasadlv View Post
One more question ,

<xsl:template match="amount[child::amount]>

the amount element has got a namespace prefix in the incoming xml.
How to use the local-name fn with child axis ?
Thanks,
Arun
Why not just assign a nmamespace mapping?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #9 (permalink)  
Old July 17th, 2009, 02:01 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

What is a local-name fn?
If you have elements in a namespace then have your XSLT stylesheet define a prefix (e.g. 'pf') for that namespace and do
Code:
<xsl:template match="pf:amount[child::pf:amount]">
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #10 (permalink)  
Old July 22nd, 2009, 06:30 AM
Authorized User
Points: 60, Level: 1
Points: 60, Level: 1 Points: 60, Level: 1 Points: 60, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Martin -

I want to make this template generic and call this template from my xsl , by passing the node (which i dont want to copy) as an argument to the generic template

So my generic template should check if the context node is the node which is passed as an argument, if not copy execute identity template else call another template.

- Is it possible to send the node as an argument ? Is there any way to convert the string into nodeset ?

Thanks
Arun
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
Processing specific Nodes Krippers XSLT 3 November 6th, 2008 05:38 AM
Problem Copying Nodes tclotworthy XSLT 14 February 13th, 2007 01:13 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Problem copying XML nodes francislang XSLT 9 October 21st, 2005 10:37 AM
Copying XML nodes from one document to another hughcr C# 2 May 12th, 2005 01:20 AM



All times are GMT -4. The time now is 07:31 PM.


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