I have an xml that looks like this:
<OuterTag> outerVal
<Name> value1 </Name>
<Desc> value2 </Desc>
</OuterTag>
and I want to retrieve the value of the outer tag ("outerVal").
when I use
xsl:value-of select="OuterTag" />
I get "outerValvalue1value2".
How can i retrieve only the outer value?
A complete solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" method="text"/>
<xsl:template match="/">
<xsl:value-of select="normalize-space(OuterTag/text()[1])" />
</xsl:template>
</xsl:stylesheet>
Output:
outerVal
Note: Whitespace is significant inside XML elements. Here's a stylesheet that reveals the placement/structure of the text nodes in OuterTag
and its children:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text" />
<xsl:template match="OuterTag/text()">
<xsl:value-of select="concat('[', ., ']')" />
</xsl:template>
<xsl:template match="OuterTag/*/text()">
<xsl:value-of select="concat('(', ., ')')" />
</xsl:template>
</xsl:stylesheet>
Output:
[ outerVal
]( value1 )[
]( value2 )[
]
Adding normalize-space
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text" />
<xsl:template match="OuterTag/text()">
<xsl:value-of select="concat('[', normalize-space(), ']')" />
</xsl:template>
<xsl:template match="OuterTag/*/text()">
<xsl:value-of select="concat('(', normalize-space(), ')')" />
</xsl:template>
</xsl:stylesheet>
Produces the following result:
[outerVal](value1)[](value2)[]
Try text():
<xsl:value-of select="OuterTag/text()" />
Use this: <xsl:value-of select="OuterTag/text()"/>
Adapt this xpath to your context
"/Outertag/text()[1]"
This will retrieve the first text node of the Outertag root node. The other answers show you how to get all text children nodes of Outertag context node.