 |
| 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 26th, 2011, 05:15 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
how to append the values using xsl
I want to read the value from one xml and use xls to output these value as one in another xml.
input xml:
<rootNode>
-<Node>
--<anotherNode>
---<Text>Hi</Text>
---<Text>Hello</Text>
---<Text>Hey</Text>
---...
--</anotherNode>
-</Node>
</rootNode>
output xml:
<Rootnode>
-<node1 tag="Text" detail="HiHelloHey" />
</Rootnode>
Requirements:
1. the Text nodes may or may not in the input xml, so we need to determine it, if there is no Text node, do not show node1 in the output xml.
2. the number of Text nodes is uncertain, so it may 3 or 30 Text nodes in the input xml. We need append all of them into the node1 in the output xml.
How can we achieve this goal using xsl? Thanks.
Last edited by JohnKiller; July 26th, 2011 at 05:20 PM..
|
|

July 26th, 2011, 05:55 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well something like:
Code:
<xsl:if test="/rootNode/Node/anotherNode/Text">
<node1 tag="Text">
<xsl:attribute name="detail">
<xsl:variable name="temp"><xsl:value-of select="/rootNode/Node/anotherNode/Text"/></xsl:variable>
<xsl:value-of select="replace($temp, ' ', '')"/>
</xsl:attribute>
</node1>
</xsl:if>
|
|

July 27th, 2011, 10:45 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Thank you for your reply.
But I can not use replace function since I am using version 1.0. So what kind of alternative method?
Thank you again.
|
|

July 27th, 2011, 10:46 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Try translate(%temp, ' ', '') instead - it should work.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

July 27th, 2011, 11:12 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
I changed to translate($temp, ' ', ''), but it only returns the first Text value.
|
|

July 27th, 2011, 11:16 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Oh, sorry, I forget to tell that the In the Text nodes, its values are sentences, like "Hi, Anna, this", " is John. How ", "are you?"
|
|

July 27th, 2011, 11:40 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Quote:
Originally Posted by JohnKiller
I want to read the value from one xml and use xls to output these value as one in another xml.
input xml:
<rootNode>
-<Node>
--<anotherNode>
---<Text>Hi</Text>
---<Text>Hello</Text>
---<Text>Hey</Text>
---...
--</anotherNode>
-</Node>
</rootNode>
output xml:
<Rootnode>
-<node1 tag="Text" detail="HiHelloHey" />
</Rootnode>
Requirements:
1. the Text nodes may or may not in the input xml, so we need to determine it, if there is no Text node, do not show node1 in the output xml.
2. the number of Text nodes is uncertain, so it may 3 or 30 Text nodes in the input xml. We need append all of them into the node1 in the output xml.
How can we achieve this goal using xsl? Thanks.
|
Simply do
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="rootNode">
<RootNode>
<xsl:apply-templates select="Node[anotherNode/Text]"/>
</RootNode>
</xsl:template>
<xsl:template match="Node">
<node1 tag="Text">
<xsl:attribute name="detail">
<xsl:apply-templates select="anotherNode/Text"/>
</xsl:attribute>
</node1>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

July 27th, 2011, 12:05 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Hi, Martin,
Thank you for your replying.
Is it possible not to use the template to achieve the goal?
Thanks.
|
|

July 27th, 2011, 12:07 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Quote:
Originally Posted by JohnKiller
Is it possible not to use the template to achieve the goal?
|
Which template exactly are you referring to?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

July 27th, 2011, 12:16 PM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Currently, it is using
<xsl:template match="response">
I am part of the project, so I do not know what is that mean. I write my code under that template.
|
|
 |