How do I preserve markup tags?

后端 未结 5 1535
太阳男子
太阳男子 2021-02-18 23:52

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.

相关标签:
5条回答
  • 2021-02-18 23:53

    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>
    
    0 讨论(0)
  • 2021-02-18 23:54

    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"/>
    
    0 讨论(0)
  • 2021-02-19 00:03

    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!

    0 讨论(0)
  • 2021-02-19 00:04

    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.

    0 讨论(0)
  • 2021-02-19 00:18

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

    0 讨论(0)
提交回复
热议问题