我有一个示例消息,其中我需要删除temperatureInformation和temperatureStats块中的重复项。
示例消息是
<document>
<body>
<party>
<gtin>1000909090</gtin>
<pos>
<attrGroupMany name="temperatureInformation">
<row>
<gtin>1000909090</gtin>
<attr name="temperatureCode">STORAGE</attr>
<attrQualMany name="temperature">
<value qual="FAH">10</value>
<value qual="CC">20</value>
</attrQualMany>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE1</attr>
</row>
<row>
<attr name="StatsCode">CODE2</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="temperatureCode">STORAGE</attr>
<attrQualMany name="temperature">
<value qual="FAH">10</value>
<value qual="CC">20</value>
</attrQualMany>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE1</attr>
</row>
<row>
<attr name="StatsCode">CODE3</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="temperatureCode">HANDLING</attr>
<attrQualMany name="temperature">
<value qual="FAH">10</value>
</attrQualMany>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE5</attr>
</row>
<row>
<attr name="StatsCode">CODE6</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="temperatureCode">HANDLING</attr>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE7</attr>
</row>
<row>
<attr name="StatsCode">CODE8</attr>
</row>
</attrGroupMany>
</row>
</attrGroupMany>
</pos>
</party>
</body>
</document>
我使用的是下面的XSLT,其中删除了父项的重复,即temperatureInformation,但没有删除子项temperatureStats中的重复
预期输出为
<document>
<body>
<party>
<gtin>1000909090</gtin>
<pos>
<attrGroupMany name="temperatureInformation">
<row>
<gtin>1000909090</gtin>
<attr name="temperatureCode">STORAGE</attr>
<attrQualMany name="temperature">
<value qual="FAH">10</value>
<value qual="CC">20</value>
</attrQualMany>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE1</attr>
</row>
<row>
<attr name="StatsCode">CODE2</attr>
</row>
<row>
<attr name="StatsCode">CODE3</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="temperatureCode">HANDLING</attr>
<attrQualMany name="temperature">
<value qual="FAH">10</value>
</attrQualMany>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE5</attr>
</row>
<row>
<attr name="StatsCode">CODE6</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="temperatureCode">HANDLING</attr>
<attrGroupMany name="temperatureStats">
<row>
<attr name="StatsCode">CODE7</attr>
</row>
<row>
<attr name="StatsCode">CODE8</attr>
</row>
</attrGroupMany>
</row>
</attrGroupMany>
</pos>
</party>
</body>
</document>
使用的XSLT是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="grouptemperatureInformation" match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row"
use="concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature'])"/>
<xsl:key name="grouptemperatureStats" match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row/attrGroupMany[@name = 'temperatureStats']/row"
use="concat(generate-id(ancestor::pos), '|', attr[@name = 'StatsCode'])"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attrGroupMany[@name = 'temperatureInformation']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="row[generate-id() = generate-id(key('grouptemperatureInformation', concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature']))[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attrGroupMany[@name = 'temperatureStats']">
<xsl:copy>
<xsl:apply-templates select="@* | key('grouptemperatureInformation', concat(generate-id(ancestor::pos), '|',../attr[@name = 'temperatureCode'], '|', ../attrQualMany[@name = 'temperature']))/attrGroupMany[@name = 'temperatureStats']/row"/>
<!-- <xsl:apply-templates select="@*"/>
<xsl:apply-templates select="row[generate-id() = generate-id(key('grouptemperatureStats', concat(generate-id(ancestor::pos), '|', attr[@name = 'StatsCode']))[1])]"/> -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
有人能告诉我哪里出了问题吗。
除了删除重复的"temperatureInformation"元素外,对于每个这样不同的元素,您似乎希望合并组中所有元素的不同"temperature Stats"元素。
由于这是第二级分组,因此关键也需要考虑第一级分组,所以看起来像这样:
<xsl:key name="grouptemperatureStats"
match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row/attrGroupMany[@name = 'temperatureStats']/row"
use="concat(generate-id(ancestor::pos), '|', ../../../attr[@name = 'temperatureCode'], '|', ../../../attrQualMany[@name = 'temperature'], '|', attr[@name = 'StatsCode'])"/>
要使用它,在与"temperatureStats"元素匹配的模板中,您需要首先选择当前父组中的所有元素
<xsl:variable name="group"
select="key('grouptemperatureInformation', concat(generate-id(ancestor::pos), '|', ../attr[@name = 'temperatureCode'], '|', ../attrQualMany[@name = 'temperature']))/attrGroupMany[@name='temperatureStats']/row" />
然后,您可以使用第二个键在其中选择不同的"温度统计"元素
<xsl:apply-templates select="@* | $group[generate-id() = generate-id(key('grouptemperatureStats', concat(generate-id(ancestor::pos), '|', ../../../attr[@name = 'temperatureCode'], '|', ../../../attrQualMany[@name = 'temperature'], '|', attr[@name = 'StatsCode']))[1])]"/>
尝试此XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="grouptemperatureInformation"
match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row"
use="concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature'])"/>
<xsl:key name="grouptemperatureStats"
match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row/attrGroupMany[@name = 'temperatureStats']/row"
use="concat(generate-id(ancestor::pos), '|', ../../../attr[@name = 'temperatureCode'], '|', ../../../attrQualMany[@name = 'temperature'], '|', attr[@name = 'StatsCode'])"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attrGroupMany[@name = 'temperatureInformation']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="row[generate-id() = generate-id(key('grouptemperatureInformation', concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature']))[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attrGroupMany[@name = 'temperatureStats']">
<xsl:variable name="group" select="key('grouptemperatureInformation', concat(generate-id(ancestor::pos), '|', ../attr[@name = 'temperatureCode'], '|', ../attrQualMany[@name = 'temperature']))/attrGroupMany[@name='temperatureStats']/row" />
<xsl:copy>
<xsl:apply-templates select="@* | $group[generate-id() = generate-id(key('grouptemperatureStats', concat(generate-id(ancestor::pos), '|', ../../../attr[@name = 'temperatureCode'], '|', ../../../attrQualMany[@name = 'temperature'], '|', attr[@name = 'StatsCode']))[1])]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>