 |
| 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
|
|
|
|

September 17th, 2004, 10:39 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Isolating element text
I have a piece of XML:
<Question>
The term "onion-skin weathering" refers to layers or sheets of rock that gradually break off. This action is commonly called
<AnswerChoices>
<Distractor>abrasion</Distractor>
<Distractor>vegetation</Distractor>
<Distractor>peeling</Distractor>
<Answer>exfoliation</Answer>
</AnswerChoices>
</Question>
I need to isolate the text in the Question element without selecting the child AnswerChoices/Distractor or AnswerChoices/Answer elements. In my Question template, I've tried:
<xsl:value-of select="self::text()" />, which doesn't pull anything at all
and
<xsl:value-of select="self::node()" />, which pulls everything into a single clump
<xsl:value-of select="." />, also pulls everything in.
The last two output
The term "onion-skin weathering" refers to layers or sheets of rock that gradually break off. This action is commonly called
abrasion
vegetation
peeling
exfoliation
What I'm trying to isolate is:
The term "onion-skin weathering" refers to layers or sheets of rock that gradually break off. This action is commonly called
I'm very new to XSLT; can someone point me in the right direction? I do have separate templates that process the Answer and Distractor tags; when I call them, it repeats those elements.
|
|

September 17th, 2004, 11:28 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think you'd be better off if you place your text in an element:
<Question>
<Q>
..your text here..
</Q>
<AnswerChoices>
...
Not that you can't select the text using your structure, but I had a lot of trouble doing the same and was advised to change it.
|
|

September 17th, 2004, 12:05 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the advice, but that's not an option. I'm the stylesheet developer only - I have no say in how the XML is marked up, and they are dead set on going this way.
You said you had trouble doing it, but it sounds like it should be possible. Would you mind sharing the method?
|
|

September 17th, 2004, 12:13 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Mixed content can be very difficult. I think for the current example you need something like:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Question">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

September 17th, 2004, 12:25 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Joe, that's exactly what I needed! If you don't mind taking a few minutes with the newbie, could you please explain why that worked but selecting self::text() didn't? I was under the impression that it would find the current node (self) and then select the text() of it.
Thanks so much.
|
|

September 17th, 2004, 12:34 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Because you want the child nodes of Question that are text, self::text() means that you want the context node if it's a text node. Probably technically a bad explanation but I hope you understand.
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

September 17th, 2004, 01:15 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No, that makes sense. I'm still getting used to the fact that each element is really two nodes, the element itself and its text, which is really a child. I needed the child text node, but wasn't remembering that it was actually a child, and not the element itself.
Thanks again for the clarification.
|
|
 |