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>
Пояснение : Muenchian группировка на две клавиши.
II.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-документу дает тот же требуемый, правильный результат .
Пояснение : Использование инструкции <xsl:for-each-group>
XSLT 2.0