XSLT检查以select / when格式传递param字符串值

时间:2017-06-08 10:40:20

标签: c# xml xslt

我正在尝试从C#程序中读取参数,其中userPrimaryInput是字母R或T之间的选择,表示不同类别的结果,userSecondary输入是与xml节点编号匹配的C#数组的索引。我的问题是,尽管基于用户输入所需的html输出将其全部置于选择/何时块中,但它似乎只产生第一个时间。

那么,我的问题是,如何正确使用select / when格式来检查一个字符串参数(这里是userPrimaryInput参数)?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>



<xsl:param name="userPrimaryInput"/>

<xsl:param name="userSecondaryInput"/>

<xsl:template match="/">

<xsl:choose>
  <!--IF Region select-->
   <xsl:when test="userPrimaryInput = R | userPrimaryInput = r "><!--Region 
select if-->
<html>
  <body>
    <h1>

     Incidents of Crime in <xsl:value-of select="/crime-in-
         canada/region[position()=$userSecondaryInput]/@name"/>, 2013
    </h1>
    <table border="1">
      <tr>
        <th>Crime Type</th>
        <th>Incidents</th>
        <th>Rate(incidents/100,000)</th>
      </tr>
      <tr>

          <xsl:for-each select="/crime-in-  
           canada/region[position()=$userSecondaryInput]/crime">
            <tr>
        <td>
          <xsl:value-of select="@type"/>
        </td>
        <td>
          <xsl:value-of select="@incidents"/>
        </td>
        <td>
          <xsl:value-of select="(@incidents div (../population-millions*1000000 div 100000))"/>
        </td>
     </tr>
        </xsl:for-each>
      </tr>
    </table>
  </body>
</html>
</xsl:when><!--End of region select if -->
<!--IF Type select-->
  <xsl:when test="userPrimaryInput = T | userPrimaryInput = t ">
    <html>
      <body>
        <h1>

          Incidents of <xsl:value-of select="/crime-in-
    canada/region/crime[position()=$userSecondaryInput]/@type"/> Across All 
    Regions, 2013
        </h1>
        <table border="1">
          <tr>
            <th>Crime Type</th>
            <th>Incidents</th>
            <th>Rate(incidents/100,000)</th>
          </tr>
          <tr>

            <xsl:for-each select="/crime-in-
 canada/region/crime[position()=$userSecondaryInput]">
              <tr>
                <td>
                  <xsl:value-of select="../@name"/>
                </td>
                <td>
                  <xsl:value-of select="@incidents"/>
                </td>
                <td>
                  <xsl:value-of select="(@incidents div (../population-
 millions*1000000 div 100000))"/>
                </td>
              </tr>
            </xsl:for-each>
          </tr>
        </table>
      </body>
    </html>
  </xsl:when>
</xsl:choose>
  </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

我很惊讶你得到任何输出,因为我认为罪魁祸首就是这条线......

<xsl:when test="userPrimaryInput = R | userPrimaryInput = r ">

这里有三个问题......

  1. 您错过了$的{​​{1}}前缀,因此它正在寻找名为userPrimaryInput的元素,而不是参数
  2. 执行userPrimaryInput表示它希望将其与名为userPrimaryInput = R的元素进行比较。使用R将其与字符串文字进行比较
  3. 'R'是union运算符。它不是一个逻辑“或”运算符。在这里使用“或”!
  4. 所以,测试应该是这样的

    |

    同样适用于<xsl:when test="$userPrimaryInput = 'R' or $userPrimaryInput = 'r'"> 的线路测试(或者您也可以在此使用T