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 17th, 2009, 04:22 AM
Authorized User
 
Join Date: May 2008
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
 
Old July 17th, 2009, 04:58 AM
Friend of Wrox
 
Join Date: Jun 2008
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
 
Old July 17th, 2009, 06:22 AM
Authorized User
 
Join Date: May 2008
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
 
Old July 17th, 2009, 06:39 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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/
 
Old July 17th, 2009, 01:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old July 17th, 2009, 01:53 PM
Authorized User
 
Join Date: May 2008
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
 
Old July 17th, 2009, 02:00 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
Old July 17th, 2009, 02:00 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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/
 
Old July 17th, 2009, 02:01 PM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old July 22nd, 2009, 06:30 AM
Authorized User
 
Join Date: May 2008
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





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





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