我打算使用 XSLT 从以下数据中清除一些不需要的符号:
<Cart>
<Row>
<Ld>Restaurant/Cafe Bar & Stores J-K,3</Ld>
<Ln>Restaurant/Cafe Bar & Stores J-K</Ln>
</Row>
</Cart>
如何删除以下符号
&
/
结果数据将是
<Cart>
<Row>
<Ld>Restaurant,Cafe Bar, Stores J-K,3</Ld>
<Ln>Restaurant, Cafe Bar, Stores J-K</Ln>
</Row>
</Cart>
最佳答案
您可以使用translate()功能:
应用于身份转换以将这些字符替换为 ,
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<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>
通过 XSLT/XPath 2.0,您可以使用 replace()函数,为查找/替换操作以及前导/尾随空格标准化等提供了更强大的功能。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="@*| node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="replace(., '\s?(&|/)\s?', ', ')"/>
</xsl:template>
</xsl:stylesheet>
关于xml - 用于清理数据的 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239290/