xml到html使用xslt样式表

时间:2014-10-31 09:08:31

标签: xml xslt

我正在尝试使用xslt样式表将xml文件转换为html格式以打印输出值。 我的.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type= "text/xsl" href="rt.xsl"?>
<entityset>    
<entity id="32" name="RT: Testing : Testing" hidden="true">
    <attribute id="1242" name="Tiketi receiver" code="start_supportGroup">
        <value>3.Saldo</value>
    </attribute>
    <attribute id="682" name="Kohde" code="store">
        <reference id="288799" name="Market"/>
    </attribute>
    <attribute id="683" name="Contact" code="person">
        <value>john.doe@market.com</value>
    </attribute>
    <attribute id="684" name="Puhelinnumero" code="phone">
        <value>0505444566</value>
    </attribute>
</entity>
<entity id="32" name="RT: Testing2 : Testing2" hidden="true">
    <attribute id="1243" name="Tiketi receiver2" code="start_supportGroup">
        <value>4.Saldo</value>
    </attribute>
    <attribute id="682" name="Kohde" code="store">
        <reference id="288799" name="Market2"/>
    </attribute>
    <attribute id="683" name="Contact" code="person">
        <value>john.doe2@market.com</value>
    </attribute>
    <attribute id="684" name="Puhelinnumero" code="phone">
        <value>05054445663</value>
    </attribute>
</entity>
</entityset>

我想打印出属性中code =“”部分的html td元素。 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="entity">
<html>
<body>
<h2>RKPP-TIKETIT</h2>
<table border="1">
  <tr bgcolor="#9acd32">
    <th style="text-align:left">ID</th>
    <th style="text-align:left">Kohde</th>
    <th style="text-align:left">StartSupportgroup</th>
  </tr>
  <xsl:for-each select="entity/attribute">
  <tr>
    <td><xsl:for-each select="attribute">
  <xsl:attribute name="{@code}" >
     <xsl:value-of select="."/>
  </xsl:attribute>
  </xsl:for-each></td>
  </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

嗨,我可以做到这一点,请建议。

编辑:这就是我想要实现的目标:

start_supportgroup  store   phone
<td>3.Saldo</td>    <td>Market</td>     <td>505444566</td>
<td>4.Saldo</td>    <td>Market2</td>    <td>5054445663</td>

作为图片: http://www.imgrobot.com/image/wUQ

谢谢,

托比

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你需要这样的东西:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/entityset">
    <table border="1">
        <tr>
            <th>StartSupportgroup</th>
            <th>Store</th>
            <th>Phone</th>
        </tr>
        <xsl:apply-templates select="entity"/>
    </table>
</xsl:template>

<xsl:template match="entity">  
    <tr>
        <td><xsl:value-of select="attribute[@code='start_supportGroup']/value"/></td>
        <td><xsl:value-of select="attribute[@code='store']/reference/@name"/></td>
        <td><xsl:value-of select="attribute[@code='phone']/value"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

应用于您的输入,将返回:

<?xml version="1.0" encoding="utf-8"?>
<table border="1">
   <tr>
      <th>StartSupportgroup</th>
      <th>Store</th>
      <th>Phone</th>
   </tr>
   <tr>
      <td>3.Saldo</td>
      <td>Market</td>
      <td>0505444566</td>
   </tr>
   <tr>
      <td>4.Saldo</td>
      <td>Market2</td>
      <td>05054445663</td>
   </tr>
</table>

呈现为:

enter image description here