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 26th, 2011, 05:15 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Question 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..
 
Old July 26th, 2011, 05:55 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 27th, 2011, 10:45 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

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.
 
Old July 27th, 2011, 10:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try translate(%temp, ' ', '') instead - it should work.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
JohnKiller (July 27th, 2011)
 
Old July 27th, 2011, 11:12 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

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

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?"
 
Old July 27th, 2011, 11:40 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
Originally Posted by JohnKiller View Post
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:
JohnKiller (July 27th, 2011)
 
Old July 27th, 2011, 12:05 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi, Martin,

Thank you for your replying.

Is it possible not to use the template to achieve the goal?

Thanks.
 
Old July 27th, 2011, 12:07 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
Originally Posted by JohnKiller View Post

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
 
Old July 27th, 2011, 12:16 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Display distinct values elayaraja.s XSLT 1 July 17th, 2008 07:00 AM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
retrieving values with xsl Tomi XSLT 14 April 5th, 2007 02:07 AM
values comparison in xsl sani723 XSLT 2 April 3rd, 2007 04:05 AM
Reassigning values to variables in XSL. rmiller XSLT 1 September 25th, 2004 02:43 AM





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