使用 XSLT 将具有相同 id 的非空元素合并到单个项目中

发布于 2024-11-06 07:24:21 字数 6615 浏览 0 评论 0 原文

我想将具有相同 id 的项目合并为单个项目。生成的项目需要具有项目组中的第一个非空元素。如果项目组中没有非空元素,则该元素本身会从项目组中忽略,并且不应出现在结果项目中。

示例 XML 输入:

<?xml version="1.0" encoding="UTF-8" ?>
<shiporder orderid="orderid106">
   <orderperson id="123">orderperson107</orderperson>
   <shipto>
      <name>name108</name>
      <address>address109</address>
      <city>city110</city>
      <country>country111</country>
   </shipto>

   <!--Item Group 100-->
   <item>
      <id>100</id>
      <title>
         <first>
            <a></a>
         </first>
         <last>item100_lastTitle1</last>
      </title>
      <note></note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a>a_100_2</a>
         </first>
         <last>item100_lastTitle2</last>
      </title>
      <note>note100_2</note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a att1="abc" att2='cde'>a_100_3</a>
         </first>
         <last id="1" attr="2">item100_lastTitle3</last>
      </title>
      <note>note100_3</note>
      <quantity>1</quantity>
   </item>

   <!--Item Group 101-->
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_1</a>
         </first>
         <last></last>
      </title>
      <note>note101_1</note>
      <quantity>10</quantity>
   </item>
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_2</a>
         </first>
         <last>item101_lastTitle2</last>
      </title>
      <note>note101_2</note>
      <quantity>5</quantity>
   </item>

   <!--Item Group 103-->
   <item>
      <id>103</id>
      <title>
         <first>
            <a>a_103_2</a>
         </first>
         <last>item103_lastTitle2</last>
      </title>
      <note>note103_1</note>
      <quantity></quantity>
   </item>
</shiporder>

示例 XML 输出:

<?xml version = '1.0' encoding = 'UTF-8'?>
<shiporder orderid="orderid106">
  <item>
    <id>100</id>
    <title>
      <first>
        <a>a_100_2</a>
      </first>
      <last>item100_lastTitle1</last>
    </title>
    <note>note100_2</note>
    <quantity>6</quantity>
  </item>

  <item>
    <id>101</id>
    <title>
      <first>
        <a>a_101_1</a>
      </first>
      <last>item101_lastTitle2</last>
    </title>
    <note>note101_1</note>
    <quantity>10</quantity>
  </item>

  <item>
    <id>103</id>
    <title>
      <first>
        <a>a_103_2</a>
      </first>
      <last>item103_lastTitle2</last>
    </title>
    <note>note103_1</note>
  </item>
</shiporder>

我尝试使用以下 XSL 代码来获得上述输出:

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/shiporder">
      <shiporder>
         <xsl:if test="@orderid">
            <xsl:attribute name="orderid">
               <xsl:value-of select="@orderid"/>
            </xsl:attribute>
         </xsl:if>

         <!-- Grouping each item based on its ID element value-->
         <xsl:for-each-group select="item" group-by="id">
            <item-group>
               <!-- For Each group search for first non empty child element-->
               <xsl:for-each select="current-group()[id/text()][1]">
                  <xsl:copy-of select="id"/>
               </xsl:for-each>
               <title>
                  <first>
                     <!-- for 'a' element -->
                     <xsl:variable name="temp1">
                        <xsl:for-each select="current-group()/title/first[a/text()][1]">
                           <elm>
                              <xsl:copy-of select="a"/>
                           </elm>
                        </xsl:for-each>
                     </xsl:variable>
                     <xsl:copy-of select="$temp1/elm[1]/a"/>
                  </first>

                  <!-- for 'last' element -->
                  <xsl:variable name="temp2">
                     <xsl:for-each select="current-group()/title[last/text()][1]">
                        <elm>
                           <xsl:copy-of select="last"/>
                        </elm>
                     </xsl:for-each>
                  </xsl:variable>
                  <xsl:copy-of select="$temp2/elm[1]/last"/>
               </title>

               <!-- for 'note' element -->
               <xsl:for-each select="current-group()[note/text()][1]">
                  <xsl:copy-of select="note"/>
               </xsl:for-each>

               <!-- for 'quantity' element -->
               <xsl:for-each select="current-group()[quantity/text()][1]">
                  <xsl:copy-of select="quantity"/>
               </xsl:for-each>

            </item-group>
         </xsl:for-each-group>
      </shiporder>
   </xsl:template>
