Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 1st, 2015, 09:18 AM
Registered User
 
Join Date: Sep 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default valud-of - How to NOT include child elements?

Consider the following XML...

Code:
<myxml><hello>hello<world>world</world></hello></myxml>
And the following XSLT...

Code:
    <xsl:template match="myxml">

      <transformed>
        <xsl:value-of select="hello" />      
      </transformed>
    
    </xsl:template>
When we apply the transformation, the end result is...

Code:
<transformed>helloworld</transformed>
value-of returns the value of the selected element, <hello> and its children elements, in this case <world>. We want just the value of the selected element and NOT the children elements.

How do we just get the value of just the selected element, <hello>?

Thanks! -- Curt
 
Old September 1st, 2015, 09:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Do a recursive descent using template rules. For example

Code:
<xsl:template match="hello">
  <xsl:apply-templates/>
</xsl:template>

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

<xsl:template match="world"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 1st, 2015, 09:47 AM
Registered User
 
Join Date: Sep 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick reply, Michael.

Wow... That certainly does work. Now I just gotta figure out why... I'm a bit of a newbie.

Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stylesheet not returning child elements jorbri1513 XSLT 4 February 15th, 2013 05:59 AM
XPath to exclude child elements. WilliamYou XSLT 2 December 18th, 2012 12:58 PM
How to know whether the father element has child elements or not JohnKiller XSLT 7 October 18th, 2012 09:50 AM
Change ALL DateTime Child Elements BobSSC XSLT 2 February 18th, 2010 01:28 PM
Checking for valid child elements. Chamkaur XSLT 0 January 25th, 2007 06:51 PM





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