开发者

How to filter what is shown in the for-each loop in XSL

开发者 https://www.devze.com 2023-02-08 03:28 出处:网络
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 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:

  1. There is no == operator in XPath. You probably want the = comparison operator.

  2. \'PB\' is syntactically invalid -- you probably meant just 'PB'

  3. 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 expression NewDataSet/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消