【问题标题】:Combining XML nodes with XSL将 XML 节点与 XSL 相结合
【发布时间】:2015-06-29 23:02:30
【问题描述】:

我有一个 API,我目前正在使用 SOAP 调用,它正在输出一个 XML 文件。我遇到麻烦的地方是将文件放入表格中以便于阅读。最大的问题是asset.device 和相应的asset.os 没有被包裹在任何东西中。

输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<DeviceAssetInfoExport>
  <asset.device>
    <asset.device.id>123</asset.device.id>
    ...
  </asset.device>
  <asset.os>
    <asset.os.reportedos>abc</asset.os.reportedos>
    ...
  <asset.os>
  <asset.device>
    <asset.device.id>321</asset.device.id>
    ...
  </asset.device>
  <asset.os>
    <asset.os.reportedos>cba</asset.os.reportedos>
    ...
  <asset.os>
</DeviceAssetInfoExport>

所需的输出是一个 html 表格,如:

<html>
<body>
  <h2>Servers</h2>
    <table>
      <tr>
        <th>Name</th>
        <th>Address</th>
        <th>OS</th>
      </tr>
      <tr>
        <td>123</td>
        <td>home.co</td>
        <td>abc</td>
      </tr>
  </table>
</body>
</html>

我目前的尝试如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="DeviceAssetInfoExport">
    <h2>Servers</h2>
        <table border="1">
            <tr bgcolor="gray">
                <th>Name</th>
                <th>Address</th>
                <th>OS</th>
            </tr>
            <xsl:apply-templates select="asset.device | asset.os"/>
        </table>
</xsl:template>

<xsl:template match="asset.device | asset.os">
    <tr>
        <td><xsl:value-of select="asset.device.longname"/></td>
        <td><xsl:value-of select="asset.device.uri"/></td>
        <td><xsl:value-of select="asset.os.reportedos"/> - <xsl:value-of select="asset.os.osarchitecture"/></td>
    </tr>
</xsl:template>


</xsl:stylesheet>

不幸的是,我从中收到的输出将asset.os.reportedos 信息放在表中自己的行中。它位于正确的第三位置,只是不在设备信息的正确行上。

如果我可以做些什么来使我想要的结果更清楚,请告诉我。

谢谢!

【问题讨论】:

  • 我不知道你的问题是什么。首先你说想要的输出是[一些 XML],然后你说你想要一个 HTML 表格。并且您的输入没有您在 XSLT 中引用的节点,例如asset.device.longname。您为什么不向我们展示您正在使用的输入 XML 的最小但完整示例,以及您希望在 XSL 转换结束时看到的实际输出。
  • 感谢您的评论!我在最初的问题演示中意见不一,我觉得如果我能以更有用的格式获得 XML,我就能达到我想要的结果,但直接得到解决方案更有意义。如果还不清楚,请告诉我。
  • 我怀疑您希望表格显示实际值(当前从您的输出中丢失),而不是它们的标签。如果是这样,请编辑您的输入以显示两条完整的记录,具有可识别的值,并相应地调整您的预期输出。
  • 您是正确的,我已经进行了建议的编辑。谢谢

标签: xml xslt


【解决方案1】:

看看这是否能让你开始:

输入 XML

<DeviceAssetInfoExport>
  <asset.device>
    <asset.device.id>123</asset.device.id>
  </asset.device>
  <asset.os>
    <asset.os.reportedos>abc</asset.os.reportedos>
  </asset.os>
  <asset.device>
    <asset.device.id>456</asset.device.id>
  </asset.device>
  <asset.os>
    <asset.os.reportedos>def</asset.os.reportedos>
  </asset.os>
</DeviceAssetInfoExport>

XSLT

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

<xsl:template match="/DeviceAssetInfoExport">
    <html>
        <body>
            <h2>Servers</h2>
            <table border="1">
                <tr>
                    <th>ID</th>
                    <th>OS</th>
                </tr>
                <xsl:apply-templates select="asset.device"/>
            </table>
        </body>
    </html>     
</xsl:template>

<xsl:template match="asset.device">
    <tr>
        <td><xsl:value-of select="asset.device.id"/></td>
        <td><xsl:value-of select="following-sibling::asset.os[1]/asset.os.reportedos"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

结果

<html>
   <body>
      <h2>Servers</h2>
      <table border="1">
         <tr>
            <th>ID</th>
            <th>OS</th>
         </tr>
         <tr>
            <td>123</td>
            <td>abc</td>
         </tr>
         <tr>
            <td>456</td>
            <td>def</td>
         </tr>
      </table>
   </body>
</html>

【讨论】:

  • michael,非常感谢您抽出宝贵时间帮助我阐明我想要的目标并解决问题。 following-sibling 属性正是我所需要的,我什至不知道它的存在。
  • @TomMcDonald 不客气。请注意:following-sibling 是 轴,而不是属性。术语很重要。
猜你喜欢
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多