使用XSLT将实体转换为键盘字符
我需要转换使用XSLT将实体转换为键盘字符,xslt,Xslt,我需要转换 实体转换为正常空间(键盘空间)。但是我得到了实体 而不是空格 示例XML: <chapter xmlns="http://www.w3.org/1998/Math/MathML"> <math display='block'> <mrow> <mtext>x y + y x</mtext> </mrow&
使用XSLT1.0将代码>实体转换为正常空间(键盘空间)。但是我得到了实体 代码>而不是空格
示例XML:
<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'?>
<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(.,' ')">
<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(., ' ', ' ')"/>
</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 y + y 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(.,' ')">
<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(., ' ', ' ')"/>
</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 y + y x</mtext></mrow></math></chapter>
x ;y + y ;x
;
;
简单得多:
<xsl:template match="text()">
<xsl:value-of select="translate(., ' ', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
<mrow>
<mtext>x y + y x</mtext>
</mrow>
</math>
</chapter>
这里是一个完整的转换:
<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'?>
<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(.,' ')">
<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(., ' ', ' ')"/>
</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(., ' ', ' ')"/>
</xsl:template>
<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
生成所需的正确结果:
<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'?>
<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(.,' ')">
<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(., ' ', ' ')"/>
</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(., ' ', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
<mrow>
<mtext>x y + y x</mtext>
</mrow>
</math>
</chapter>
用另一个字符替换单个字符(或删除它)的最佳方法是使用标准XPath函数简单得多:
<xsl:template match="text()">
<xsl:value-of select="translate(., ' ', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
<mrow>
<mtext>x y + y x</mtext>
</mrow>
</math>
</chapter>
这里是一个完整的转换:
<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'?>
<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(.,' ')">
<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(., ' ', ' ')"/>
</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(., ' ', ' ')"/>
</xsl:template>
<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
生成所需的正确结果:
<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'?>
<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(.,' ')">
<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(., ' ', ' ')"/>
</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(., ' ', ' ')"/>
</xsl:template>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math display='block'>
<mrow>
<mtext>x y + y 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使用代码>或 使用code>时,XSLT处理器只看到相同的字符;作为一个string@Treemonkey,字符串和#xA0
实际上是一个字符实体,由XML解析器转换为该实体表示的单个字符。XSLT处理器将其视为单个字符,实际上并不知道存在实体替换。无论与#xA0使用代码>或 当使用code>时,XSLT处理器只会看到相同的字符。