I hope my title does justice for the question. Please consider the following block of XML and sample block of XSL.
<root>
<level_one>
My first line of text on level_one
<level_two>
My only line of text on level_two
</level_two>
My second line of text on level_one
</level_one>
</root>
<xsl:template match="level_one">
<xsl:value-of select="text()"/>
<br/>
<xsl:apply-templates select="level_two"/>
</xsl:template>
<xsl:template match="level_two">
<xsl:value-of select="text()"/>
<br/>
</xsl:template>
As it stands, the output (modified here for reading) when executing the above is
My first line of text on level_one
<br/>
My only line of text on level_two
<br/>
I’m missing the second line of text on level_one. So I’m wondering two things.
- Is the XML valid? From what I know, the answwer is yes, but am I wrong?
- How can I modify the XSL in order to get the second line (or even more lines in my case than I’ve shown)?
Thanks
Yes your XML is valid. Also, contrary to a comment above, there is nothing wrong with having mixed content (text and elements mixed together) in XML. It all depends on context and how the XML is used. For example, it would be almost impossible to author technical manuals without mixed content. (A good example is reference elements mixed with text in paragraph elements.)
I’m not sure exactly what you’re trying to accomplish, but the reason you’re not seeing the second line of text is because you’re only matching the first line with the first
<xsl:value-of select="text()"/>.I’m not sure if this would work on your full set of XML data, but you could replace both
level_oneandlevel_twotemplates with one template that matches alltext():This produces the following output:
You could also narrow the match down by specifying the level_one and level_two parents:
This produces the exact same output, but leaves any other text open for matching in other templates.
Hope this helps.