如何创建相同的 XML 工作表,但删除了每个属性的前导和尾随空格? (使用 XSLT 2.0)
从这里开始:
<node id="DSN ">
<event id=" 2190 ">
<attribute key=" Teardown"/>
<attribute key="Resource "/>
</event>
</node>
对此:
<node id="DSN">
<event id="2190">
<attribute key="Teardown"/>
<attribute key="Resource"/>
</event>
</node>
我想我更喜欢使用 normalize-space()
函数,但不管用什么。
最佳答案
normalize-space()
不仅会删除前导和尾随的空格,还会安装一个空格字符来代替任何连续的空格字符序列。
正则表达式可用于处理前导和尾随空格:
<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="@*">
<xsl:attribute name="{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="replace(., '^\s+|\s+$', '')"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
关于xml - XSLT 删除所有属性的前导和尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573215/