selecting content though xpath with text()

Mike Kamermans

Usenet Archive
Joined
Aug 10, 2004
Messages
14
Reaction score
0
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
 

Joris Gillis

Usenet Archive
Joined
Sep 28, 2004
Messages
355
Reaction score
0
<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,
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
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
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top