使用查找以在XML XSL中找到值

  • 本文关键字:XSL XML 查找 html xml xslt
  • 更新时间 :
  • 英文 :


我是这项技术的新手。我需要在这里使用查找概念

我有一个XML文件:如下,

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="lookup.xsl"?>
    <member>
            <name>Indhu</name>
            <pir>PIR1</pir>
            <age>25</age>
            <novel>Nothing hides</novel>
            <script>Hello you </script>
    </member>

XSL文件是:

      <?xml version="1.0" encoding="utf-8"?>

          <html>
         <head>
         <title>YOU</title>
         </head>
         <body>
         <h1>Hello world</h1>
         <div name="test">
         <table border="0">
         <tr><td><xsl:value-of select="member/name" /></td></tr>
         <tr><td><xsl:value-of select="member/age" /></td></tr> 
         <tr>
         <td>PIR Rate: </td>
              <td><xsl:value-of select="member/pir"/>
                <xsl:call-template name="pircode_lookup">
                <xsl:with-param name="pircode" select="pirCode" />
                </xsl:call-template>
              </td>
              </tr>
              </table>
              </div>
              </body> 
              </html>
               </xsl:template>
             </xsl:transform>

我有一个LookupValues XSL文件:

       <?xml version="1.0" encoding="UTF-8"?>
       <xsl:stylesheet version="1.0" 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

         <xsl:template name="pircode_lookup">
         <xsl:param name="pircode"/>
         <xsl:choose>
          <xsl:when test="$pircode='PIR0'">
            <xsl:text>Zero</xsl:text>
        </xsl:when>
        <xsl:when test="$pircode='PIR1'">
            <xsl:text>One</xsl:text>
        </xsl:when>
        <xsl:when test="$pircode='PIR2'">
            <xsl:text>Two</xsl:text>
        </xsl:when>
        <xsl:when test="$pircode='PIR3'">
            <xsl:text>Three</xsl:text>
        </xsl:when>
        <xsl:when test="$pircode='PIRD'">
            <xsl:text>Default</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>Unknown PIR Code: </xsl:text><xsl:value-of select="pirCode"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

我希望此调用访问LookupValues.xsl并为我提供PIR1的值,但是我将默认值作为结果获得(未知PIR代码(

在调用pircode_lookup模板时,您处于根节点,希望将member/pir节点作为$pircode参数传递。所以:

<xsl:with-param name="pircode" select="member/pir" />

当您应该在<xsl:choose>中写入$pircode时,您还会一次写pirCode

<xsl:choose>
    <xsl:when test="$pircode='PIR0'">Zero</xsl:when>
    <xsl:when test="$pircode='PIR1'">One</xsl:when>
    <xsl:when test="$pircode='PIR2'">Two</xsl:when>
    <xsl:when test="$pircode='PIR3'">Three</xsl:when>
    <xsl:when test="$pircode='PIRD'">Default</xsl:when>
    <xsl:otherwise>Unknown PIR Code: <xsl:value-of select="$pircode"/></xsl:otherwise>
</xsl:choose>

注意:您可以使用<xsl:text>,这是绝对正确的,以避免输出中的不必要的空格。

但是,只要您在XSL中没有任何不必要的空格,就可以通过丢弃<xsl:text>来稍微压缩代码,如上所述。

最新更新