XSLT Removal of duplicate with multiple loops

妖精的绣舞 提交于 2019-12-14 03:26:50

问题


I have to convert the below XML using the XSLT.

Input XML is

<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>

The XSLT should be looped inside attrGroupMany= "pagetype" for each row as well as loop inside attrQualMany ="pagecontain" and then loop inside attrQualMany="pagewidth". so it becomes 2*2*2 times loops which is 8 times.

The output should be concat of

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

Expected output should be

<?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>

But I am getting duplicate rows in the output as well as count is getting double.

Actual output is below which is incorrect.

<?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 which is used here is

 <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>

Please help to remove duplciates


回答1:


You can use this:

<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>


来源:https://stackoverflow.com/questions/27565699/xslt-removal-of-duplicate-with-multiple-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!