Search code examples
xmlxsltxpathxslt-2.0

XSL get the element value and ignore the nested element


How can I get the element value only without the sub-node value?

For example

XML

<root>
    <a>
      parent value
      <b>
         child value
      </b> 
    </a>
</root>

XSL

<xsl:for-each select="a">
    <xsl:call-template name="foo">
        <xsl:with-param name="elem" select="." />
    </xsl:call-template>
</xsl:for-each>

<xsl:template name="foo">
    <xsl:param name="elem" />
    
    <i>Val: <xsl:value-of select="$elem"/></i>
</xsl:template>

The output is: "parent valuechild value" And I want just to display "parent value"

Any suggestions?

Thanks!


Solution

  • Use either:

    <xsl:value-of select="$elem/text()"/>
    

    or:

    <xsl:value-of select="$elem/text()[1]"/>
    

    depending on whether you want to get the value of all text nodes that are children of a or only the first one of them.