2014-12-19 42 views
0

我必須使用XSLT轉換以下XML。帶多個循環的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="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 = 「網頁類型」 的每一行,以及循環內attrQualMany = 「頁寬」。所以它變成4倍的2 * 2次循環。

輸出應該是

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

CONCAT和預期輸出是

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

您需要將哪些分組進行分組?這似乎沒有必要? – Tomalak 2014-12-19 07:30:20

+0

好的,那麼我們怎樣才能不分組 – Victor 2014-12-19 07:43:57

+0

這不是問題。你需要*分組嗎? – Tomalak 2014-12-19 07:47:12

回答

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

謝謝。我在另一個問題中提出了另一個問題。你可以請幫忙嗎.. http://stackoverflow.com/questions/27565699/xslt-removal-of-duplicate-with-multiple-loops – Victor 2014-12-19 12:00:47