I thought it would be as easy as tell the for-each to only select top_coach_sales_vw that has "Site" equal to "PB" but when I run the script it does not loop through any of the data in the XML.
I am escaping the single quotes because it is part of a php echo.
<xsl:for-each select="NewDataSet/top_coach_sales_vw[Site==\'PB\']">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="Site"/></td>
<td><xsl:value-of select="Status"/></td>
</xsl:for-each>
XML:
<NewDataSet>
<top_coach_sales_vw>
<name>Mike</name>
<Site>PB</Site>
<State>Ready</State>
</top_coach_sales_vw>
<top_coach_sales_vw>
<name>Bill</name>
<Site>EL</Site>
<State>Talking</State>
</top_coach_sales_vw&g开发者_如何学JAVAt;
<top_coach_sales_vw>
<name>Ted</name>
<Site>PB</Site>
<State>Ready</State>
</top_coach_sales_vw>
</NewDataSet>
<xsl:for-each select="NewDataSet/top_coach_sales_vw[Site==\'PB\']">
There are a few problems with this:
There is no
==
operator in XPath. You probably want the=
comparison operator.\'PB\'
is syntactically invalid -- you probably meant just'PB'
NewDataSet
is the top element of the provided XML document. If the above<xsl:for-each>
instruction is in any template that is not matching the root (/
) of the document, the expressionNewDataSet/top_coach_sales_vw[Site='PB']
will not select any node, because this is a relative expression and will be evaluated off the current node (the one that the template matches).
Solution:
Use:
<xsl:for-each select="/NewDataSet/top_coach_sales_vw[Site='PB']">
XSLT uses a single =
as it's equality operator.
Also, you're selecting Status
in your loop body, though the xml sample you list contains State
elements - that's probably an oversight.
精彩评论