【问题标题】:How to select using group-by from xml using xslt in java如何在java中使用xslt从xml中选择使用group-by
【发布时间】:2011-03-30 11:03:30
【问题描述】:

我有这个 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,但没有运气

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 您要求输出采用什么格式?
  • 好问题,+1。请参阅我对两种解决方案(XSLT 1.0 和 XSLT 2.0)的回答,它们既简短又高效。还提供了解释。

标签: java xml xslt xpath


【解决方案1】:

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

解释Muenchian grouping在两个键上。

二。 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 文档时,会产生同样想要的正确结果

说明:使用&lt;xsl:for-each-group&gt; XSLT 2.0 指令

【讨论】:

  • 有没有办法让它不区分大小写?例如,如果我有“结构”和“结构”,我希望它们相等。
  • @Joe:当然。只需将所有出现的 $string 替换为 translate($string, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
猜你喜欢
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 2023-04-03
相关资源
最近更新 更多