I have a node-set stored in a variable like below
<xsl:v开发者_如何转开发ariable name="myXML">
<list>
<input name="First" elementName="FirstName" option="one" />
<input name="Second" elementName="SecondName" option="Two" />
<input name="Third" elementName="ThirdName" option="Three" />
<input name="Fourth" elementName="FourthName" option="Four" />
</list>
</xsl:variable>
My code below retrieves the node and its attributes correctly.
But the for-each in the below code repeats even after finding the match, until it reaches the last <input>
node.
So if i have a big list with many <input>
nodes in my node-set, it may cause performance issue.
I need to re-factor the below code much simpler, may be without for-each.
<xsl:template match="/">
<xsl:variable name="checkName" select="'Third'" />
<xsl:variable name="getNode">
<xsl:for-each select="$myXML/list/input">
<xsl:if test="./@name=$checkName">
<xsl:copy-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="element" select="$getNode/input/@elementName" />
<xsl:variable name="option" select="$getNode/input/@option" />
<element><xsl:value-of select="$element" /></element>
<option><xsl:value-of select="$option" /></option>
</xsl:template>
All i wanted is, i have a input variable checkName="Third" and i need the value of the attributes 'elementName' and 'option' in two different variables that matches the value in the name attribute of the <input>
node. Please help me with a solution and also i don't want to use exslt or any other extensions.
My code below retrieves the node and its attributes correctly. But the for-each in the below code repeats even after finding the match, until it reaches the last node. So if i have a big list with many nodes in my node-set, it may cause performance issue. I need to re-factor the below code much simpler, may be without for-each.
<xsl:variable name="getNode"> <xsl:for-each select="$myXML/list/input"> <xsl:if test="./@name=$checkName"> <xsl:copy-of select="." /> </xsl:if> </xsl:for-each> </xsl:variable>
Use:
<xsl:variable name="getNode" select="$myXml/list/input[@name=$checkName]"/>
Use a predicate filter to compare the @name
to the checkName
variable.
The stylesheet can be greatly simplified by removing the for-each
and extra variables to use simple XPATH statements:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="myXML">
<list>
<input name="First" elementName="FirstName" option="one" />
<input name="Second" elementName="SecondName" option="Two" />
<input name="Third" elementName="ThirdName" option="Three" />
<input name="Fourth" elementName="FourthName" option="Four" />
</list>
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="checkName" select="'Third'" />
<element><xsl:value-of select="$myXML/list/input[@name=$checkName]/@elementName" /></element>
<option><xsl:value-of select="$myXML/list/input[@name=$checkName]/@option" /></option>
</xsl:template>
</xsl:stylesheet>
精彩评论