Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 September 17th, 2004, 10:39 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 
Old September 17th, 2004, 11:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old September 17th, 2004, 12:05 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
 
Old September 17th, 2004, 12:13 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old September 17th, 2004, 12:25 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old September 17th, 2004, 12:34 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old September 17th, 2004, 01:15 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
remove duplicate text in an output html element mrame XSLT 1 June 20th, 2008 01:41 AM
positioning text + form element + firefox Adam H-W CSS Cascading Style Sheets 2 June 12th, 2007 03:18 AM
Trouble Isolating Substring in Quotes tclotworthy XSLT 1 February 19th, 2007 04:31 AM
Turning element text into XPath Kael XSLT 5 October 14th, 2004 03:37 PM
Getting HTML element text using id badgolfer Javascript How-To 1 July 27th, 2004 12:17 PM





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