如何在结果树中保留源代码树中的转义字符?



我正在编写一个XSL,旨在将一个XML文件转换为另一个XML文件。源树在文本节点中包含&。我希望将其保留在结果树中,但在输出后,它变成了裸 &.

我的<xsl:output>具有以下属性:method="xml",版本="1.0",编码="UTF-8",独立="是"。

我尝试将字符映射用于 &(无法执行此操作,因为裸 & 是无效的 XML,XSL 是 XML(、&amp;(不匹配任何内容(和&amp;amp;(多个字符,因此无法适应@character<xsl:output-character>

如果我将&amp;的源 XML 实例更改为&amp;amp;,则 XSL 输出具有所需的&amp;。但是,我不想在这个过程中引入另一个步骤。

我也尝试制作输出 html。这无济于事。

我尝试对可能包含&amp;的结果节点使用 cdata-section-elements ,但我从解析器中得到一个错误:{cdata-section-elements} 的值必须是 '{uri}local' 表示法中的 QNames 列表

<?xml version="1.0" encoding="UTF-8"?>
<PRESENTATION>
<CONTROL_ID>3199846</CONTROL_ID>
<PRESENTATION_ID>2726948</PRESENTATION_ID>
<SESSION_TRACK>abc</SESSION_TRACK>
<FINAL_ID>4</FINAL_ID>
<TITLE>abc</TITLE>
<STATUS>Sessioned</STATUS>
<AUTHORS>
<AUTHOR order="1" person_id="5811496" presenter="true">
<FNAME>Newhart</FNAME>
<MNAME/>
<LNAME>Bob</LNAME>
<EMAIL>newhart@bob.com</EMAIL>
<DEGREE/>
<AFFILIATIONS>
<AFFL author_order="1" number="1">
<DEPT>Psychiatry &amp; Other things</DEPT>
<INST>Zippers</INST>
<CITY>Frankfurt</CITY>
<STATE>Hesse</STATE>
<COUNTRY>Germany</COUNTRY>
</AFFL>
</AFFILIATIONS>
</AUTHOR>
</AUTHORS>
<BODY>
<SECTION part_of_body="0">
<SECTION_NAME bold="true"
italic="false"
underline="false"
name_appears="true">Abstract</SECTION_NAME>
<TEXT>This will not be relevant.</TEXT>
</SECTION>
</BODY>
</PRESENTATION>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml"
version="1.0"
encoding="UTF-8"
standalone="yes"/>

<xsl:template match="/">
<xsl:choose>
<xsl:when test="/PRESENTATION/AUTHORS/AUTHOR">
<contrib-group>
<!-- Handle 1 or more authors -->
<xsl:for-each select="/PRESENTATION/AUTHORS/AUTHOR">
<xsl:variable name="currentAuthor" select="current()"/>
<contrib>
<name name-style="western">
<surname>
<xsl:value-of select="$currentAuthor/LNAME"/>
</surname>
<given-names>
<xsl:value-of select="$currentAuthor/FNAME"/>
<xsl:if test="matches($currentAuthor/MNAME, '.+')">
<xsl:value-of select="concat(' ', ./MNAME)"/>
</xsl:if>
</given-names>
</name>
<xsl:for-each select="/$currentAuthor/AFFILIATIONS/AFFL">
<xsl:variable name="currentAff" select="current()"/>
<xsl:if test="position() = 1">
<aff>
<xsl:if test="$currentAff/DEPT">
<xsl:apply-templates select="$currentAff/DEPT"/>
</xsl:if>
<xsl:if test="$currentAff/INST">
<institution>
<xsl:apply-templates select="$currentAff/INST"/>
</institution>
</xsl:if>
</aff>
</xsl:if>
</xsl:for-each>
</contrib>
</xsl:for-each>
</contrib-group>
</xsl:when>    
</xsl:choose>
</xsl:template>

<xsl:template match="text()">
<xsl:if test="position() > 1">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" disable-output-escaping="yes"/>
</xsl:template>
<xsl:template name="superscript-preserver" match="sup">
<sup>
<xsl:value-of select="normalize-space(.)"/>
</sup>
</xsl:template>
<xsl:template name="subscript-preserver" match="sub">
<sub>
<xsl:value-of select="normalize-space(.)"/>
</sub>
</xsl:template>
<xsl:template name="italic-space-normalizer" match="i">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template name="bold-space-normalizer" match="b">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template name="underline-space-normalizer" match="u">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template name="br-space-normalizer" match="br">
<xsl:value-of select="normalize-space(.)"/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template name="square-bracket-extractor" match="/PRESENTATION/SESSION_TRACK">
<!-- extract string between square brackets -->
<xsl:analyze-string select="." regex="(.+)">
<xsl:matching-substring>
<!-- Take the result from the 1st regex match group and do a replacement. -->
<xsl:sequence select="replace(regex-group(1), 'I&amp;EC', 'I+EC')"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="final-id-format" match="/PRESENTATION/FINAL_ID">
<xsl:value-of select="format-number(., '0000')"/>
</xsl:template>
</xsl:stylesheet>

您已通过使用 disable-output-escaping="yes" 显式要求处理器不要转义 & 符号。如果您希望它转义,请不要禁用转义。

最新更新