get another sequence elements'value in one sequence
I have an XML file as below, and I want to transform it with 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>
what I want to achieve is: when do the for-each of "s1" elements, I want to get the corresponding "r1"'s "value" attbute value just according the index/postion("s1" elements count equal "r1") . the xslt I wrote as below, but it does not work, can anyone give a help? thanks.
<?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>
<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>
the problem is with the getr1
template
you access the value attribute with <xsl:value-of select="r1[$index][@value]"/>
but is should be <xsl:value-of select="r1[$index]/@value"/>
精彩评论