【问题标题】:Remove empty tags from generated XML after XSLT transformationXSLT 转换后从生成的 XML 中删除空标签
【发布时间】:2020-10-03 05:50:17
【问题描述】:

我正在使用 XSLT 将输入 XML 转换为输出 XML。我的要求是在从输入进行 XSLT 转换期间,我需要从输出 XML 中删除所有空标签。

我尝试了这里给出的说明

https://stackoverflow.com/questions/6648679/removing-empty-tags-from-xml-via-xslt

但这可能是在讨论我们只编写用于从 XML 中删除空标签的 XSLT 的场景。在我的例子中,我必须在从输入 XML 转换到输出 XML 时消除空标签(在用于转换的同一个 XSLT 中)。你能建议我怎么做吗?

【问题讨论】:

  • 目前尚不清楚在 XSLT 转换的结果中您认为 empty tag 是什么,结果首先是可能被序列化的节点树,如果您想删除空节点,那么准确定义那是什么(没有子节点且没有属性的元素节点或只有空白内容的元素节点或?)。如果您想真正从输出中删除空节点,请链接两个转换,其中第一个的输出是第二个的输入。在 XSLT 3 的世界中也没有 xsl:where-populated w3.org/TR/xslt-30/#where-populated。
  • 请显示样本输入和所需输出,以及您迄今为止的最佳尝试。这有助于我们了解您在哪里遇到了困难。

标签: xml xslt-2.0


【解决方案1】:

在 XSLT 3 中,使用 xsl:where-populated 作为元素匹配中身份转换的“包装器”就足够了:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="*">
      <xsl:where-populated>
          <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates/>
          </xsl:copy>
      </xsl:where-populated>
  </xsl:template>
  
  <xsl:template match="foo"/>
  
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多