我是这项技术的新手。我需要在这里使用查找概念,
我有一个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代码)
答案 0 :(得分:1)
当您调用pircode_lookup
模板时,您在根节点处想要将member/pir
节点作为$pircode
参数传递。所以:
<xsl:with-param name="pircode" select="member/pir" />
您还应该在pirCode
编写$pircode
时写过<xsl:choose>
一次:
<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>
来压缩代码,如上所示。