[英]How to select using group-by from xml using xslt in java
I have this xml file 我有这个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>
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 我试图让所有Reference_x0020_ID按照Discipline和DocType分组(对于Discipline和DocType的所有值)我尝试使用XSLT但没有运气
Any help would be appreciated 任何帮助,将不胜感激
Thanks 谢谢
I. XSLT 1.0 I. XSLT 1.0
Here is an XSLT 1.0 solution : 这是一个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>
when this transformation is applied on the provided XML document: 当此转换应用于提供的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>
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. 说明 : Muenchian分组两个键。
II. II。 XSLT 2.0
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 . 当应用于同一XML文档时,产生相同的想要的,正确的结果 。
Explanation : Use of the <xsl:for-each-group>
XSLT 2.0 instruction 说明 :使用
<xsl:for-each-group>
XSLT 2.0指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.