使用多个循环删除重复项

使用多个循环删除重复项

本文介绍了XSLT 使用多个循环删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 XSLT 转换以下 XML.

I have to convert the below XML using the XSLT.

输入 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= "pagetype" 内循环,并在 attrQualMany ="pagecontain" 内循环,然后在 attrQualMany= 内循环页面宽度".所以它变成了 2*2*2 次循环,也就是 8 次.

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.

输出应该是concat的

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   )"/>

预期输出应该是

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

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

请帮忙删除重复项

推荐答案

你可以使用这个:

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

这篇关于XSLT 使用多个循环删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:13