1

次のxmlから各「文字列」ノードをliアイテムとして出力したい:

<soapenv:Envelope 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenv="http://schemas.url.org/url/envelope/"
>
    <soapenv:Header>
        <MultiSpeakMsgHeader 
            Version="" UserID="" 
            Pwd="" AppName="xx" AppVersion="x" 
            Company="xxx" xmlns="http://www.url.org/Version_3.0"
        />
    </soapenv:Header>
    <soapenv:Body>
        <GetMethodsResponse xmlns="http://www.url.org/Version_3.0">
            <GetMethods>
                <string>GetDomainMembers</string>
                <string>GetDomainNames</string>
                <string>GetMethods</string>
                <string>pingURL</string>
                <string>GetAllMeters</string>
                <string>GetAllServiceLocations</string>
                <string>GetAllCustomers</string>
                <string>GetCustomerByCustId</string>
                <string>GetServiceLocationByAccountNo</string>
                <string>GetServiceLocationByMeterNo</string>
                <string>ReadingChangedNotification</string>
            </GetMethods>
        </GetMethodsResponse>
    </soapenv:Body>
</soapenv:Envelope>

私は現在、次のxslコードを持っています -

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exslt="http://exslt.org/common"
>
    <xsl:output method="html" />

    <xsl:template match="/GetMethods">
        <ul>
            <xsl:for-each select="GetMethods/string">
                <li>
                    <xsl:value-of select="string"/><br/>
                </li>
            </xsl:for-each>    
        </ul>
    </xsl:template>
</xsl:stylesheet>

ただし、これは、各文字列ノードをリストの書式設定なしで長い行として出力するだけです。任意の助けをいただければ幸いです。

私はこの単純な出力が欲しい:

    <ul>
        <li>GetDomainMembers</li>
        <li>GetDomainNames</li>
        <li>GetMethods</li>
        <li>pingURL</li>
        <li>GetAllMeters</li>
        <li>GetAllServiceLocations</li>
        <li>GetAllCustomers</li>
        <li>GetCustomerByCustId</li>
        <li>GetServiceLocationByAccountNo</li>
        <li>GetServiceLocationByMeterNo</li>
        <li>ReadingChangedNotification</li>
    </ul>

私は現在、この出力を取得しています(フォーマットはありません):

                                          GetDomainMembers            GetDomainNames            GetMethods            pingURL            GetAllMeters            GetAllServiceLocations            GetAllCustomers            GetCustomerByCustId            GetServiceLocationByAccountNo            GetServiceLocationByMeterNo            ReadingChangedNotification                  
4

2 に答える 2

2
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exslt="http://exslt.org/common"
    xmlns:response="http://www.url.org/Version_3.0"
    exclude-result-prefixes="exslt response"
>
    <xsl:output method="html" />

    <xsl:template match="/">
        <xsl:apply-templates select="//response:GetMethods" />
    </xsl:template>

    <xsl:template match="response:GetMethods">
        <ul>
            <xsl:apply-templates select="response:string" />
        </ul>
    </xsl:template>

    <xsl:template match="response:string">
        <li>
            <xsl:value-of select="." />
        </li>
    </xsl:template>
</xsl:stylesheet>

ノート:

  • 最初に、応答要素が含まれる名前空間を知らせる必要があります。この場合、名前空間 URI は ですhttp://www.url.org/Version_3.0。これをプレフィックスに割り当てて、XSLT で使用できるようにする必要があります。私はそのプレフィックスを呼び出すことにしましたresponse
  • 次に、XSLT の出力を制御する必要があります。これは、ルート ノード ( ) を照合し、処理のため<xsl:template match="/">に直接選択して次に進む場所を定義するのが最も簡単です。//response:GetMethods
  • テンプレートは、残りの変換response:GetMethodsを定義します。response:string
  • 最後に、結果の HTML に XML 名前空間プレフィックスが表示されないようにする必要があります。それexclude-result-prefixesがそうです。
  • ここでそれを参照してください: http://www.xmlplayground.com/kS1Cul
  • XSL の「call-template」と「apply-templates」の違いは何ですか?も読むことができます。<xsl:apply-templates>もう少し詳しく説明しました。
  • 一般に、書くことは避け、代わりに<xsl:for-each>選択する必要があります<xsl:apply-templates>
于 2013-01-09T20:07:28.463 に答える
1

指定された入力に pattern に一致する要素がありません"/GetMethods"。そのため、XML はおそらくデフォルトのテンプレートによって処理されています (ただし、XSLT の残りの部分を見なければわかりません)。デフォルトのテンプレートは、各要素のテキスト コンテンツを出力します。

これを修正するには、

  1. は最上位の要素ではないため/、一致パターンを で開始することはできません。GetMethodsを意味する場合、それはどこにでも出現する要素 (名前空間なし)//GetMethodsと一致します。<GetMethods>しかし、パターンも同様GetMethodsです。//冒頭は冗長です。
  2. の正しい名前空間を指定する必要がありますGetMethods。入力 XML によると、GetMethodsは URI が である名前空間にありますhttp://www.test.org/Version_3.0。したがって、その名前空間の名前空間プレフィックス (「test」など) を宣言し、一致パターンで使用する必要があります。

_

 <xsl:template match="test:GetMethods"
      xmlns:test="http://www.test.org/Version_3.0">...

xmlns:test="..."実際には、名前空間宣言 ( ) を最上位要素に移動する方がおそらくより実用的<xsl:stylesheet>です (まだ持っていない場合)。その後、test必要なスタイルシートのどこでも接頭辞を使用できます。

于 2013-01-09T19:26:15.423 に答える