解析XSLT中选定节点的文本

时间:2014-02-17 11:04:59

标签: xslt xslt-1.0

输入XML是这样的:

<input>
  <foo>John&apos;s bar</foo>
  <bar>test</bar>
  <foobar>testing</foobar>
</input>

XSL转换后:

<input>
  <foo>John's bar</foo>
  <bar>this_test</bar>
</input>

但遗留系统预计:

<foo>John&apos;s bar</foo>

不是<foo>John's bar</foo>

所以我希望保留<foo>下的值,而不是让XSLT解析它。

我尝试使用<xsl:output method="text"/>但没有成功的运气......

我认为加载时XML本身会被解析,而XSLT只是按原样输出。 如果这是真的,我至少想要逃避它并使&apos;无论输入XML中是&apose还是'

我试过的XSLT是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="bar">
    <xsl:copy>
        <xsl:text>this_</xsl:text>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
<xsl:template match="foobar"/>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

如果您仅限于XSLT 1.0,请使用disable-output-escaping="yes"。此属性可用于xsl:textxsl:value-of元素,在XSLT 2.0中已弃用。

样式表(XSLT 1.0)

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:variable name="vApos">'</xsl:variable>
   <xsl:variable name="vAmp">&amp;</xsl:variable>

   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="node()"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="bar">
       <xsl:copy>
           <xsl:text>this_</xsl:text>
           <xsl:apply-templates/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="foobar"/>

   <xsl:template match="foo">
      <xsl:variable name="rep">
         <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="replace" select="$vApos" />
            <xsl:with-param name="with" select="concat($vAmp,'apos;')"/>
         </xsl:call-template>
      </xsl:variable>
      <xsl:copy>
         <xsl:value-of select="$rep" disable-output-escaping="yes"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

XSLT 1.0解决方案利用Dimitre Novatchev here和Mads Hansen的回答here给出的建议。

XSLT 2.0解决方案更优雅,使用character-map来控制输出的序列化。请确保您也转义&符号(&amp;apos;而不是&apos;)。

样式表(XSLT 2.0)

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes" use-character-maps="apo"/>
   <xsl:strip-space elements="*"/>

   <xsl:character-map name="apo">
      <xsl:output-character character="&apos;" string="&amp;apos;"/>
   </xsl:character-map> 

   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="node()"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="bar">
       <xsl:copy>
           <xsl:text>this_</xsl:text>
           <xsl:apply-templates/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="foobar"/>

</xsl:stylesheet>

输出(Saxon 9.5 for 2.0,Xalan 2.7.1 for 1.0)

<?xml version="1.0" encoding="UTF-8"?>
<input>
   <foo>John&apos;s bar</foo>
   <bar>this_test</bar>
</input>