开发者

How to select using group-by from xml using xslt in java

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</Referenc开发者_JAVA技巧e_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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