使用xslt显示xml节点的多个实例

时间:2015-09-16 02:36:51

标签: xml xslt

我使用xslt和css为以下xml设置样式。我想知道是否可以设置xslt以便能够选择标签的多个实例并在单个代码块中对它们进行样式化。我有基本上相同的节点,在它们下面有相同的子节点,但它们是通过使用id =" 01" ID =" 02&#34 ;.

这是xml

 <?xml version="1.0" encoding="UTF-8" ?>
    <?xml-stylesheet type="text/xsl" href="devices.xsl" ?>
    <mpreader>
        <heading>Example</heading>
        <firmware>Firmware.134</firmware>
        <sn>123455</sn>
        <devices>
            <device id="01">
                <category>sim</category>
                <description>sim</description>
                <readable>yes</readable>
                <code size="00 18" crc="053F">test</code>
                <atr>4566</atr>
            </device>
            <device id="02>
                <category>sim</category>
                <description>sim</description>
                <readable>yes</readable>
                <code size="00 18" crc="053F">test</code>
                <atr>4566</atr>
         </devices>
      </mpreader>

这是我的xslt。基本上我要问的是有没有办法使用下面的代码块来不仅仅是样式而且不必重写?

<?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>
    <style type="text/css">h1{font-size:1.5em;margin-top:5px;margin-bottom:5px;}
    .deviceNum{font-size:1em; text-decoration:underline; margin-bottom:5px;font-weight:bold;}
    .deviceNum:hover .lineMov:{display:block;}
    .deviceInfo{list-style:none;font-weight:none;margin:0;margin-top:10px;padding:0;text-decoration:none;}
    .lineMov{margin-top:2px;margin-bottom:2px;}
    </style>
    </head>
    <body style="text-align:left;">
        <h1>
            <xsl:value-of select="mpreader/heading"/>
        </h1>
        <h1>
            Firmware: <xsl:value-of select="mpreader/firmware"/>
        </h1>
        <h1>
            Serial Number: <xsl:value-of select="mpreader/sn"/>
        </h1>
        <h1>
            Devices: <xsl:value-of select="count(//device)"/>
        </h1>
            <xsl:for-each select="mpreader/devices/device">
        <ul class="deviceInfo">
        <div class="deviceNum">
            Device <xsl:value-of select="mpreader/devices/device/@id"/>
        </div>
            <li class="lineMov">
            Simple Mobile
            </li>
            <li  class="lineMov">
                Category: <xsl:value-of select="//category"/>
            </li >
            <li  class="lineMov">
                Device Description: <xsl:value-of select="//description"/>
            </li >
            <li  class="lineMov">
                Readable: <xsl:value-of select="//readable"/>
            </li>
            <li  class="lineMov">
                Code: <xsl:value-of select="//code"/>
            </li >
            <li  class="lineMov">
                ATR: <xsl:value-of select="//atr"/>
            </li >
            <li class="lineMov">
                ICCID: <xsl:value-of select="//iccid"/>
            </li>
        </ul>
            </xsl:for-each>

我想使用列表在设备下显示这些项目。每个新设备都应显示在上一个设备下方。

1 个答案:

答案 0 :(得分:0)

This XSLT 2.0 stylesheet ...

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

<xsl:output method="html" version="5" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <hmtl>
    <head>
      <title>New Version!</title>
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </hmtl>
</xsl:template>

<xsl:template match="mpreader">
  <h1><xsl:value-of select="heading"/></h1>
  <h1>Firmware: <xsl:value-of select="firmware"/></h1>
  <h1>Serial Number: <xsl:value-of select="sn"/></h1>
  <h1>Devices: <xsl:value-of select="count(devices/device)"/></h1>
  <xsl:apply-templates select="devices/device" />    
</xsl:template>

<xsl:template match="device">
  <div class="deviceNum">Device <xsl:value-of select="@id"/></div>
  <ul class="deviceInfo">
    <li class="lineMov">Simple Mobile</li>
    <li class="lineMov">Category: <xsl:value-of select="category"/></li>
    <li class="lineMov">Device Description: <xsl:value-of select="description"/></li>
    <li class="lineMov">Readable: <xsl:value-of select="readable"/></li>
    <li class="lineMov">Code: <xsl:value-of select="code"/></li>
    <li class="lineMov">ATR: <xsl:value-of select="atr"/></li>
  </ul>
</xsl:template>

</xsl:transform>

...将转换此输入文档......

<mpreader>
    <heading>Example</heading>
    <firmware>Firmware.134</firmware>
    <sn>123455</sn>
    <devices>
        <device id="01">
            <category>sim</category>
            <description>sim</description>
            <readable>yes</readable>
            <code size="00 18" crc="053F">test</code>
            <atr>4566</atr>
        </device>
        <device id="02">
            <category>sim</category>
            <description>sim</description>
            <readable>yes</readable>
            <code size="00 18" crc="053F">test</code>
            <atr>4566</atr>
        </device>
    </devices>
</mpreader>

进入这个输出html文档...

&#13;
&#13;
<!DOCTYPE HTML>
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>New Version!</title>
   </head>
   <body>
      <h1>Example</h1>
      <h1>Firmware: Firmware.134</h1>
      <h1>Serial Number: 123455</h1>
      <h1>Devices: 2</h1>
      <div class="deviceNum">Device 01</div>
      <ul class="deviceInfo">
         <li class="lineMov">Simple Mobile</li>
         <li class="lineMov">Category: sim</li>
         <li class="lineMov">Device Description: sim</li>
         <li class="lineMov">Readable: yes</li>
         <li class="lineMov">Code: test</li>
         <li class="lineMov">ATR: 4566</li>
      </ul>
      <div class="deviceNum">Device 02</div>
      <ul class="deviceInfo">
         <li class="lineMov">Simple Mobile</li>
         <li class="lineMov">Category: sim</li>
         <li class="lineMov">Device Description: sim</li>
         <li class="lineMov">Readable: yes</li>
         <li class="lineMov">Code: test</li>
         <li class="lineMov">ATR: 4566</li>
      </ul>
   </body>
</hmtl>
&#13;
&#13;
&#13;

注意事项

  1. 我无法弄清楚您对ICCID的订单项的意图是什么,因为提供的示例文档中没有此类元素。
  2. 我排除了头部中的style元素,因为它不是问题的一部分,采用此样式表并根据需要添加样式元素是一项微不足道的练习。