2015-11-07 97 views
-1

我有我想要使用XSLT在HTML表格中显示这个XML文件:显示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

究竟什么是你的问题? –

+0

如何使用xslt在表中显示该xml文件? – Mina

+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> 

呈现为:

enter image description here

+0

这对你来说非常慷慨。 – kjhughes

+0

非常感谢@ michael.hor257k – Mina