tags:

views:

661

answers:

6

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.

+1  A: 

If you have control over the input document, CDATA is the right way to go.

Eugene Katz
Thanks but I cannot change the format of the document.
gilles27
A: 

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"/>
Enreeco
I tried this but it did not make any difference.
gilles27
try with false...I always don't remember the correct use
Enreeco
another errore...it was YES or NO
Enreeco
disable-output-escaping is for enabling/disabling the escaping of reserved characters (> becomes > or stays >).
Goran
+5  A: 

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!

Blair Conrad
Thanks Blair this has worked a treat.
gilles27
A: 

Please see http://www.xmlplease.com/xsltidentity

Goran
+1  A: 

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>
Eugene Katz
Thanks, this does work, I followed Blair Conrad's suggestion as I saw that first but you've both said the same thing.
gilles27
A: 

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.

Robert Rossney