Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 转换要删除重复项,请从重复项中提取一个元素_Xml_Xslt 1.0 - Fatal编程技术网

Xml 转换要删除重复项,请从重复项中提取一个元素

Xml 转换要删除重复项,请从重复项中提取一个元素,xml,xslt-1.0,Xml,Xslt 1.0,问题与此类似,, 迪米特已经回答了。在响应xml中有一个小小的修改。然而,这一次,我们需要从重复块中提取一个元素。不确定我们是否可以在这里使用xsl键函数 我的输入xml <M> <a> <b> <c f="123"> <key>Al</key> <e NO="678"> <f>Y<

问题与此类似,, 迪米特已经回答了。在响应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>

艾尔
Y
FTO
艾尔
Y
FTO
艾尔
Y
FTO
我想要的输出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>

艾尔
艾尔
Y
FTO
艾尔
Y
FTO

此转换:

<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>
<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>

应用于提供的XML文档时

<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>
<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>

按原样复制每个节点。它由两个匹配特定节点的模板覆盖

  • 第一个覆盖模板删除所有不是使用键(指定的第一个
    a
    元素的
    a
    元素

  • 第二个覆盖模板匹配并处理(由第一个覆盖模板)未删除的
    a
    元素的
    c
    子元素


  • 棒 极 了谢谢你,迪米特。我仍在调试这些行,以便在流下运行。@Suresh:不客气。我用解释更新了问题。