【问题标题】:Transform to remove the duplicate, take one element from duplicates变换删除重复项,从重复项中取一个元素
【发布时间】:2012-06-14 12:16:04
【问题描述】:

问题与此类似,earlier question 哪个 Dimitre 已经回答了。响应 xml 中略有修改。然而这一次,我们需要从重复块中提取一个元素。不确定我们是否可以在这里使用 xsl 键功能。

我的输入 xml

<M>
   <a>
      <b>
         <c f="123">
            <key>Al</key>
            <e NO="678">
               <f>Y</f>
               <g>
                  <h>FTO</h>
               </g>
            </e>
         </c>
      </b>
   </a>
   <a>
      <b>
         <c f="123">
            <key>Al</key>
            <e NO="678">
               <f>Y</f>
               <g>
                  <h>FTO</h>
               </g>
            </e>
         </c>
      </b>
   </a>
   <a>
      <b>
         <c f="567">
            <key>Al</key>
            <e NO="678">
               <f>Y</f>
               <g>
                  <h>FTO</h>
               </g>
            </e>
         </c>
      </b>
   </a>
   <a>
      <b>
         <somethingelse/>
      </b>
   </a>
</M>

我想要的输出 xml - 请注意,除了删除重复项之外,我们还需要从重复块中获取关键元素。它可能相同,也可能不同。

<M>
   <a>
      <b>
         <c f="123">
            <key>Al</key>
            <key>Al</key>
            <e NO="678">
               <f>Y</f>
               <g>
                  <h>FTO</h>
               </g>
            </e>
         </c>
      </b>
   </a>
   <a>
      <b>
         <c f="567">
            <key>Al</key>
            <e NO="678">
               <f>Y</f>
               <g>
                  <h>FTO</h>
               </g>
            </e>
         </c>
      </b>
   </a>
   <a>
      <b>
         <somethingelse/>
      </b>
   </a>
</M>

【问题讨论】:

    标签: xml xslt-1.0


    【解决方案1】:

    这种转变:

    <xsl:stylesheet version="1.0"
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
             <xsl:output omit-xml-declaration="yes" indent="yes"/>
             <xsl:strip-space elements="*"/>
    
             <xsl:key name="kAByC-F" match="a" use="*/c/@f"/>
    
             <xsl:template match="node()|@*">
                 <xsl:copy>
                   <xsl:apply-templates select="node()|@*"/>
                 </xsl:copy>
             </xsl:template>
    
             <xsl:template match=
              "a[*/c
               and
                 not(generate-id()
                    =
                     generate-id(key('kAByC-F', */c/@f)[1])
                     )
                ]"/>
    
             <xsl:template match="a/b/c[@f]">
              <c f="{@f}">
                <xsl:apply-templates select="key('kAByC-F', @f)/b/c/key"/>
                <xsl:apply-templates select="*[not(self::key)]"/>
              </c>
             </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <M>
        <a>
            <b>
                <c f="123">
                    <key>Al</key>
                    <e NO="678">
                        <f>Y</f>
                        <g>
                            <h>FTO</h>
                        </g>
                    </e>
                </c>
            </b>
        </a>
        <a>
            <b>
                <c f="123">
                    <key>Al</key>
                    <e NO="678">
                        <f>Y</f>
                        <g>
                            <h>FTO</h>
                        </g>
                    </e>
                </c>
            </b>
        </a>
        <a>
            <b>
                <c f="567">
                    <key>Al</key>
                    <e NO="678">
                        <f>Y</f>
                        <g>
                            <h>FTO</h>
                        </g>
                    </e>
                </c>
            </b>
        </a>
        <a>
            <b>
                <somethingelse/>
            </b>
        </a>
    </M>
    

    产生想要的正确结果:

    <M>
       <a>
          <b>
             <c f="123">
                <key>Al</key>
                <key>Al</key>
                <e NO="678">
                   <f>Y</f>
                   <g>
                      <h>FTO</h>
                   </g>
                </e>
             </c>
          </b>
       </a>
       <a>
          <b>
             <c f="567">
                <key>Al</key>
                <e NO="678">
                   <f>Y</f>
                   <g>
                      <h>FTO</h>
                   </g>
                </e>
             </c>
          </b>
       </a>
       <a>
          <b>
             <somethingelse/>
          </b>
       </a>
    </M>
    

    解释:

    1. identity rule“按原样”复制每个节点。它被两个匹配特定节点的模板覆盖。

    2. 第一个覆盖模板会删除所有 a 元素,这些元素不是使用键 (Muenchian grouping method) 指定的第一个 a 元素。

      李>
    3. 第二个覆盖模板匹配并处理未删除(由第一个覆盖模板)a 元素的c 孙子元素。

    【讨论】:

    • 好极了。谢谢迪米特。我仍在调试线路以了解流程。
    • @Suresh:不客气。我用解释更新了问题。
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 2015-09-30
    • 2018-08-21
    • 1970-01-01
    • 2014-01-15
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多