我有一条示例消息,其中我需要在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,其中删除了父级的副本,即温度信息,但不删除子级温度内的副本
预期输出
<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>
任何人都可以指导我出错的地方。
答案 0 :(得分:1)
除了删除重复的“temperatureInformation”元素之外,对于每个这样的不同元素,您看起来想要为该组中的所有元素合并不同的“temperatureStats”元素。
由于这是第二级分组,因此关键是需要进入第一级分组,所以看起来像这样:
<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" />
然后,您可以使用第二个键
在其中选择不同的“temperatureStats”元素 <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>