添加soap标头 - 更新节点 - 复制文档

时间:2009-07-17 22:46:55

标签: xslt templates apply-templates

我正在尝试将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>

我一直在阅读教程,但遇到了解决模板问题以及在哪里实施它们的麻烦。

1 个答案:

答案 0 :(得分: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",有些体操不是那么重要,但我尽量让它与你的输出尽可能匹配。

相关问题