2013-10-31 92 views
-1

我需要能夠將平坦的xml數據集轉換爲html表格,並且我無法找到符合我需要的語法示例。我想使用一個樣式表,可以將類似的外觀數據集轉換爲具有可變列的html表格。 這是我的XML文件的一部分:使用XSLT將XML數據轉換爲HTML表格

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="XSLT_StyleSheet.xsl"?> <Services> 

    <Service WsdlUrl="http://venus.eas.asu.edu/WSRepository/Services/BasicThreeSvc/Service.svc"> 
    <Name>ServiceName</Name> 
    <Provider></Provider> 
    <Category>CatName</Category> 
    <Operations> 
     <Operaion> 
     <Name>HelloWorld</Name> 
     <MsgIn>elloWorldInputMessage</MsgIn> 
     <MsgOut>HelloWorldOutputMessage</MsgOut> 
     </Operaion> 
     <Operaion> 
     <Name>OP2name</Name> 
     <MsgIn>InputMessage</MsgIn> 
     <MsgOut>OutputMessage</MsgOut> 
     </Operaion> 
<Operaion> 
     <Name>Op3Name</Name> 
     <MsgIn>InputMessage</MsgIn> 
     <MsgOut>OutputMessage</MsgOut> 
     </Operaion>  
    </Operations> 

這個HTML表必須看怎麼樣:

enter image description here

回答

1

如果你沒有找到XML轉換成的例子帶XSLT的HTML,那麼你看起來不是很難。這是它的主要動機之一。無論如何,這應該讓你開始:在你(修正)文件(這是缺少結束標記)

<?xml version="1.0" encoding="utf-8"?> 
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="node()|@*"/> 

    <xsl:template match="/Services"> 
     <html> 
      <head> 
       <title>XSLT example</title> 
      </head> 
      <body> 
       <xsl:apply-templates/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="Service"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="Operations"> 
     <table> 
      <thead> 
       <tr> 
        <td>Name</td> 
        <td>Description</td> 
        <td>Type</td> 
       </tr> 
      </thead> 
      <tbody> 
       <xsl:apply-templates/> 
      </tbody> 
     </table> 
    </xsl:template> 

    <xsl:template match="Operaion"> <!-- [sic] --> 
     <xsl:variable name="service" select="ancestor::Service"/> 

     <tr> 
      <td><xsl:value-of select="$service/Name"/></td> 
      <td><xsl:value-of select="Name"/></td> 
      <td><xsl:value-of select="$service/Category"/></td> 
     </tr> 
    </xsl:template> 

</xsl:transform> 

輸出:

<html> 
    <head> 
     <title>XSLT example</title> 
    </head> 
    <body> 
     <table> 
      <thead> 
       <tr> 
        <td>Name</td> 
        <td>Description</td> 
        <td>Type</td> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>ServiceName</td> 
        <td>HelloWorld</td> 
        <td>CatName</td> 
       </tr> 
       <tr> 
        <td>ServiceName</td> 
        <td>OP2name</td> 
        <td>CatName</td> 
       </tr> 
       <tr> 
        <td>ServiceName</td> 
        <td>Op3Name</td> 
        <td>CatName</td> 
       </tr> 
      </tbody> 
     </table> 
    </body> 
</html> 
+0

它似乎是確定首先映入眼簾的,但是當我嘗試在瀏覽器中打開XML文件,它不會像表格這樣出現在這兩個文件中:https://www.dropbox.com/sh/gggkh4wg94simrl/2GbiHAGkVF – user1863359

+0

正如TimC在[這個問題]的答案中指出的那樣(http: //stackoverflow.com/q/19751667/4525),您應該使用CSS來設置表格的樣式。請將此答案標記爲已接受,因爲您正在使用代碼(實際上聲稱它是您自己的)。如果你總是被要求做一些基本的事情,比如[提供代碼](http://stackoverflow.com/q/18361330/4525)和[接受答案](http:///stackoverflow.com/a/17378277/4525)。 – harpo

+0

很明顯,但放心,這是真誠的,我還在學習! 謝謝你的回答 – user1863359