Beginner's XSLT question: Select node based on a value elsewhere in the XML structure
--edited for clarity (hopefully)
I have an XML file that looks something like this:
<questionBlock>
<responses>
. . .
<response>
<degree>1</degree>
<instdegree>1</instdegree>
</response>
. . .
</responses>
<question>
<variable>degree</variable>
<text>Please select a degree or credential you have pursued.</text>
<responses>
<response>
<label>High School Diploma</label>
<value>1</value>
</response>
<response>
<label>Certificate</label>
<value>2</value>
</response>
</responses>
. . .
</question>
</questionBlock>
A subset of the XSLT looks like this:
In addition to selecting the value of the degree (1), I also want to be able to select the "High School Diploma" based on that value, but I haven't been able to think through the XPath to do this in the general case (e.g., if degree equals 2). . . .
<xsl:for-each select="responses/response">
<xsl:variable name="paddedCount" select="c开发者_开发百科oncat('00',position())"/>
<xsl:variable name="s" select="substring($paddedCount,string-length($paddedCount)-1,string-length($paddedCount))"/>
<xsl:variable name="degree" select="degree" />
. . .
Any help would be much appreciated. Thanks.
The xpath: ../../responses[response=current()]/label
EDIT: I guess you mean something like this?
<xsl:for-each select="responses/response">
<xsl:variable name="degree" select="degree" />
<xsl:variable name="degree-response" select="
../question[variable = 'degree']/responses/response[value = $degree]
" />
<xsl:value-of select="$degree-response/label" />
</xsl:for-each>
Really, you should try to refactor all of this to <xsl:apply-templates>
instead of <xsl:for-each>
. The for-each sucks in more ways than one, and it makes things harder than they should be. See: "Using xsl:for-each is almost always wrong"
For-each has it's uses, but all "regular" processing should be done through apply-templates.
Original version of the answer (for reference only, since OP has modified the question):
When the context node is <degree>1</degree>
:
parent::condition/following-sibling::responses[1]/response[1]/label
or, shorter:
../following-sibling::responses[1]/response[1]/label
精彩评论