I've got an XML document containing news stories, and the body element of a news story contains p tags amongst the plain text. When I use XSL to retrieve the body, e.g.
<xsl:value-of select="body" />
the p tags seem to get stripped out. I'm using Visual Studio 2005's implementation of XSL.
Does anyone have any ideas how to avoid this? Thanks.
Try to use
<xsl:copy-of select="body"/>
instead. From w3schools' documentation on same:
The <xsl:copy-of>
element creates a
copy of the current node.
Note: Namespace nodes, child nodes,
and attributes of the current node are
automatically copied as well!
If you don't have control over the input document, copy-of should work:
From http://www.xml.com/pub/a/2000/06/07/transforming/index.html
"The xsl:copy-of element, on the other hand, can copy the entire subtree of each node that the template selects. This includes attributes, if the xsl:copy-of element's select attribute has the appropriate value. In the following example, the template copies title element nodes and all of their descendant nodes -- in other words, the complete title elements, including their tags, subelements, and attributes:"
<xsl:template match="title">
<xsl:copy-of select="*"/>
</xsl:template>
If you have control over the input document, CDATA is the right way to go.
The value of an XML element - this is true not just in XSLT but in DOM implementations - is the concatenation of all of its descendant text nodes. In XSLT, value-of
emits an element's value, while copy-of
emits a copy of the element.
It is because the engine is interpreting the <p> tag (excluding it for the output). You need to specify you want the content "as it is", using the "disable-output-escaping=yes|no" attribute.
<xsl:value-of select="body" disable-output-escaping="yes"/>