XSL 使用变量评估属性
XSL evaluate attribute with variables
我想使用 xsl 文件来显示和评估 xml 文件。 XML 文件包含一定数量的零件,这些零件具有标题、描述、图像和数量。但这个数量取决于我将在 XSL 文件中声明的一些变量。但在这种情况下,我必须从 XSL 文件的 XML 中计算表达式。有办法吗?
参见示例:(@amount 中的表达式应使用 XML 中的变量求值)
XML:
<partslist>
<part value="1" amount="$variable1" visible="true">
<title>
<nl>Onderdeel 1</nl>
<fr>Partie 1</fr>
<en>Part 1</en>
<de>Teil 1</de>
</title>
<image src="images/partslist/part1.jpg"/>
</part>
<part value="2" amount="$variable1 * $variable2" visible="true">
<title>
<nl>Onderdeel 2</nl>
<fr>Partie 2</fr>
<en>Part 2</en>
<de>Teil 2</de>
</title>
<image src="images/partslist/part2.jpg"/>
</part>
<part value="3" amount="$variable3" visible="true">
<title>
<nl>Onderdeel 3</nl>
<fr>Partie 3</fr>
<en>Part 3</en>
<de>Teil 3</de>
</title>
<image src="images/partslist/part3.jpg"/>
</part>
<part value="4" amount="$variable1 + $variable3" visible="true">
<title>
<nl>Onderdeel 4</nl>
<fr>Partie 4</fr>
<en>Part 4</en>
<de>Teil 4</de>
</title>
<image src="images/partslist/part4.jpg"/>
</part>
</partslist>
XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1" />
<xsl:variable name="variable2" select="2" />
<xsl:variable name="variable3" select="3" />
<div class="np">
<H2>Parts List</H2>
<table border ="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td><xsl:value-of select="@value" /></td>
<td><xsl:value-of select="@amount"/></td>
<td><xsl:value-of select="title/nl"/></td>
<td><img style="width:100%;" src="{image/@src}"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
提前致谢
如果您使用支持 EXSLT dyn:evaluate
函数 (http://exslt.org/dyn/functions/evaluate/index.html) 的 Xalan 解释器之类的 XSLT 处理器,那么您可以使用
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dyn="http://exslt.org/dynamic" exclude-result-prefixes="dyn">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1" />
<xsl:variable name="variable2" select="2" />
<xsl:variable name="variable3" select="3" />
<div class="np">
<H2>Parts List</H2>
<table border ="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td><xsl:value-of select="@value" /></td>
<td><xsl:value-of select="dyn:evaluate(@amount)"/></td>
<td><xsl:value-of select="title/nl"/></td>
<td><img style="width:100%;" src="{image/@src}"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.hikmatu.com/948Fn5d。
根据您的 XSLT 处理器,即使不支持 dyn:evaluate
,您也可以通过扩展函数实现该功能。
或移动到支持 xsl:evaluate
的 XSLT 3 处理器:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
version="3.0">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1"/>
<xsl:variable name="variable2" select="2"/>
<xsl:variable name="variable3" select="3"/>
<div class="np">
<H2>Parts List</H2>
<table border="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td>
<xsl:value-of select="@value"/>
</td>
<td>
<xsl:evaluate xpath="@amount" context-item="."
with-params="
map {
xs:QName('variable1'): $variable1,
xs:QName('variable2'): $variable2,
xs:QName('variable3'): $variable3
}"
/>
</td>
<td>
<xsl:value-of select="title/nl"/>
</td>
<td>
<img style="width:100%;" src="{image/@src}"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我想使用 xsl 文件来显示和评估 xml 文件。 XML 文件包含一定数量的零件,这些零件具有标题、描述、图像和数量。但这个数量取决于我将在 XSL 文件中声明的一些变量。但在这种情况下,我必须从 XSL 文件的 XML 中计算表达式。有办法吗?
参见示例:(@amount 中的表达式应使用 XML 中的变量求值)
XML:
<partslist>
<part value="1" amount="$variable1" visible="true">
<title>
<nl>Onderdeel 1</nl>
<fr>Partie 1</fr>
<en>Part 1</en>
<de>Teil 1</de>
</title>
<image src="images/partslist/part1.jpg"/>
</part>
<part value="2" amount="$variable1 * $variable2" visible="true">
<title>
<nl>Onderdeel 2</nl>
<fr>Partie 2</fr>
<en>Part 2</en>
<de>Teil 2</de>
</title>
<image src="images/partslist/part2.jpg"/>
</part>
<part value="3" amount="$variable3" visible="true">
<title>
<nl>Onderdeel 3</nl>
<fr>Partie 3</fr>
<en>Part 3</en>
<de>Teil 3</de>
</title>
<image src="images/partslist/part3.jpg"/>
</part>
<part value="4" amount="$variable1 + $variable3" visible="true">
<title>
<nl>Onderdeel 4</nl>
<fr>Partie 4</fr>
<en>Part 4</en>
<de>Teil 4</de>
</title>
<image src="images/partslist/part4.jpg"/>
</part>
</partslist>
XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1" />
<xsl:variable name="variable2" select="2" />
<xsl:variable name="variable3" select="3" />
<div class="np">
<H2>Parts List</H2>
<table border ="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td><xsl:value-of select="@value" /></td>
<td><xsl:value-of select="@amount"/></td>
<td><xsl:value-of select="title/nl"/></td>
<td><img style="width:100%;" src="{image/@src}"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
提前致谢
如果您使用支持 EXSLT dyn:evaluate
函数 (http://exslt.org/dyn/functions/evaluate/index.html) 的 Xalan 解释器之类的 XSLT 处理器,那么您可以使用
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dyn="http://exslt.org/dynamic" exclude-result-prefixes="dyn">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1" />
<xsl:variable name="variable2" select="2" />
<xsl:variable name="variable3" select="3" />
<div class="np">
<H2>Parts List</H2>
<table border ="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td><xsl:value-of select="@value" /></td>
<td><xsl:value-of select="dyn:evaluate(@amount)"/></td>
<td><xsl:value-of select="title/nl"/></td>
<td><img style="width:100%;" src="{image/@src}"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.hikmatu.com/948Fn5d。
根据您的 XSLT 处理器,即使不支持 dyn:evaluate
,您也可以通过扩展函数实现该功能。
或移动到支持 xsl:evaluate
的 XSLT 3 处理器:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
version="3.0">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<xsl:variable name="variable1" select="1"/>
<xsl:variable name="variable2" select="2"/>
<xsl:variable name="variable3" select="3"/>
<div class="np">
<H2>Parts List</H2>
<table border="1">
<tr>
<th style="width:15%;">ID</th>
<th style="width:15%;">Amount</th>
<th style="width:40%;">Description</th>
<th style="width:30%;">Image</th>
</tr>
<xsl:for-each select="partslist/part">
<tr>
<td>
<xsl:value-of select="@value"/>
</td>
<td>
<xsl:evaluate xpath="@amount" context-item="."
with-params="
map {
xs:QName('variable1'): $variable1,
xs:QName('variable2'): $variable2,
xs:QName('variable3'): $variable3
}"
/>
</td>
<td>
<xsl:value-of select="title/nl"/>
</td>
<td>
<img style="width:100%;" src="{image/@src}"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>