</xsl:stylesheet>

代码工作正常,但最糟糕的是,我无法使其泛化。对于项目下的每个元素,我需要编写特定于特定元素的 xpath 的 xsl 代码。我什至无法根据用户定义的模板编写代码。

我发现标题“在 XSLT 中递归地组合相同的同级元素< /a>”。但我无法根据我的要求定制它。

我在每个项目下都有数百个这样的子元素或后代元素,并且每个元素的深度是任意的。编写对应于每个子代或后代的 xsl 代码是非常可笑的。任何专家都可以指导我编写通用 xsl 代码(无论元素数量及其深度如何)或优化现有代码吗?

提前致谢,
凯瑟尔

I want to merge items with same id into single item. The resultant item needs to have first non-empty element from the item group. If there is no non-empty element in the item group then the element itself ignored from the item group and should not present in the resultant item.

Sample XML Input:

<?xml version="1.0" encoding="UTF-8" ?>
<shiporder orderid="orderid106">
   <orderperson id="123">orderperson107</orderperson>
   <shipto>
      <name>name108</name>
      <address>address109</address>
      <city>city110</city>
      <country>country111</country>
   </shipto>

   <!--Item Group 100-->
   <item>
      <id>100</id>
      <title>
         <first>
            <a></a>
         </first>
         <last>item100_lastTitle1</last>
      </title>
      <note></note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a>a_100_2</a>
         </first>
         <last>item100_lastTitle2</last>
      </title>
      <note>note100_2</note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a att1="abc" att2='cde'>a_100_3</a>
         </first>
         <last id="1" attr="2">item100_lastTitle3</last>
      </title>
      <note>note100_3</note>
      <quantity>1</quantity>
   </item>

   <!--Item Group 101-->
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_1</a>
         </first>
         <last></last>
      </title>
      <note>note101_1</note>
      <quantity>10</quantity>
   </item>
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_2</a>
         </first>
         <last>item101_lastTitle2</last>
      </title>
      <note>note101_2</note>
      <quantity>5</quantity>
   </item>

   <!--Item Group 103-->
   <item>
      <id>103</id>
      <title>
         <first>
            <a>a_103_2</a>
         </first>
         <last>item103_lastTitle2</last>
      </title>
      <note>note103_1</note>
      <quantity></quantity>
   </item>
</shiporder>

Sample XML Output:

<?xml version = '1.0' encoding = 'UTF-8'?>
<shiporder orderid="orderid106">
  <item>
    <id>100</id>
    <title>
      <first>
        <a>a_100_2</a>
      </first>
      <last>item100_lastTitle1</last>
    </title>
    <note>note100_2</note>
    <quantity>6</quantity>
  </item>

  <item>
    <id>101</id>
    <title>
      <first>
        <a>a_101_1</a>
      </first>
      <last>item101_lastTitle2</last>
    </title>
    <note>note101_1</note>
    <quantity>10</quantity>
  </item>

  <item>
    <id>103</id>
    <title>
      <first>
        <a>a_103_2</a>
      </first>
      <last>item103_lastTitle2</last>
    </title>
    <note>note103_1</note>
  </item>
</shiporder>

