2015-06-29 108 views
1

我有一个API,我正在使用SOAP调用并输出一个XML文件。我遇到麻烦的地方是把文件放在桌子上以便阅读。最大的问题是asset.device和相应的asset.os不包含任何内容。将XML节点与XSL结合使用

输出是沿着线:

<?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信息放在选项卡中的自己的行上乐。它处于正确的第三个位置,它不在设备信息的正确行上。

如果有什么我可以做,使我想要的结果更清楚,请让我知道。

谢谢!

+0

我无法弄清楚你的问题是什么。首先你说所需的输出是[一些XML],然后你说你想要一个HTML表格。您的输入中没有您在XSLT中引用的节点,例如'asset.device.longname'。你为什么不向我们展示一个最小的 - 但是完整的** - 你正在使用的输入XML范例,以及你想在XSL转换结束时看到的实际输出。 –

+0

感谢您的评论!我在最初的问题演示中分裂了,我觉得如果我能够以更有用的格式获得XML,那么我就可以达到我想要的结果,但更直接的解决方案更有意义。如果它仍然不清楚让我知道。 –

+0

我怀疑你希望表格显示实际值(当前从输出中丢失),而不是它们的标签。如果是这样,请编辑您的输入,以显示两个完整的记录,具有可识别的值,并相应地调整您的预期输出。 –

回答

2

看看这可以让你开始:

输入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> 
+0

迈克尔,非常感谢您花时间帮助我澄清我期望的目标并解决问题。以下 - 兄弟姐妹属性正是我所需要的,我甚至没有意识到它的存在。 –

+0

@TomMcDonald不​​客气。请注意:'follow-sibling'是一个**轴**,而不是一个属性。术语很重要。 –