2010-04-13 64 views
0

我有一個XML文件,如下所示,我想用xslt進行轉換。在一個序列中獲得另一個序列元素的值

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <s1 name="kfb" /> 
    <s1 name="kfb" /> 
    <s1 name="kfb" /> 
    <s1 name="kfb" /> 
    <s1 name="kfb" /> 
    <s1 name="kfb" /> 
    <summary> 
     <r1 value="1" /> 
     <r1 value="5" /> 
     <r1 value="c" /> 
     <r1 value="h" /> 
     <r1 value="3" /> 
     <r1 value="1" /> 
    </summary> 
</root> 

我想要實現的是:別當了,每個「S1」的元素,我想只是根據指數/現在的位置,以獲得相應的「R1」的‘價值’attbute值(」 s1「元素計數等於」r1「)。我寫下如下的xslt,但它不起作用,任何人都可以提供幫助嗎?謝謝。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template mode="getr1" match="summary" > 
     <xsl:param name="index"/> 
     <xsl:value-of select="r1[$index][@value]"/> 
    </xsl:template> 

    <xsl:template match="/"> 
     <html> 
      <body> 
       <ul> 
       <xsl:for-each select="root/s1"> 
        <xsl:variable name="i" select="position()"/> 
        <li> 
         <xsl:value-of select ="@name"/> 
         : 
         <!--<xsl:apply-templates mode="getr1" select="/root/summary"> 
          <xsl:with-param name="index" select="$i" /> 
         </xsl:apply-templates>--> 
         <!--I want to get the corresponding r1's value according to the index --> 
         <!-- but above code is not work.--> 
        </li> 
       </xsl:for-each> 
       </ul> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

如果有的話,這將是'R1 [$指數]/@值',而不是'r1 [$ index] [@ value]'。 – Tomalak 2010-04-13 17:11:31

回答

0
<xsl:template match="root"> 
    <html> 
    <body> 
     <ul> 
     <xsl:apply-templates select="s1" /> 
     </ul> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="s1"> 
    <!-- remember current position --> 
    <xsl:variable name="myPos" select="position()" /> 
    <li> 
    <xsl:text>Name: </xsl:text> 
    <xsl:value-of select="@name"/> 

    <xsl:text>Value: </xsl:text> 
    <!-- use current position to pull out the correct <r1> --> 
    <xsl:value-of select="following-sibling::summary[1]/r1[$myPos]/@value"/> 

    <!-- if there is only ever one <summary> in your XML, you can also do --> 
    <xsl:value-of select="/root/summary/r1[$myPos]/@value"/> 
    </li> 
</xsl:template> 
+0

非常感謝您的支持。 – Leonard 2010-04-14 01:40:45

0

問題與getr1模板

<xsl:value-of select="r1[$index][@value]"/>訪問值屬性,但應該是<xsl:value-of select="r1[$index]/@value"/>

相關問題