按原样分析XSLT中选定节点的文本

按原样分析XSLT中选定节点的文本,xslt,xslt-1.0,Xslt,Xslt 1.0,输入XML如下所示: <input> <foo>John&apos;s bar</foo> <bar>test</bar> <foobar>testing</foobar> </input> 如果仅限于XSLT1.0,请使用禁用输出转义=“yes”。此属性可用于xsl:text和xsl:value元素,在XSLT2.0中不推荐使用 样式表(XSLT 1.0) 输出(Saxon 9

输入XML如下所示:

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

如果仅限于XSLT1.0,请使用
禁用输出转义=“yes”
。此属性可用于
xsl:text
xsl:value
元素,在XSLT2.0中不推荐使用

样式表(XSLT 1.0)

输出(Saxon 9.5代表2.0,Xalan 2.7.1代表1.0)


约翰&apos;酒吧
这次考试

您正在XSLT 1.0中使用replace()。您是对的。我编辑了我的帖子。很抱歉今天有点“困惑”。
<input>
  <foo>John's bar</foo>
  <bar>this_test</bar>
</input>
<foo>John&apos;s bar</foo>
<?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>
<?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>
<?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>
<?xml version="1.0" encoding="UTF-8"?>
<input>
   <foo>John&apos;s bar</foo>
   <bar>this_test</bar>
</input>