XSLT删除具有多个循环的副本

时间:2014-12-19 12:00:00

标签: xml xslt xslt-1.0 xslt-2.0 xslt-grouping

我必须使用XSLT转换下面的XML。

输入XML

<document>
<item>
    <ID>1000909090</ID>
    <flex>
        <attrGroupMany name="pageinfo">
            <row>
                <attrQualMany name="pageinput">
                    <value qual="en">User Intake</value>
                </attrQualMany>
                <attrGroupMany name="pagetype">
                    <row>
                        <attr name="pagemeasure">EXACT</attr>
                        <attrQualMany name="pagecontain">
                            <value qual="GR1">20</value>
                            <value qual="GR2">21</value>
                        </attrQualMany>
                    </row>
                    <row>
                        <attr name="pagemeasure">EXACT</attr>
                        <attrQualMany name="pagecontain">
                            <value qual="JH1">30</value>
                            <value qual="JH2">31</value>
                        </attrQualMany>
                    </row>
                </attrGroupMany>
                <attr name="pagestate">PREPARED</attr>
                <attrQualMany name="pagewidth">
                    <value qual="OZ">10</value>
                    <value qual="AB">11</value>
                </attrQualMany>
            </row>
        </attrGroupMany>
    </flex>
</item>
</document>

XSLT应该在每个行的attrGroupMany = &#34; pagetype&#34; 内循环,以及在attrQualMany = &#34; pagecontain&#34; 然后在attrQualMany = &#34; pagewidth&#34; 内循环。所以它变成2 * 2 * 2次循环,这是8次。

输出应该是

的连续性
<xsl:value-of select="concat('PAGEDETAILSINFO','-',ancestor::item/id,../../attr[@name='pagestate'], '-', pagewidthValue ,'-', pagewidthuom,  '-',  attr[@name='pagemeasure'] ,  '-',pagecontainValue,  '-',  pagecontainUOM   )"/> 

预期输出应为

<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
  <Relationship>
     <RelationType>PAGEDETAILSINFO</RelationType>
     <RelatedItems count="8">
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-31-JH2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-31-JH2" />
     </RelatedItems>
  </Relationship>
</RelationshipData>
</CatalogItem>

但是我在输出中得到重复的行,而且计数正在增加一倍。

实际输出低于不正确。

<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
  <Relationship>
     <RelationType>PAGEDETAILSINFO</RelationType>
     <RelatedItems count="16">
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-31-JH2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-31-JH2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-20-GR1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-21-GR2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-30-JH1" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-31-JH2" />
        <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-31-JH2" />
     </RelatedItems>
  </Relationship>
</RelationshipData>
</CatalogItem>

这里使用的XSLT是

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output indent="yes" />
 <xsl:template match="document">
    <xsl:for-each select="item">
        <xsl:variable name="item" select="." />
        <xsl:variable name="pageinfo" select="flex//attrGroupMany[@name = 'pageinfo']/row" />
        <xsl:variable name="pagetype" select="flex//attrGroupMany[@name = 'pagetype']/row" />
        <xsl:variable name="pagecontain" select="$pagetype/attrQualMany[@name = 'pagecontain']/value" />  
        <xsl:variable name="pagewidth" select="flex//attrQualMany[@name = 'pagewidth']/value" />
        <CatalogItem>
            <RelationshipData>
                <Relationship>
                    <RelationType>PAGEDETAILSINFO</RelationType>
                    <RelatedItems count="{count($pagetype) * count($pagewidth)  * count($pagecontain) }">
                        <xsl:for-each select="$pagetype">
                            <xsl:variable name="t" select="." />
                            <xsl:for-each select="$pagecontain">
                                <xsl:variable name="p" select="." />
                                <xsl:for-each select="$pagewidth">
                                    <xsl:variable name="w" select="." />
                                    <RelatedItem referenceKey="PAGEDETAILSINFO-{$item/ID}-{$pageinfo/attr[@name='pagestate']}-{$w}-{$w/@qual}-{$t/attr[@name='pagemeasure']}-{$p}-{$p/@qual}" />
                                </xsl:for-each>
                            </xsl:for-each>
                        </xsl:for-each>
                    </RelatedItems>
                </Relationship>
            </RelationshipData>
        </CatalogItem>
    </xsl:for-each >
    </xsl:template>
 </xsl:stylesheet>

请帮助删除重复文件

1 个答案:

答案 0 :(得分:1)

您可以使用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/document">
    <CatalogItem>
        <RelationshipData>
            <Relationship>
                <RelationType>PAGEDETAILSINFO</RelationType>
                <xsl:variable name="pagetype" select="//attrGroupMany[@name = 'pagetype']/row"/>
                <xsl:variable name="pagecontain" select="$pagetype//attrQualMany[@name='pagecontain']"/>
                <xsl:variable name="pagewidth" select="//attrQualMany[@name = 'pagewidth']/value"/>
                <RelatedItems count="{count($pagetype) * count($pagecontain) * count($pagewidth)}">
                    <xsl:for-each select="$pagetype">
                        <xsl:variable name="attr" select="attr[@name='pagemeasure']"/>
                        <xsl:for-each select=".//attrQualMany[@name='pagecontain']/value">
                            <xsl:variable name="value" select="."/>
                            <xsl:for-each select="$pagewidth">
                                <RelatedItem referenceKey="{concat('PAGEDETAILSINFO','-',ancestor::item/ID,'-',../../attr[@name='pagestate'], '-', . ,'-', @qual, '-', $attr , '-',$value, '-', $value/@qual)}"/>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>
        </RelationshipData>
    </CatalogItem>
</xsl:template>
</xsl:stylesheet>