简体   繁体   English

如何保留标记标记?

[英]How do I preserve markup tags?

I've got an XML document containing news stories, and the body element of a news story contains p tags amongst the plain text. 我有一个包含新闻故事的XML文档,新闻故事的主体元素在纯文本中包含p标签。 When I use XSL to retrieve the body, eg 当我使用XSL来检索身体时,例如

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

the p tags seem to get stripped out. p标签似乎被剥离了。 I'm using Visual Studio 2005's implementation of XSL. 我正在使用Visual Studio 2005的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 : w3schools的文档相同

The <xsl:copy-of> element creates a copy of the current node. <xsl:copy-of>元素创建当前节点的副本。

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: 如果您无法控制输入文档,则copy-of应该有效:

From http://www.xml.com/pub/a/2000/06/07/transforming/index.html 来自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:copy-of元素可以复制模板选择的每个节点的整个子树。如果xsl:copy-of元素的select属性具有适当的值,则包括属性。在下面的示例中,模板复制标题元素节点及其所有后代节点 - 换句话说,复制完整的标题元素,包括它们的标签,子元素和属性:

<xsl:template match="title">
  <xsl:copy-of select="*"/>
</xsl:template>

如果您可以控制输入文档, CDATA是正确的方法。

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. XML元素的价值 - 不仅在XSLT中,而且在DOM实现中 - 都是其所有后代文本节点的串联。 In XSLT, value-of emits an element's value, while copy-of emits a copy of the element. 在XSLT中, value-of发出元素的值,而copy-of发出元素的副本。

It is because the engine is interpreting the <p> tag (excluding it for the output). 这是因为引擎正在解释<p>标签(输出除外)。 You need to specify you want the content "as it is", using the "disable-output-escaping=yes|no" attribute. 您需要使用“disable-output-escaping = yes | no”属性指定您希望内容“按原样”。

<xsl:value-of select="body" disable-output-escaping="yes"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM