XSL Transform

R

Ravi

I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.
However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5

which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line? I tried using concat and substring-before and though it
works its not a good solution as I do not know how many children abc has
in advance (besides being very laborious). Any suggestions will be
highly appreciated.

TIA.
 
D

Dino Morelli

I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.
However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5

which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line? I tried using concat and substring-before and though it
works its not a good solution as I do not know how many children abc has
in advance (besides being very laborious). Any suggestions will be
highly appreciated.

To get all of the text values of all of the children of <abc> on
separate lines you can loop through all children:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="text" />

<xsl:template match="/">
<xsl:for-each select="*">
<xsl:value-of select="." />
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


To get just the single text value of <abc> without the <def> children at
all you can use the text() function:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="text" />

<xsl:template match="/">
<xsl:value-of select="/abc/text()" />
</xsl:template>

</xsl:stylesheet>
 
D

Dimitre Novatchev

Ravi said:
I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.

No, the string value of an element is the concatenation of all of its
descendent text nodes:

From the XPath spec:

"The string-value of an element node is the concatenation of the
string-values of all text node descendants of the element node in document
order."

http://www.w3.org/TR/xpath#element-nodes



However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5

This is the correct string value of the element (or root node) according to
the XPath spec.
which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line?

By "value of the parent node" I guess you mean the value of its first text
node child?

Yes, this transformation produces the results you want:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<xsl:eek:utput method="text"/>
<xsl:strip-space elements="*" />

<xsl:template match="text()">
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top