selecting content though xpath with text()

M

Mike Kamermans

I'm having some trouble using text() in an xsl:value-of xpath. I have the
following xml:

....
<graphemes>
<grapheme>1</grapheme>
<grapheme>2</grapheme>
<grapheme>3</grapheme>
</graphemes>
....

and I want to xslt it into just "<graphemes>123</graphemes>". I used the
following xslt:

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

but that results in "<graphemes/>" being generated. If I use
select="./node()" instead, I just get "<graphemes>1</graphemes>"

how should I be doing this?


- Mike Kamermans
 
J

Joris Gillis

<grapheme>1</grapheme>
<grapheme>2</grapheme>
<grapheme>3</grapheme>
</graphemes>

I want to xslt it into just "<graphemes>123</graphemes>". I used the
following xslt:

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

Hi,

With <xsl:value-of select="./text()"> , you're selecting the immediate
text nodes of the 'graphemes' element. But this element only contains
other elements named 'grapheme'. It are these 'grapheme' elements that
contain the text nodes.
And also, 'xsl:value-of' only selects the first node.
So you should be using:
<xsl:copy-of select="grapheme/text()"/>


But if the problem is really as simple as you've posted, then just use:

<xsl:template match="graphemes">
<xsl:value-of select="."/>
</xsl:template>

regards,
 
D

David Carlisle

Mike Kamermans said:
I'm having some trouble using text() in an xsl:value-of xpath. I have the
following xml:

...
<graphemes>
<grapheme>1</grapheme>
<grapheme>2</grapheme>
<grapheme>3</grapheme>
</graphemes>
...

and I want to xslt it into just "<graphemes>123</graphemes>". I used the
following xslt:

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

but that results in "<graphemes/>" being generated. If I use
select="./node()" instead, I just get "<graphemes>1</graphemes>"

how should I be doing this?


- Mike Kamermans

text() selects the text nodes of graphemes which in your case just
consists of white space text noded used to indent the grapheme elements.

You don't want the text node _children_ you want teh string value of teh
whol eelement (which is the character data in all _descendants_) so just
use select="."

By default that will give you
" 1
2
3
"

If you don't want the space to be considered use

<xsl:strip-space elements="graphemes"/>

at the top level of your stylesheet.


David
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top