I tried with the following XSL code to get the above output:

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/shiporder">
      <shiporder>
         <xsl:if test="@orderid">
            <xsl:attribute name="orderid">
               <xsl:value-of select="@orderid"/>
            </xsl:attribute>
         </xsl:if>

         <!-- Grouping each item based on its ID element value-->
         <xsl:for-each-group select="item" group-by="id">
            <item-group>
               <!-- For Each group search for first non empty child element-->
               <xsl:for-each select="current-group()[id/text()][1]">
                  <xsl:copy-of select="id"/>
               </xsl:for-each>
               <title>
                  <first>
                     <!-- for 'a' element -->
                     <xsl:variable name="temp1">
                        <xsl:for-each select="current-group()/title/first[a/text()][1]">
                           <elm>
                              <xsl:copy-of select="a"/>
                           </elm>
                        </xsl:for-each>
                     </xsl:variable>
                     <xsl:copy-of select="$temp1/elm[1]/a"/>
                  </first>

                  <!-- for 'last' element -->
                  <xsl:variable name="temp2">
                     <xsl:for-each select="current-group()/title[last/text()][1]">
                        <elm>
                           <xsl:copy-of select="last"/>
                        </elm>
                     </xsl:for-each>
                  </xsl:variable>
                  <xsl:copy-of select="$temp2/elm[1]/last"/>
               </title>

               <!-- for 'note' element -->
               <xsl:for-each select="current-group()[note/text()][1]">
                  <xsl:copy-of select="note"/>
               </xsl:for-each>

               <!-- for 'quantity' element -->
               <xsl:for-each select="current-group()[quantity/text()][1]">
                  <xsl:copy-of select="quantity"/>
               </xsl:for-each>

            </item-group>
         </xsl:for-each-group>
      </shiporder>
   </xsl:template>
</xsl:stylesheet>

The code works fine But the worst part is, I couldn’t make it generalize. For each element under the item I need to write a xsl code specific to the xpath of particular element. I couldn’t even write code in terms of user-defined-templates.

I found already answered question in the title of "Recursively combine identical sibling elements in XSLT". But I'm not able to customize it according to my requirements.

I'm having hundreds of such child or decedent element under each item and the depth of each one is arbitrary. It is very ridiculous to write xsl code corresponds to each of those child or descendants. Can any of the experts guide me to write generic xsl code (irrespective of number of elements and also its depth) or optimize the existing code?

Thanks in advance,

Kathir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-11-13 07:24:21

这实际上是一个 XSLT 1.0 解决方案(我手头没有 XSLT2 处理器),但它使用通用的“组合”命名模板,该模板应该可以满足您的需要。

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="item" use="id" match="item" />

  <xsl:template match="item[generate-id() = generate-id(key('item',id)[1])]">
    <xsl:call-template name="combine">
      <xsl:with-param name="list" select="../*[id = current()/id]" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="item" />

  <xsl:template name="combine">
    <xsl:param name="list" />
    <xsl:element name="{name($list[1])}">
      <xsl:for-each select="*[not(preceding-sibling::*[name() = name(current())])]" >
        <xsl:call-template name="combine">
          <xsl:with-param name="list" select="$list/*[name() = name(current())]" />
        </xsl:call-template>
      </xsl:for-each>
      <xsl:value-of select="$list[text()][1]/text()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

combine 模板采用节点列表,并递归地将它们组合成一个,并采用每个节点的第一个可用值。该模板也不组合属性,但由于您提供的项目 XML 中没有任何属性,因此应该没问题。模板中间的 使用 XSLT1 技术来挑选唯一的元素名称;它只选择那些没有同名兄弟姐妹的人。可能有一种方法可以使用 XSLT2 中的 for-each-group 来做到这一点,但我无法在这里检查。

This is actually an XSLT 1.0 solution (I don't have an XSLT2 processor to hand), but it uses a generic 'combine' named template that should do what you need.

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="item" use="id" match="item" />

  <xsl:template match="item[generate-id() = generate-id(key('item',id)[1])]">
    <xsl:call-template name="combine">
      <xsl:with-param name="list" select="../*[id = current()/id]" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="item" />

  <xsl:template name="combine">
    <xsl:param name="list" />
    <xsl:element name="{name($list[1])}">
      <xsl:for-each select="*[not(preceding-sibling::*[name() = name(current())])]" >
        <xsl:call-template name="combine">
          <xsl:with-param name="list" select="$list/*[name() = name(current())]" />
        </xsl:call-template>
      </xsl:for-each>
      <xsl:value-of select="$list[text()][1]/text()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The combine template takes a list of nodes, and combines them into one recursively, taking the first available value for each node. This template does NOT combine attributes as well, but as you don't have any in the item XML you provided, this should be OK. The <xsl:for-each in the middle of the template uses an XSLT1 technique for picking out unique element names; it only selects those that don't have a previous sibling with the same name. There's probably a way of doing this using for-each-group in XSLT2, but I can't check that here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文