我有一个示例XML,必须循环3次才能获得输出。但有时候,其中一个循环 (attrQualMany name =" pagecontain")可以是可选的,这会在这里产生问题。
示例XML
<document>
<item>
<ID>1000909090</ID>
<flex>
<attrGroupMany name="pageinfo">
<row>
<attrQualMany name="pageinput">
<value qual="en">User Intake</value>
</attrQualMany>
<attrGroupMany name="pagetype"> <!-- Mandatory Loop -->
<row>
<attr name="pagemeasure">EXACT</attr>
<attrQualMany name="pagecontain"> <!-- Optional Loop -->
<value qual="GR1">20</value>
<value qual="GR2">21</value>
</attrQualMany>
</row>
<row>
<attr name="pagemeasure">EXACT1</attr>
<attrQualMany name="pagecontain"><!-- Optional Loop -->
<value qual="JH1">30</value>
<value qual="JH2">31</value>
</attrQualMany>
</row>
</attrGroupMany>
<attr name="pagestate">PREPARED</attr>
<attrQualMany name="pagewidth"> <!-- Mandatory Loop -->
<value qual="OZ">10</value>
<value qual="AB">11</value>
</attrQualMany>
</row>
</attrGroupMany>
</flex>
</item>
</document>
我在XSLT下使用,当我们存在所有循环时,它正常工作。
XSLT
<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:variable name="value1" select="./@qual"/>
<xsl:for-each select="$pagewidth">
<RelatedItem referenceKey="{concat('PAGEDETAILSINFO','-',ancestor::item/ID,'-',../../attr[@name='pagestate'], '-', . ,'-', @qual, '-', $attr , '-',$value, '-', $value1)}"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
</xsl:template>
</xsl:stylesheet>
如果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>
</row>
<row>
<attr name="pagemeasure">EXACT1</attr>
</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>
这种情况下的预期输出是
<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>PAGEDETAILSINFO</RelationType>
<RelatedItems count="8">
<RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT--"/>
<RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT--"/>
<RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-10-OZ-EXACT1--"/>
<RelatedItem referenceKey="PAGEDETAILSINFO-1000909090-PREPARED-11-AB-EXACT1--"/>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
答案 0 :(得分:2)
请考虑将与您当前的for-loop
变量匹配的$pagewidth
改为模板。
<xsl:template match="attrQualMany[@name = 'pagewidth']/value">
<xsl:param name="pagetype" />
<xsl:param name="pagecontain" />
<xsl:param name="pagecontainqual" />
<RelatedItem referenceKey="{concat('PAGEDETAILSINFO','-',ancestor::item/ID,'-',../../attr[@name='pagestate'], '-', . ,'-', @qual, '-', $pagetype , '-', $pagecontain, '-', $pagecontainqual)}"/>
</xsl:template>
然后,您可以xsl:choose
确定是否有pagecontain
值
<xsl:variable name="pagecontain" select=".//attrQualMany[@name='pagecontain']/value"/>
<xsl:choose>
<xsl:when test="$pagecontain">
如果条件为真,那么您可以调用新模板而不是嵌套xsl:for-each
<xsl:for-each select="$pagecontain">
<xsl:apply-templates select="$pagewidth">
<xsl:with-param name="pagetype" select="$attr" />
<xsl:with-param name="pagecontain" select="." />
<xsl:with-param name="pagecontainqual" select="@qual" />
</xsl:apply-templates>
否则,您只需在没有xsl:for-each
<xsl:apply-templates select="$pagewidth">
<xsl:with-param name="pagetype" select="$attr" />
</xsl:apply-templates>
试试这个XSLT
<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:variable name="pagecontain" select=".//attrQualMany[@name='pagecontain']/value"/>
<xsl:choose>
<xsl:when test="$pagecontain">
<xsl:for-each select="$pagecontain">
<xsl:apply-templates select="$pagewidth">
<xsl:with-param name="pagetype" select="$attr" />
<xsl:with-param name="pagecontain" select="." />
<xsl:with-param name="pagecontainqual" select="@qual" />
</xsl:apply-templates>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$pagewidth">
<xsl:with-param name="pagetype" select="$attr" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
</xsl:template>
<xsl:template match="attrQualMany[@name = 'pagewidth']/value">
<xsl:param name="pagetype" />
<xsl:param name="pagecontain" />
<xsl:param name="pagecontainqual" />
<RelatedItem referenceKey="{concat('PAGEDETAILSINFO','-',ancestor::item/ID,'-',../../attr[@name='pagestate'], '-', . ,'-', @qual, '-', $pagetype , '-', $pagecontain, '-', $pagecontainqual)}"/>
</xsl:template>
</xsl:stylesheet>