2010-04-13 4 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 [$ index] [@ value]'가 아니라'value '이다. – Tomalak

답변

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

귀하의 코 런트에 많은 도움을 주신 vefy에게 감사드립니다. – Leonard

0

문제는 '것, R1 [$ 지수]/@

당신이 <xsl:value-of select="r1[$index][@value]"/>과 value 속성에 액세스 getr1 템플릿으로하지만 <xsl:value-of select="r1[$index]/@value"/>

관련 문제