Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何将双引号替换为字符串'\&引用';在XSLT中?_Xml_Xslt - Fatal编程技术网

Xml 如何将双引号替换为字符串'\&引用';在XSLT中?

Xml 如何将双引号替换为字符串'\&引用';在XSLT中?,xml,xslt,Xml,Xslt,我有一个XML,其中双引号应替换为字符串\“ 例如:teset“message”here]> 因此,输出应该是teset\“message\”here]]> 有人能解释一下如何做到这一点吗?I.XSLT 1.0解决方案: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="y

我有一个XML,其中双引号应替换为字符串\“

例如:
teset“message”here]>

因此,输出应该是
teset\“message\”here]]>


有人能解释一下如何做到这一点吗?

I.XSLT 1.0解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>

"
\"
应用于提供的XML文档时

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>

在此处设置“消息”]>
生成所需的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>

在此处设置\“消息\”]>
II.XSLT 2.0解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>

"
\\"
应用于同一XML文档(如上所述)时,会产生相同的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPattern">"</xsl:param>
 <xsl:param name="pReplacement">\"</xsl:param>

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

 <xsl:template match="statement1/text()" name="replace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pPat" select="$pPattern"/>
  <xsl:param name="pRep" select="$pReplacement"/>

 <xsl:choose>
  <xsl:when test="not(contains($pText, $pPat))">
   <xsl:copy-of select="$pText"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:copy-of select="substring-before($pText, $pPat)"/>
   <xsl:copy-of select="$pRep"/>
   <xsl:call-template name="replace">
    <xsl:with-param name="pText" select=
         "substring-after($pText, $pPat)"/>
    <xsl:with-param name="pPat" select="$pPat"/>
    <xsl:with-param name="pRep" select="$pRep"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<root>
    <statement1><![CDATA[<u>teset "message"here</u>]]></statement1>
</root>
<root>
   <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"
 cdata-section-elements="statement1"/>

 <xsl:param name="pPat">"</xsl:param>
 <xsl:param name="pRep">\\"</xsl:param>

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

  <xsl:template match="statement1/text()">
   <xsl:sequence select="replace(.,$pPat, $pRep)"/>
  </xsl:template>
</xsl:stylesheet>
<root>
      <statement1><![CDATA[<u>teset \"message\"here</u>]]></statement1>
</root>

在此处设置\“消息\”]>


它对我起作用了……

问题是替换字符串有两个字符长。你看到的是引号的转义序列-然后我使用了散列标记。这将说明-你可以这样做-甚至将转义序列与符号混合:)正如我所说,问题是替换字符串有两个字符长。这是
\“
,而不是
#