-1

フラットな xml データ セットを html テーブルに変換できるようにする必要がありますが、ニーズに合った構文例を見つけるのに苦労しています。似たようなデータ セットを可変列を持つ html テーブルに変換できるスタイルシートを 1 つ使用したいと考えています。これは私の XML ファイルの一部です:

<?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 テーブルが次のように見える必要がある方法です。

ここに画像の説明を入力

4

1 に答える 1

1

XSLT を使用して XML を HTML に変換する例が見つからなかったとしても、それほど難しくはありません。それが主な動機の 1 つです。とにかく、これで始められるはずです:

<?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>
于 2013-10-31T17:41:17.670 に答える