具有多个循环的 XSLT 转换



我必须使用 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= "pagetype" 中循环,并在 attrQualMany="pagewidth" 中循环。 所以它变成了 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>

这个问题通过使用几个变量很容易解决。

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

相关内容

  • 没有找到相关文章

最新更新