2016-10-02 29 views
0

INPUT XML基於對應值在物體上應使用XSLT採取

<root> 
     <file1> 
      <commodity> 
       <units>1</units> 
       <obj>mango</obj> 
      </commodity> 
      <commodity> 
       <units>5</units> 
       <obj>guava</obj> 
      </commodity> 
     </file1> 

     <file2> 
      <category> 
       <object>guava</object> 
       <type>CAT1</type> 
       <colour>green</colour> 
      </category> 
      <category> 
       <object>mango</object> 
       <type>CAT2</type> 
       <colour>yellow</colour> 
      </category> 
     </file2> 
    </root> 

我需要obj的值在根據根file2的file1和對象進行比較,如果相同我需要採取他們相應的單位,類型和顏色,並使用xslt產生以下輸出。

輸出XML

<output> 
     <com> 
      <name>guava</name> 
      <num>5</num> 
      <category>CAT1</category> 
      <col>green</col> 
     </com> 
     <com> 
      <name>mango</name> 
      <num>1</num> 
      <category>CAT2</category> 
      <col>yellow</col> 
     </com> 
    </output> 

我想下面的XSLT,但並不如預期的響應。它沒有正確循環。你能告訴我我哪裏出錯了嗎?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" 
    indent="yes" /> 
    <xsl:key name="object-search" match="root/file1/commodity" use="obj" /> 
    <xsl:template match="/"> 
    <output> 
     <xsl:for-each select="key('object-search', //category/object)"> 
      <com> 

       <name> 
        <xsl:value-of select="obj" /> 
       </name> 
       <num> 
        <xsl:value-of select="units" /> 
       </num> 
       <category> 
        <xsl:value-of 
         select="//root/file2/category/type" /> 
       </category> 
       <col> 
        <xsl:value-of 
         select="//root/file2/category/colour" /> 
       </col> 

      </com> 
     </xsl:for-each> 
    </output> 
    </xsl:template> 
    </xsl:stylesheet> 

回答

0

試試這樣說:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:key name="cat" match="category" use="object" /> 

<xsl:template match="/root"> 
    <output> 
     <xsl:for-each select="file1/commodity"> 
      <com> 
       <name> 
        <xsl:value-of select="obj" /> 
       </name> 
       <num> 
        <xsl:value-of select="units" /> 
       </num> 
       <xsl:variable name="cat" select="key('cat', obj)" /> 
       <category> 
        <xsl:value-of select="$cat/type" /> 
       </category> 
       <col> 
        <xsl:value-of select="$cat/colour" /> 
       </col> 
      </com> 
     </xsl:for-each> 
    </output> 
</xsl:template> 

</xsl:stylesheet> 

注意,結果是你發佈什麼稍有不同:

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <com> 
     <name>mango</name> 
     <num>1</num> 
     <category>CAT2</category> 
     <col>yellow</col> 
    </com> 
    <com> 
     <name>guava</name> 
     <num>5</num> 
     <category>CAT1</category> 
     <col>green</col> 
    </com> 
</output> 

或者,你可以這樣做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:key name="com" match="commodity" use="obj" /> 

<xsl:template match="/root"> 
    <output> 
     <xsl:for-each select="file2/category"> 
      <xsl:variable name="com" select="key('com', object)" /> 
      <com> 
       <name> 
        <xsl:value-of select="$com/obj" /> 
       </name> 
       <num> 
        <xsl:value-of select="$com/units" /> 
       </num> 
       <category> 
        <xsl:value-of select="type" /> 
       </category> 
       <col> 
        <xsl:value-of select="colour" /> 
       </col> 
      </com> 
     </xsl:for-each> 
    </output> 
</xsl:template> 

</xsl:stylesheet> 

,並得到:

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <com> 
     <name>guava</name> 
     <num>5</num> 
     <category>CAT1</category> 
     <col>green</col> 
    </com> 
    <com> 
     <name>mango</name> 
     <num>1</num> 
     <category>CAT2</category> 
     <col>yellow</col> 
    </com> 
</output>