Xml 使用XSLT显示数据
我有一个xml文件,它是Xml 使用XSLT显示数据,xml,xslt,Xml,Xslt,我有一个xml文件,它是 <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="test1.xsl"?> <products> <node> <node> <dist_value> <node> 55 </node>
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="test1.xsl"?>
<products>
<node>
<node>
<dist_value>
<node> 55 </node>
<node> 59 </node>
<node> 72 </node>
</dist_value>
<reg_str_dt>
<node> 2013-08-03 17:29:00 </node>
</reg_str_dt>
<product_id> 1 </product_id>
</node>
</node>
<node>
<node>
<dist_value>
<node> 72 </node>
<node> 19 </node>
<node> 49 </node>
</dist_value>
<reg_str_dt>
<node> 2013-10-25 17:29:00 </node>
</reg_str_dt>
<product_id> 2 </product_id>
</node>
</node>
<node>
<node>
<dist_value>
<node> 12 </node>
<node> 548 </node>
<node> 112 </node>
</dist_value>
<reg_str_dt>
<node> 2013-08-12 17:29:00 </node>
</reg_str_dt>
<name> test </name>
<product_id> 3 </product_id>
</node>
</node>
</products>
我是xslt的初学者,这里有些问题
我想要这样的输出
Product ID | Product DATA
--------------------------------
2 |dist_value => 72
| 19
| 79
|reg_str_dt => 2013-10-25 17:29:00
|product_id => 2
对于包含名称的产品3,您需要稍微修改模板select语句。 在匹配语句中,您选择的是
节点,因此在模板的其余部分中,select语句需要相对于该
节点,如下所示:
<xsl:template match="products/node/node/dist_value[node < 50]">
<tr>
<td>
<xsl:value-of select="../product_id" />
</td>
<td>
<xsl:for-each select="./node">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
<xsl:value-of select="../reg_str_dt/node" />
</td>
</tr>
</xsl:template>
您需要稍微修改模板select语句。
在匹配语句中,您选择的是
节点,因此在模板的其余部分中,select语句需要相对于该
节点,如下所示:
<xsl:template match="products/node/node/dist_value[node < 50]">
<tr>
<td>
<xsl:value-of select="../product_id" />
</td>
<td>
<xsl:for-each select="./node">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
<xsl:value-of select="../reg_str_dt/node" />
</td>
</tr>
</xsl:template>
关于XSLT需要了解的一件事是,它具有内置模板的概念。如果它正在寻找与某个元素匹配的模板,但XSLT中不存在该模板,则将使用这些模板。在本例中,您首先要查找节点元素
<xsl:apply-templates select="products/node/node" />
但是,模板正在匹配dist\u值元素
<xsl:template match="products/node/node/dist_value[node < 50]">
这意味着XSLT将开始使用内置模板,该模板将输出元素的文本,然后处理所有子元素。您可能应该这样做,以匹配节点元素
<xsl:template match="products/node/node[dist_value/node < 50]">
尽管您还需要一个模板来匹配节点元素,其中dist_值不小于50。或者,您可以更改应用模板以仅选择所需的模板
<xsl:apply-templates select="products/node/node[dist_value/node < 50]" />
另一个问题是此行在dist_值模板中
<xsl:value-of select="//product_id" />
双斜杠意味着它将查找相对于根元素的产品id,并始终选择第一个。实际上,您只需要这样做,就可以查找相对于当前节点元素的产品id
<xsl:value-of select="product_id" />
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Product ID</th>
<th>Product DATA</th>
</tr>
<xsl:apply-templates select="products/node/node[dist_value/node < 50]"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="products/node/node">
<tr>
<td>
<xsl:value-of select="product_id"/>
</td>
<td>
<xsl:apply-templates select="dist_value/node"/>
</td>
</tr>
<tr>
<td/>
<td>
<xsl:value-of select="reg_str_dt/node"/>
</td>
</tr>
<tr>
<td/>
<td>
<xsl:value-of select="product_id"/>
</td>
</tr>
</xsl:template>
<xsl:template match="dist_value/node">
<xsl:value-of select="concat(., ' ')"/>
</xsl:template>
</xsl:stylesheet>
产品ID
产品数据
应用于XML时,将输出以下内容
<html>
<body>
<table border="1">
<tr>
<th>Product ID</th>
<th>Product DATA</th>
</tr>
<tr>
<td> 2 </td>
<td> 72 19 49 </td>
</tr>
<tr>
<td/>
<td> 2013-10-25 17:29:00 </td>
</tr>
<tr>
<td/>
<td> 2 </td>
</tr>
<tr>
<td> 3 </td>
<td> 12 548 112 </td>
</tr>
<tr>
<td/>
<td> 2013-08-12 17:29:00 </td>
</tr>
<tr>
<td/>
<td> 3 </td>
</tr>
</table>
</body>
</html>
产品ID
产品数据
2.
72 19 49
2013-10-25 17:29:00
2.
3.
12 548 112
2013-08-12 17:29:00
3.
关于XSLT需要了解的一件事是,它具有内置模板的概念。如果它正在寻找与某个元素匹配的模板,但XSLT中不存在该模板,则将使用这些模板。在本例中,您首先要查找节点元素
<xsl:apply-templates select="products/node/node" />
但是,模板正在匹配dist\u值元素
<xsl:template match="products/node/node/dist_value[node < 50]">
这意味着XSLT将开始使用内置模板,该模板将输出元素的文本,然后处理所有子元素。您可能应该这样做,以匹配节点元素
<xsl:template match="products/node/node[dist_value/node < 50]">
尽管您还需要一个模板来匹配节点元素,其中dist_值不小于50。或者,您可以更改应用模板以仅选择所需的模板
<xsl:apply-templates select="products/node/node[dist_value/node < 50]" />
另一个问题是此行在dist_值模板中
<xsl:value-of select="//product_id" />
双斜杠意味着它将查找相对于根元素的产品id,并始终选择第一个。实际上,您只需要这样做,就可以查找相对于当前节点元素的产品id
<xsl:value-of select="product_id" />
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Product ID</th>
<th>Product DATA</th>
</tr>
<xsl:apply-templates select="products/node/node[dist_value/node < 50]"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="products/node/node">
<tr>
<td>
<xsl:value-of select="product_id"/>
</td>
<td>
<xsl:apply-templates select="dist_value/node"/>
</td>
</tr>
<tr>
<td/>
<td>
<xsl:value-of select="reg_str_dt/node"/>
</td>
</tr>
<tr>
<td/>
<td>
<xsl:value-of select="product_id"/>
</td>
</tr>
</xsl:template>
<xsl:template match="dist_value/node">
<xsl:value-of select="concat(., ' ')"/>
</xsl:template>
</xsl:stylesheet>
产品ID
产品数据
应用于XML时,将输出以下内容
<html>
<body>
<table border="1">
<tr>
<th>Product ID</th>
<th>Product DATA</th>
</tr>
<tr>
<td> 2 </td>
<td> 72 19 49 </td>
</tr>
<tr>
<td/>
<td> 2013-10-25 17:29:00 </td>
</tr>
<tr>
<td/>
<td> 2 </td>
</tr>
<tr>
<td> 3 </td>
<td> 12 548 112 </td>
</tr>
<tr>
<td/>
<td> 2013-08-12 17:29:00 </td>
</tr>
<tr>
<td/>
<td> 3 </td>
</tr>
</table>
</body>
</html>
产品ID
产品数据
2.
72 19 49
2013-10-25 17:29:00
2.
3.
12 548 112
2013-08-12 17:29:00
3.
您的代码有很多地方出错。重点学习上下文节点、应用模板以及处理器如何迭代节点。此外,你的模式是笨拙的;如果可以,考虑将“节点/节点”替换为“产品”。
下面是一个XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr><th>Product ID</th><th>Product DATA</th></tr>
<xsl:apply-templates select="/products/node/node" />
</table>
</body>
</html>
</xsl:template>
<!-- ignore the node with dist_value child by default -->
<xsl:template match="node[ dist_value ]" />
<xsl:template match="node[ dist_value[node < 50] ]" priority="1.0">
<xsl:variable name="span" select="1 + count( * )" />
<tr>
<td align="center" rowspan="{$span}"><xsl:value-of select="product_id" /></td>
</tr>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="name">
<tr>
<td>
<xsl:value-of select="name()" />
<xsl:text disable-output-escaping="yes"> => </xsl:text>
<xsl:value-of select="." />
</td>
</tr>
</xsl:template>
<xsl:template match="dist_value">
<tr>
<td>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes"> => </xsl:text>
<xsl:for-each select="*">
<xsl:if test="position()>1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" />
</xsl:for-each>
</td>
</tr>
</xsl:template>
<xsl:template match="reg_str_dt">
<tr>
<td>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes"> => </xsl:text>
<xsl:value-of select="node" />
</td>
</tr>
</xsl:template>
<xsl:template match="product_id">
<tr>
<td>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes"> => </xsl:text>
<xsl:value-of select="." />
</td>
</tr>
</xsl:template>
产品数据
=>
=
,
=