2015-06-29 107 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'是一個**軸**,而不是一個屬性。術語很重要。 –