Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
使用XSLT将实体转换为键盘字符_Xslt - Fatal编程技术网

使用XSLT将实体转换为键盘字符

使用XSLT将实体转换为键盘字符,xslt,Xslt,我需要转换 实体转换为正常空间(键盘空间)。但是我得到了实体 而不是空格 示例XML: <chapter xmlns="http://www.w3.org/1998/Math/MathML"> <math display='block'> <mrow> <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext> </mrow&

我需要转换
 实体转换为正常空间(键盘空间)。但是我得到了实体
 而不是空格

示例XML:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

x ;y + ;y ;x
XSLT1.0已试用:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

所需输出:

<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><math display="block"><mrow><mtext>x y + y x</mtext></mrow></math></chapter>

xy+yx
输出:

<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><math display="block"><mrow><mtext>x&#160;y&#160;+&#160;y&#160;x</mtext></mrow></math></chapter>

x ;y + y ;x

 ;

 ;

简单得多

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

这里是一个完整的转换:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

当此转换应用于提供的XML文档时

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

x ;y + ;y ;x
生成所需的正确结果:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

xy+yx
记住

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

用另一个字符替换单个字符(或删除它)的最佳方法是使用标准XPath函数

简单得多

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

这里是一个完整的转换:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

当此转换应用于提供的XML文档时

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

x ;y + ;y ;x
生成所需的正确结果:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="mtext">
<xsl:for-each select="contains(.,'&#x00A0;')">
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
</xsl:template>

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

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

 <xsl:template match="text()">
     <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
 </xsl:template>
</xsl:stylesheet>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
   <math display="block">
      <mrow>
         <mtext>x y + y x</mtext>
      </mrow>
   </math>
</chapter>

xy+yx
记住

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA0;', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
 <mrow>
  <mtext>x&#x00A0;y&#x00A0;+&#x00A0;y&#x00A0;x</mtext>
 </mrow>
</math>
</chapter>

用另一个字符替换单个字符(或删除它)的最佳方法是使用标准XPath函数

您可以添加您尝试获得的输出吗?@Treemonkey:请查找我2012年获得的输出,有一个简单得多的解决方案--请看一看。不要使用禁用输出转义。尤其是当没有什么可以逃避的时候——那只是噪音。XSLT1.0处理器应该在这方面给您一个错误,因为xsl:for each指令的select属性的计算结果是布尔值,而不是节点集。您没有收到错误的唯一原因是您的模板规则实际上不匹配,因为您忘记了将quality=“mtext”与命名空间匹配。您可以添加您尝试获得的输出吗?@Treemonkey:请查找我2012年获得的输出,有一个简单得多的解决方案--请看一看。不要使用禁用输出转义。尤其是当没有什么可以逃避的时候——那只是噪音。XSLT1.0处理器应该在这方面给您一个错误,因为xsl:for each指令的select属性的计算结果是布尔值,而不是节点集。您没有收到错误的唯一原因是您的模板规则实际上不匹配,因为您忘记了将quality match=“mtext”与名称空间匹配。非常感谢,现在我使用此XSLTI获得了正确的输出。我不明白它为什么会起作用,“mtext”未限定名称空间的问题仍然存在。非常感谢,现在,我使用这个XSLTI获得了正确的输出。我不明白为什么它会起作用,仍然存在“mtext”未限定名称空间的问题。嗨,Dimitre,我从来没有想过这会起作用,因为我假设它需要 ;作为一个string@Treemonkey,字符串
和#xA0
实际上是一个字符实体,由XML解析器转换为该实体表示的单个字符。XSLT处理器将其视为单个字符,实际上并不知道存在实体替换。无论
与#xA0
 时,XSLT处理器只看到相同的字符;作为一个string@Treemonkey,字符串
和#xA0
实际上是一个字符实体,由XML解析器转换为该实体表示的单个字符。XSLT处理器将其视为单个字符,实际上并不知道存在实体替换。无论
与#xA0
 时,XSLT处理器只会看到相同的字符。