xml - XSLT 删除所有属性的前导和尾随空格

标签 xml xslt xslt-2.0

如何创建相同的 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/

相关文章:

xml - 我想实现一个 `split function/template` ,它将字符串和分隔符作为输入,并返回字符串的分割数组

C#反序列化xml文件形成List<T>

javascript - 一些 Jquery 中的错误

json - XSLT 1.0 转义字符串中的双引号和反斜杠

xml - 使用 XSLT 删除字符串的第一个和最后一个字符

xslt - FOP xslt 中 region-before 和 region-body 之间的相对空间

xslt - 如何在XSLt 2.0中为每个循环丢弃重复结构。

Python、Pandas、XML - 根据长度分割 XML 元素

c# - 验证 xml 的有效方法?

c# - 如何将sql查询结果制作成xml文件