[英]Match and Merge in XSLT 1.0 for multiple nodes
下面的XSLT可以正常工作,當我們有單個節點“ pos”時,刪除重復的節點,但是當我們有多個“ pos”節點時,則不起作用。
節點“ pos”內的數據必須獨立處理,以刪除其他“ pos”節點的數據重復項。 節點“ pos”內的數據彼此獨立。 使用下面的XSLT時,它將合並兩個“ pos”節點的數據並刪除重復項。 任何輸入都會有很大的幫助
樣本消息是
<document>
<party>
<gtin>1000909090</gtin>
<pos>
<attrGroupMany name="temperatureInformation">
<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">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">CODE3</attr>
</row>
<row>
<attr name="StatsCode">CODE4</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>
<pos>
<attrGroupMany name="temperatureInformation">
<row>
<attr name="temperatureCode">STORAGE1</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">CODE3</attr>
</row>
<row>
<attr name="StatsCode">CODE4</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>
</document>
我在這里使用的XSLT是
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="group" match="attrGroupMany[@name = 'temperatureInformation']/row"
use="concat(attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature'])"/>
<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('group', concat(attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature']))[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attrGroupMany[@name = 'temperatureStats']">
<xsl:copy>
<xsl:apply-templates select="@* | key('group', concat(../attr[@name = 'temperatureCode'], '|', ../attrQualMany[@name = 'temperature']))/attrGroupMany[@name = 'temperatureStats']/row"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
預期的輸出是
<?xml version="1.0" encoding="UTF-8"?>
<document>
<party>
<gtin>1000909090</gtin>
<pos>
<attrGroupMany name="temperatureInformation">
<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">CODE2</attr>
</row>
<row>
<attr name="StatsCode">CODE3</attr>
</row>
<row>
<attr name="StatsCode">CODE4</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>
<pos>
<attrGroupMany name="temperatureInformation">
<row>
<attr name="temperatureCode">STORAGE1</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>
<row>
<attr name="StatsCode">CODE4</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>
</document>
任何輸入都是非常有價值的,因為此解決方案是更大問題陳述的組成部分。
XSLT下面的工作是刪除節點內的重復項。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="group" match="party/pos/attrGroupMany[@name = 'temperatureInformation']/row"
use="concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature'])"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="pos/attrGroupMany[@name = 'temperatureInformation']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="row[generate-id() = generate-id(key('group', concat(generate-id(ancestor::pos), '|', attr[@name = 'temperatureCode'], '|', attrQualMany[@name = 'temperature']))[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="pos/attrGroupMany[@name = 'temperatureInformation']/row/attrGroupMany[@name = 'temperatureStats']">
<xsl:copy>
<xsl:apply-templates select="@* | key('group', concat(generate-id(ancestor::pos), '|',../attr[@name = 'temperatureCode'], '|', ../attrQualMany[@name = 'temperature']))/attrGroupMany[@name = 'temperatureStats']/row"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.