我想要使用xslt:
在html表中显示这个XML文件<bottle>
<composition>
<ion type="positif">calcium 67.32mg/l</ion>
<ion type="positif">magnésium 10.08mg/l</ion>
<ion type="negatif">chlorure 20.82mg/l</ion>
<ion type="negatif">nitrate 3.5mg/l</ion>
<autre type="metal">fer</autre>
</composition>
</bottle>
<bottle>
<composition>
<ion type="positif">calcium 60.32mg/l</ion>
<ion type="positif">magnésium 15.28mg/l</ion>
<ion type="negatif">chlorure 25.2mg/l</ion>
<ion type="negatif">nitrate 1.5mg/l</ion>
</composition>
</bottle>
我想像这样显示每个节点:
----------------------------------
composition | positif |
|--------------------|
| calcium 67.32mg/l |
| magnésium 10.08mg/l|
|--------------------|
| negatif |
|--------------------|
| chlorure 20.82mg/l |
| nitrate 3.5mg/l |
|--------------------|
| autre |
|--------------------|
| fer |
---------------------------------|
这就是我设法做的所有事情,而且它没有显示任何内容:
<tr>
<th rowspan="6">Composition</th>
<xsl:for-each select="document('Pub.xml')/Magasin/bouteille/composition[count(.| key('type-ion',@type)[1])=1]">
<th> <xsl:value-of select="@type"/> </th>
<tr>
<td><xsl:for-each select="key('type-ion',@type)">
<xsl:value-of select="."/>
</xsl:for-each></td>
</tr>
</xsl:for-each>
</tr>
你能帮助我吗?
答案 0 :(得分:2)
给出输入:
<强> XML 强>
<Magasin>
<bouteille>
<composition>
<ion type="positif">calcium 67.32mg/l</ion>
<ion type="positif">magnésium 10.08mg/l</ion>
<ion type="negatif">chlorure 20.82mg/l</ion>
<ion type="negatif">nitrate 3.5mg/l</ion>
<autre type="metal">fer</autre>
</composition>
</bouteille>
<bouteille>
<composition>
<ion type="positif">calcium 60.32mg/l</ion>
<ion type="positif">magnésium 15.28mg/l</ion>
<ion type="negatif">chlorure 25.2mg/l</ion>
<ion type="negatif">nitrate 1.5mg/l</ion>
</composition>
</bouteille>
</Magasin>
以下样式表:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="Magasin">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="composition">
<xsl:variable name="height" select="count(distinct-values(*/@type)) + count (*)" />
<table border="1">
<xsl:for-each-group select="*" group-by="@type">
<tr>
<xsl:if test="position()=1">
<th rowspan="{$height}">Composition</th>
</xsl:if>
<th>
<xsl:value-of select="current-grouping-key()"/>
</th>
</tr>
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</table>
</xsl:template>
<xsl:template match="composition/*">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
将返回:
<强>结果强>
<html>
<body>
<table border="1">
<tr>
<th rowspan="8">Composition</th>
<th>positif</th>
</tr>
<tr>
<td>calcium 67.32mg/l</td>
</tr>
<tr>
<td>magnésium 10.08mg/l</td>
</tr>
<tr>
<th>negatif</th>
</tr>
<tr>
<td>chlorure 20.82mg/l</td>
</tr>
<tr>
<td>nitrate 3.5mg/l</td>
</tr>
<tr>
<th>metal</th>
</tr>
<tr>
<td>fer</td>
</tr>
</table>
<table border="1">
<tr>
<th rowspan="6">Composition</th>
<th>positif</th>
</tr>
<tr>
<td>calcium 60.32mg/l</td>
</tr>
<tr>
<td>magnésium 15.28mg/l</td>
</tr>
<tr>
<th>negatif</th>
</tr>
<tr>
<td>chlorure 25.2mg/l</td>
</tr>
<tr>
<td>nitrate 1.5mg/l</td>
</tr>
</table>
</body>
</html>
呈现为: