c# - Cannot re-assign a value to XSLT variable, or can I exit from for-each loop conditionally -
cannot re-assign value xslt variable, or can exit from
<xsl:for-each
conditionally
xml
<r> <a id="a"> <s id="a111">1</s> <s id="a222">2</s> <s id="a333">3 <s id="a3331">3.1</s> <s id="a3332">3.2</s> <s id="a3333">3.3</s> </s> <s id="a444">4</s> <s id="a555">5</s> <s id="a666">6</s> </a> <a id="x"> <s id="x111">1</s> <s id="x222">2</s> <s id="x333">3 <s id="x3331">3.1</s> <s id="x3332">3.2</s> <s id="x3333">3.3</s> </s> <s id="x444">4</s> <s id="x555">5</s> <s id="x666">6</s> </a> </r>
my xslt
<select id ="s" name="slist"> <option> <xsl:attribute name="value"></xsl:attribute> please select s </option> <xsl:for-each select="a[id='x']//s"> <xsl:if test="'x3333'= @id"> <xsl:variable name="currents" select="true()"/> </xsl:if> <xsl:if test="'x3333'!= @id"> <xsl:variable name="currents" select="false()"/> </xsl:if> <xsl:if test="@currents = false()"> <option> <xsl:if test="@id = 'x3333'"> <xsl:attribute name="selected">selected</xsl:attribute> </xsl:if> <xsl:attribute name="value"> <xsl:value-of select="@id"/> </xsl:attribute> <xsl:value-of select="text"/> </option> </xsl:if> </xsl:for-each>
i'm trying create drop down list , need include s
items in list. passing current position (current s
, in example x3333
) , current id of a
. want list of s
elements within recent a
element.
in example, since current s
x3333
, id of a
x
need x111
,x222
,x333
,x3331
,x3332
in list. means need eliminate these
<s id="x444">4</s>, <s id="x555">5</s>, <s id="x666">6</s>, nodes
from code <xsl:for-each select="a[id='x']//s">
i'm getting list of s
elements in a
id='x'
can suggest me solution please?
it not clear try do.
here hints xslt:
wrong access of attribute: <xsl:for-each select="a[id='x']//s">
test attribute id use @id: <xsl:for-each select="a[@id='x']//s">
you cant redefine variable:
<xsl:if test="'x3333'= @id"> <xsl:variable name="currents" select="true()"/> </xsl:if> <xsl:if test="'x3333'!= @id"> <xsl:variable name="currents" select="false()"/> </xsl:if>
the variable currents defined in scope of xsl:if. set variable value depending of attribute id try this:
<xsl:variable name="currents"> <xsl:choose> <xsl:when test="@id = 'x3333'"> <xsl:text>true</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>false</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable>
wrong access of variable value: <xsl:if test="@currents = false()">
xslt variable access done wint $
prefix.
<xsl:if test="$currents = 'false'">
according interpretation of xlst may help:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" > <xsl:template match="r"> <select id ="s" name="slist"> <option> <xsl:attribute name="value"></xsl:attribute> please select s </option> <xsl:for-each select="a[@id='x']//s"> <xsl:variable name="currents"> <xsl:choose> <xsl:when test="preceding::s[@id = 'x3333']"> <xsl:text>true</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>false</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:if test="$currents = 'false'"> <option> <xsl:if test="@id = 'x3333'"> <xsl:attribute name="selected">selected</xsl:attribute> </xsl:if> <xsl:attribute name="value"> <xsl:value-of select="@id"/> </xsl:attribute> <xsl:value-of select="normalize-space(text())"/> </option> </xsl:if> </xsl:for-each> </select> </xsl:template> </xsl:stylesheet>
which generate following output:
<?xml version="1.0"?> <select id="s" name="slist"> <option value=""> please select s </option> <option value="x111">1</option> <option value="x222">2</option> <option value="x333">3</option> <option value="x3331">3.1</option> <option value="x3332">3.2</option> <option selected="selected" value="x3333">3.3</option> </select>
Comments
Post a Comment