XML - XSLT - Escape special characters

两盒软妹~` 提交于 2019-12-06 11:45:11

As XSLT always matches the most accurate template first you can just match setup/text-prop and create a CDATA block for this part specifically. Then depending on the XML, you can use apply-templates to continue matching the other elements.

It will probably look something like this:

 <xsl:template match="setup/text-prop">
  <xsl:copy>
    <setup>
      <text-prop>
         <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
         <xsl:value-of>whatever</xsl:value-of>
         <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </text-prop>
    </setup>
   <xsl:copy>
   <xsl:apply-templates/>
  </xsl:template>

Firstly, CDATA is not part of the XDM data model, it is considered to be purely a alternative way of escaping special characters: the two forms

<X><![CDATA[<>]]></X>

and

<X>&lt;&gt;</X>

are considered to be completely interchangeable.

This means your stylesheet can't distinguish which of the two was used on input: there is no way of knowing.

The cdata-section-elements property on xsl:output gives you some control over which form is used on output, but as you have discovered, it doesn't give you total control.

You can get more control by using disable-output-escaping, or character maps, or Andrew Welch's lexev utility, but all of these workarounds beg the question, why is it so important? If someone is treating the result document differently depending on whether it uses CDATA or not, then they are misusing XML and need to be re-educated.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!