【问题标题】:Use XSLT to transform XML with conditionals upon node count使用 XSLT 根据节点数转换带有条件的 XML
【发布时间】:2012-03-19 00:22:39
【问题描述】:

我正在尝试从 XML 文件中删除节点。对我的每个 XML 只使用一个 XSLT 我需要根据文档元素的子元素数量在 XSLT 中做出决定。

<root>
  <branch>
    <foo>bar</foo>
  </branch>
<root>

应该变成

  <branch>
  </branch>

但是

<root>
  <branch>
    <foo>bar</foo>
  </branch>
  <branch>
    <foo>baz</foo>
<root>

进入

<root>
  <branch>
  </branch>
  <branch>
  </branch>
<root>

也就是说,如果根元素(唯一的)子元素在应用 XSLT 后可以充当结果 XML 的新文档根,则应该删除根元素。 &lt;foo&gt; 节点必须在每次出现时都被删除。

有没有办法使用单个 XSL 执行此操作?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    试试

    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root[*[2]]">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root[* and not(*[2])]">
      <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="branch/foo"/>
    

    【讨论】:

    • +1 谢谢,似乎可以在我的沙箱中使用。我现在正在学习 XSLT:像 [* and not...] 这样的正则表达式是否需要 XSLT 2.0?
    • 字符串匹配的正则表达式仅在 XSLT 和 XPath 2.0 中支持。但是我只使用了 XSLT 1.0 元素匹配模式,因为 match="root[*[2]]" 只是 match="root[child::*[2]]" 的简写形式,意思是匹配 root 元素,至少有两个任意名称的子元素。
    【解决方案2】:

    更简单、更短、更通用(没有硬编码元素名称)的解决方案:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*[not(*[2])]">
      <xsl:apply-templates/>
     </xsl:template>
    
     <xsl:template match="/*/*/node()"/>
    </xsl:stylesheet>
    

    当此转换应用于第一个提供的 XML 文档时(已更正为格式正确):

    <root>
        <branch>
            <foo>bar</foo>
        </branch>
    </root>
    

    产生想要的正确结果:

    <branch></branch>
    

    当对第二个提供的 XML 文档应用相同的转换时(再次需要更正以确保格式正确):

    <root>
        <branch>
            <foo>bar</foo>
        </branch>
        <branch>
            <foo>baz</foo>
        </branch>
    </root>
    

    再次生成所需的正确输出:

    <root>
        <branch></branch>
        <branch></branch>
    </root>
    

    解释:

    1. identity rule“按原样”复制每个节点。

    2. 有两个模板覆盖特定节点的标识模板并以不同方式处理这些节点。

    3. 第一个覆盖模板匹配没有第二个子元素的顶部元素。它不会复制元素本身,而是处理其子元素。

    4. 第二个覆盖模板匹配顶部元素的孙子元素。此模板没有正文,这意味着所有此类匹配的元素都将被忽略且不包含在输出中(换句话说——“已删除”)

    请注意:

    这种转换可以应用于任何 XML 文档,无论其中的元素名称如何,并且仍然会产生想要的正确结果。

    例如,应用于此 XML 文档时:

    <t>
        <b>
            <f>brrr</f>
        </b>
        <b>
            <f>bzzz</f>
        </b>
    </t>
    

    产生想要的正确结果:

    <t>
        <b></b>
        <b></b>
    </t>
    

    将此与当前接受的答案产生的结果进行对比:

    <t>
        <b>
            <f>brrr</f>
        </b>
        <b>
            <f>bzzz</f>
        </b>
    </t>
    

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2015-03-12
      相关资源
      最近更新 更多