如何在java中使用xslt从xml中使用group-by进行选择

发布于 2024-10-28 04:52:12 字数 1356 浏览 2 评论 0原文

我有这个xml文件,

<SearchEngine>
  <XV2_2284_425_1_1>
    <RowNumber>1</RowNumber>
    <ID>104</ID>
    <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
    <Discipline>Arch</Discipline>
    <DocType>Doc1</DocType>
  </XV2_2284_425_1_1>
  <XV2_2284_425_1_3>
    <RowNumber>2</RowNumber>
    <ID>106</ID>
    <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc3</DocType>
  </XV2_2284_425_1_3>
  <XV2_1234_425_1_1>
    <RowNumber>3</RowNumber>
    <ID>105</ID>
    <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc2</DocType>
  </XV2_1234_425_1_1>
  <XV2_1234_425_2_1>
    <RowNumber>4</RowNumber>
    <ID>107</ID>
    <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc3</DocType>
  </XV2_1234_425_2_1>
</SearchEngine>

我试图获取按Discipline和DocType分组的所有Reference_x0020_ID(对于Discipline和DocType的所有值) 我尝试使用 XSLT 但没有运气

任何帮助将不胜感激

谢谢

I have this xml file

<SearchEngine>
  <XV2_2284_425_1_1>
    <RowNumber>1</RowNumber>
    <ID>104</ID>
    <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
    <Discipline>Arch</Discipline>
    <DocType>Doc1</DocType>
  </XV2_2284_425_1_1>
  <XV2_2284_425_1_3>
    <RowNumber>2</RowNumber>
    <ID>106</ID>
    <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc3</DocType>
  </XV2_2284_425_1_3>
  <XV2_1234_425_1_1>
    <RowNumber>3</RowNumber>
    <ID>105</ID>
    <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc2</DocType>
  </XV2_1234_425_1_1>
  <XV2_1234_425_2_1>
    <RowNumber>4</RowNumber>
    <ID>107</ID>
    <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
    <Discipline>Structural</Discipline>
    <DocType>Doc3</DocType>
  </XV2_1234_425_2_1>
</SearchEngine>

I am trying to get all Reference_x0020_ID grouped by Discipline and DocType (for all values of both Discipline and DocType)
I tried using XSLT but no luck

Any help would be appreciated

Thanks

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

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

发布评论

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

评论(1

很快妥协 2024-11-04 04:52:12

I. XSLT 1.0

这是一个 XSLT 1.0 解决方案

<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="kRefByDiscAndDType" match="Reference_x0020_ID"
  use="concat(../Discipline, '+', ../DocType)"/>
    
 <xsl:template match=
  "Reference_x0020_ID
         [generate-id()
         =
          generate-id(key('kRefByDiscAndDType',
                          concat(../Discipline, '+', ../DocType)
                          )[1]
                      )
         ]
  ">
     <group>
      <xsl:copy-of select="../Discipline | ../DocType"/>
      <xsl:copy-of select=
         "key('kRefByDiscAndDType',
              concat(../Discipline, '+', ../DocType)
             )
         "/>
     </group>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<SearchEngine>
    <XV2_2284_425_1_1>
        <RowNumber>1</RowNumber>
        <ID>104</ID>
        <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
        <Discipline>Arch</Discipline>
        <DocType>Doc1</DocType>
    </XV2_2284_425_1_1>
    <XV2_2284_425_1_3>
        <RowNumber>2</RowNumber>
        <ID>106</ID>
        <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc3</DocType>
    </XV2_2284_425_1_3>
    <XV2_1234_425_1_1>
        <RowNumber>3</RowNumber>
        <ID>105</ID>
        <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc2</DocType>
    </XV2_1234_425_1_1>
    <XV2_1234_425_2_1>
        <RowNumber>4</RowNumber>
        <ID>107</ID>
        <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc3</DocType>
    </XV2_1234_425_2_1>
</SearchEngine>

产生所需的正确结果: >

<group>
   <Discipline>Arch</Discipline>
   <DocType>Doc1</DocType>
   <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
</group>
<group>
   <Discipline>Structural</Discipline>
   <DocType>Doc3</DocType>
   <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
   <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
</group>
<group>
   <Discipline>Structural</Discipline>
   <DocType>Doc2</DocType>
   <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
</group>

说明慕尼黑分组< /strong> 两个键。

二. XSLT 2.0

此转换

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
 <xsl:template match="/">
     <xsl:for-each-group select="/*/*/Reference_x0020_ID"
          group-by="concat(../Discipline, '+', ../DocType)">
      <group>
       <xsl:copy-of select="../Discipline | ../DocType"/>
       <xsl:copy-of select="current-group()"/>
      </group>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

应用于同一 XML 文档时会产生相同的所需正确结果

说明 XSLT 2.0 指令的使用

I. XSLT 1.0

Here is an XSLT 1.0 solution:

<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="kRefByDiscAndDType" match="Reference_x0020_ID"
  use="concat(../Discipline, '+', ../DocType)"/>
    
 <xsl:template match=
  "Reference_x0020_ID
         [generate-id()
         =
          generate-id(key('kRefByDiscAndDType',
                          concat(../Discipline, '+', ../DocType)
                          )[1]
                      )
         ]
  ">
     <group>
      <xsl:copy-of select="../Discipline | ../DocType"/>
      <xsl:copy-of select=
         "key('kRefByDiscAndDType',
              concat(../Discipline, '+', ../DocType)
             )
         "/>
     </group>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<SearchEngine>
    <XV2_2284_425_1_1>
        <RowNumber>1</RowNumber>
        <ID>104</ID>
        <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
        <Discipline>Arch</Discipline>
        <DocType>Doc1</DocType>
    </XV2_2284_425_1_1>
    <XV2_2284_425_1_3>
        <RowNumber>2</RowNumber>
        <ID>106</ID>
        <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc3</DocType>
    </XV2_2284_425_1_3>
    <XV2_1234_425_1_1>
        <RowNumber>3</RowNumber>
        <ID>105</ID>
        <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc2</DocType>
    </XV2_1234_425_1_1>
    <XV2_1234_425_2_1>
        <RowNumber>4</RowNumber>
        <ID>107</ID>
        <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
        <Discipline>Structural</Discipline>
        <DocType>Doc3</DocType>
    </XV2_1234_425_2_1>
</SearchEngine>

the wanted, correct result is produced:

<group>
   <Discipline>Arch</Discipline>
   <DocType>Doc1</DocType>
   <Reference_x0020_ID>X_0000000017</Reference_x0020_ID>
</group>
<group>
   <Discipline>Structural</Discipline>
   <DocType>Doc3</DocType>
   <Reference_x0020_ID>X_0000000020</Reference_x0020_ID>
   <Reference_x0020_ID>X_0000000019</Reference_x0020_ID>
</group>
<group>
   <Discipline>Structural</Discipline>
   <DocType>Doc2</DocType>
   <Reference_x0020_ID>X_0000000018</Reference_x0020_ID>
</group>

Explanation: Muenchian grouping on two keys.

II. XSLT 2.0

This transformation:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
 <xsl:template match="/">
     <xsl:for-each-group select="/*/*/Reference_x0020_ID"
          group-by="concat(../Discipline, '+', ../DocType)">
      <group>
       <xsl:copy-of select="../Discipline | ../DocType"/>
       <xsl:copy-of select="current-group()"/>
      </group>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

when applied on the same XML document produces the same wanted, correct result.

Explanation: Use of the <xsl:for-each-group> XSLT 2.0 instruction

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