【问题标题】:Remove second node with same id in xslt删除 xslt 中具有相同 id 的第二个节点
【发布时间】:2014-11-28 10:56:03
【问题描述】:

我需要使用 XSLT 2.0 在 xml 文件中删除一些具有相同 ID 的节点。 结构是:

<Root>
  <media tipo="immagine" id="1">
    <numero>14.1</numero>
  </media>
  <media tipo="immagine" id="2">
    <numero>14.2</numero>
  </media>
  <media tipo="immagine" id="1">
    <numero>14.1</numero>
  </media>
</Root>

结果必须是:

<Root>
  <media tipo="immagine" id="1">
    <numero>14.1</numero>
  </media>
  <media tipo="immagine" id="2">
    <numero>14.2</numero>
  </media>
</Root>

我有多个具有相同属性 ID 值。 谢谢

【问题讨论】:

    标签: xml xslt xslt-2.0 exslt


    【解决方案1】:

    假设 id 是您想要比较和检查的所有内容

    <xsl:key name="by-id" match="*" use="@id"/>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*[@id and not(. is key('by-id', @id)[1])]"/>
    

    【讨论】:

      【解决方案2】:

      由于您使用的是 XSLT 2.0,您可以这样做:

      <xsl:stylesheet version="2.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      
      <xsl:template match="/Root">
          <Root>
              <xsl:for-each-group select="media" group-by="@id">
                  <xsl:copy-of select="current-group()[1]"/>
              </xsl:for-each-group>
          </Root>
      </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 2016-11-09
        • 2021-12-29
        • 2022-01-01
        • 1970-01-01
        相关资源
        最近更新 更多