首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >XML节点与XSL的结合

XML节点与XSL的结合
EN

Stack Overflow用户
提问于 2015-06-29 15:02:30
回答 1查看 65关注 0票数 1

我有一个API,我目前正在使用SOAP调用,它正在输出一个XML文件。我遇到麻烦的地方是把文件放进桌子里方便阅读。最大的问题是asset.device和相应的asset.os没有包装在任何东西中。

产出大致如下:

代码语言:javascript
运行
AI代码解释
复制
<?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表,如下所示:

代码语言:javascript
运行
AI代码解释
复制
<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>

我目前的尝试如下:

代码语言:javascript
运行
AI代码解释
复制
<?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信息放在表中它自己的行中。这是在正确的第三个位置,它只是不正确的行与设备的信息。

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-29 16:06:47

看看这能不能让你开始:

输入XML

代码语言:javascript
运行
AI代码解释
复制
<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

代码语言:javascript
运行
AI代码解释
复制
<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>

结果

代码语言:javascript
运行
AI代码解释
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31126981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文