1

ドキュメントに Soap ヘッダーを追加し、最初の RS ノードを次のように更新しようとしています。

 <Rs xmlns="http://tempuri.org/schemas">

ドキュメントノードの残りをコピーしながらすべて。私の実際の例では、RS 親ノード内により多くのノードがあるため、ある種のディープ コピーを使用したソリューションを探しています。

<-- this is the data which needs transforming -->

<Rs>
   <ID>934</ID>
   <Dt>995116</Dt>
   <Date>090717180408</Date>
   <Code>9349</Code>
   <Status>000</Status>
</Rs>


 <-- Desired Result -->

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">  
<SOAP-ENV:Body>
  <Rs xmlns="http://tempuri.org/schemas">
    <ID>934</ID>
    <Dt>090717180408</Dt>
    <Code>9349</Code>
    <Status>000</Status>    
    </Rs>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<-- this is my StyleSheet. it's not well formed so i can't exexute it-->

<?xml version="1.0"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
    <SOAP-ENV:Envelope
                 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body>
                    <xsl:apply-templates  select = "Rs">
                    </xsl:apply-templates>
                    <xsl:copy-of select="*"/>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
</xsl:template>
<xsl:template match ="Rs">
    <Rs xmlns="http://tempuri.org/schemas">
</xsl:template>
</xsl:stylesheet>

チュートリアルを読んでいますが、テンプレートとそれらを実装する場所に頭を悩ませています。

4

1 に答える 1

1

xmlns単なる別の属性ではなく、名前空間の変更を示します。だから少しトリッキーです。これを試して:

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
            <SOAP-ENV:Body>
                <xsl:apply-templates select="Rs"/>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
    </xsl:template>
    <xsl:template match="node()">
        <xsl:element name="{local-name(.)}" namespace="http://tempuri.org/schemas">
            <!-- the above line is the tricky one. We can't copy an element from -->
            <!-- one namespace to another, but we can create a new one in the -->
            <!-- proper namespace. -->
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()|*"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:if test="normalize-space(.) != ''">
            <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

一部の体操は、使用しない場合はそれほど重要ではありませんindent="yes"が、出力にできるだけ一致するようにしました。

于 2009-07-18T04:35:50.897 に答える