Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
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.
Reply With Quote
  #3 (permalink)  
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?
Reply With Quote
  #4 (permalink)  
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)
Reply With Quote
  #5 (permalink)  
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.

Reply With Quote
  #6 (permalink)  
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)
Reply With Quote
  #7 (permalink)  
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:29 PM.


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