How do I preserve markup tags?

2019-04-18 10:42发布

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.

标签: xml xslt
5条回答
啃猪蹄的小仙女
2楼-- · 2019-04-18 11:06

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>
查看更多
老娘就宠你
3楼-- · 2019-04-18 11:20

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!

查看更多
Root(大扎)
4楼-- · 2019-04-18 11:21

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

查看更多
贪生不怕死
5楼-- · 2019-04-18 11:21

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.

查看更多
Lonely孤独者°
6楼-- · 2019-04-18 11:22

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"/>
查看更多
登录 后发表回答