How can I get only the first, higher level, string?

I have this (token) XML file from which I want to print only the line "Print this", ignoring the following:

<tag1> Print this <tag2> Do not print this </tag2> </tag1> 

In my XSL file, with this command I get both the contents of tag1 and the contents of tag2 :

 <xsl:value-of select="tag1"/> 

Thanks!

+4
source share
3 answers

In my XSL file with this command, I get both the contents of tag1 and the contents of the printed tag2:

 <xsl:value-of select="tag1"/> 

How can I get only the first, higher level, string?

The code creates the string value of the tag1 element, which by definition is the concatenation of all text nodes-descendants of the element.

To create only

print this line

you need to specify an XPath expression that selects only the corresponding node text :

 /tag1/text()[1] 

Task [1] necessary to select only the first text node, otherwise two text nodes may be selected (this is a problem only in XSLT 2.0, where <xsl:value-of> creates string values โ€‹โ€‹of all specified nodes in the select attribute).

In addition, the above expression selects the entire text node, and its string value is not "Print this" .

The string value is actually:

 " Print this " 

and this is what will be output if you surround the <xsl:value-of> in quotation marks.

To create the desired line "Print this" , use :

 "<xsl:value-of select="normalize-space(/tag1/text()[1])"/>" 
+3
source

value-of element will give you the value of its text nodes and its descendants. If you just want an immediate text() node element, use this:

 <xsl:value-of select="tag1/text()"/> 
+3
source

<xsl:value-of select="tag1/text()"/> selects all text nodes in tag1

+2
source

All Articles