具有多个循环的XSLT转换

时间:2014-12-19 07:06:51

标签: 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="GR">12</value>
                        </attrQualMany>
                    </row>
                    <row>
                        <attr name="pagemeasure">EXACT</attr>
                        <attrQualMany name="pagecontain">
                            <value qual="JH">13</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; pagewidth&#34;。所以它变成2 * 2次循环,这是4次。

输出应该是

的连续性
<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="4">
            <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-12-GR" />
            <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT-13-JH" />
            <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-12-GR" />
            <RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT-13-JH" />                
        </RelatedItems>
    </Relationship>
</RelationshipData>
</CatalogItem>

我无法在我的XSLT内部和外部循环。我在XSLT下面使用。

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>


<xsl:template match="document"> 
    <CatalogItem>
        <RelationshipData>              
            <Relationship>
                <RelationType>PAGEDETAILSINFO</RelationType>
                <RelatedItems>
                    <xsl:attribute name="count">
                        <xsl:value-of select="count(attrQualMany[@name ='pagewidth']/value/@qual)"/>
                    </xsl:attribute>
                    <xsl:for-each select="flex//attrGroupMany[@name ='pagetype']/row">
                        <RelatedItem>
                            <xsl:attribute name="referenceKey">
                                <xsl:value-of select="concat('PAGEDETAILSINFO','-',ancestor::item/id,../../attr[@name='pagestate'], '-', pagewidthValue ,'-', pagewidthuom,  '-',  attr[@name='pagemeasure'] ,  '-',pagecontainValue,  '-',  pagecontainUOM   )"/> 

                            </xsl:attribute>
                        </RelatedItem>
                    </xsl:for-each>
                </RelatedItems>
            </Relationship>             
        </RelationshipData>
    </CatalogItem>
</xsl:template> 
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

使用一些变量很容易解决这个问题。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent="yes" />
  <xsl:template match="document/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="pagewidth" select="flex//attrQualMany[@name = 'pagewidth']/value" />
    <CatalogItem>
      <RelationshipData>
        <Relationship>
          <RelationType>PAGEDETAILSINFO</RelationType>
          <RelatedItems count="{count($pagetype) * count($pagewidth)}">
            <xsl:for-each select="$pagetype">
              <xsl:variable name="t" 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']}-{$t//value}-{$t//value/@qual}" />
              </xsl:for-each>
            </xsl:for-each>
          </RelatedItems>
        </Relationship>
      </RelationshipData>
    </CatalogItem>
  </xsl:template>
</xsl:stylesheet>