编辑两个xml文件

时间:2013-05-21 12:08:09

标签: xml xslt

我有这样的xml文件:

 <CompletePPCallback>
 <MessageId>9133235059913398501</MessageId>
 <keyB>keyBkeyBkeyB</keyB>
 <filename>c:\temp\log\SMKFFcompleteProductionPlan.xml</filename>
 </CompletePPCallback>

标记&#39;文件名&#39;是另一个xml文件的路径。此文件的示例:

 <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
        <soapenv:Body>
            <ns2:completeProductionPlan
                xmlns='http://ServiceManagement/OIS_Services_v01.00/common'
xmlns:ns2='http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types'>
                <ns2:messageID>
                    <value>9133235059913398501_9133235059913398860</value>
                </ns2:messageID>
            </ns2:completeProductionPlan>
        </soapenv:Body>
    </soapenv:Envelope>

现在我需要创建xsl文件,该文件将复制第二个xml文件并从第一个xml文件更改messageID的值。像这样:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types"
    xmlns:common="http://ServiceManagement/OIS_Services_v01.00/common"
    exclude-result-prefixes="ns2 common">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />


    <xsl:variable name="providerXMLpath">
        <xsl:value-of select="//filename" />
    </xsl:variable>

    <xsl:variable name="providerMessageId">
        <xsl:value-of select="document($providerXMLpath)//ns2:messageID/common:value" />
    </xsl:variable>

    <copy>
        <xsl:copy-of select="document($providerXMLpath)"/> 
    </copy>

    <xsl:template match="/">
        <FirstTag>
            <xsl:choose>
                <xsl:when test='$providerMessageId=//MessageId'>
                    <tag>
                        <xsl:text>Equal</xsl:text>
                    </tag>
                </xsl:when>
                <xsl:otherwise>
                    <tag>
                        <xsl:text>Different</xsl:text>
                    </tag>
                </xsl:otherwise>
            </xsl:choose>
        </FirstTag>
    </xsl:template>
</xsl:stylesheet>

如何更改复制xml文件的值?

编辑。 输出xml我想要的内容:

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
            <soapenv:Body>
                <ns2:completeProductionPlan
                    xmlns='http://ServiceManagement/OIS_Services_v01.00/common'
    xmlns:ns2='http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types'>
                    <ns2:messageID>
                        <value>9133235059913398501</value>
                    </ns2:messageID>
                </ns2:completeProductionPlan>
            </soapenv:Body>
        </soapenv:Envelope>

第二次更新:

Now I have this xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns2:completeProductionPlan
            xmlns="http://ServiceManagement/OIS_Services_v01.00/common"
            xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types">
            <ns2:messageID>
                <value>9133235059913398501_9133235059913398860</value>
            </ns2:messageID>
            <ns2:productionPlan>
                <entityKey>
                    <keyA>9c4332b3-e60d-466b-9ab8-1187e98582e9</keyA>
                </entityKey>
                <state>
                    <value>readyForCompletion</value>
                </state>
                <service>
                    <entityKey>
                        <keyB>f51c2436-1a8e-4411-9a95-4eed04bcb412</keyB>
                    </entityKey>
                    <specification>
                        <specificationName>Access_Line</specificationName>
                        <specificationID>Access_Line</specificationID>
                        <characteristic>
                            <characteristicID>SpecificationType</characteristicID>
                            <characteristicValue>RFS</characteristicValue>
                        </characteristic>
                    </specification>
                </service>
                <service>
                    <entityKey>
                        <keyB>29b81be7-94e0-47e7-82f7-884ad57755d7</keyB>
                    </entityKey>
                    <specification>
                        <specificationName>Workforce_Recherche</specificationName>
                        <specificationID>Workforce_Recherche</specificationID>
                        <characteristic>
                            <characteristicID>SpecificationType</characteristicID>
                            <characteristicValue>RFS</characteristicValue>
                        </characteristic>
                    </specification>    
                </service>
            </ns2:productionPlan>
        </ns2:completeProductionPlan>
    </soapenv:Body>
</soapenv:Envelope>

我想替换&#39; keyB&#39;最后一个代码示例中的值为&#39; keyB&#39;第一个xml示例中的tag(&#34; keyBkeyBkeyB&#34;),如果其子标记&#39; specificationName&#39;等于Workforce_Recherche值。我可以用身份模板吗?我有几个标签&#39; keyB&#39;和&#39; specificationName&#39;但需要只使用上述属性更改一个值。

我想得到:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns2:completeProductionPlan
            xmlns="http://ServiceManagement/OIS_Services_v01.00/common"
            xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types">
            <ns2:messageID>
                <value>9133235059913398501</value>
            </ns2:messageID>
            <ns2:productionPlan>
                <entityKey>
                    <keyA>9c4332b3-e60d-466b-9ab8-1187e98582e9</keyA>
                </entityKey>
                <state>
                    <value>readyForCompletion</value>
                </state>
                <service>
                    <entityKey>
                        <keyB>f51c2436-1a8e-4411-9a95-4eed04bcb412</keyB>
                    </entityKey>
                    <specification>
                        <specificationName>Access_Line</specificationName>
                        <specificationID>Access_Line</specificationID>
                        <characteristic>
                            <characteristicID>SpecificationType</characteristicID>
                            <characteristicValue>RFS</characteristicValue>
                        </characteristic>
                    </specification>
                </service>
                <service>
                    <entityKey>
                        <keyB>keyBkeyBkeyB</keyB>
                    </entityKey>
                    <specification>
                        <specificationName>Workforce_Recherche</specificationName>
                        <specificationID>Workforce_Recherche</specificationID>
                        <characteristic>
                            <characteristicID>SpecificationType</characteristicID>
                            <characteristicValue>RFS</characteristicValue>
                        </characteristic>
                    </specification>    
                </service>
            </ns2:productionPlan>
        </ns2:completeProductionPlan>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

使用副本后,无法改变任何东西。

因此:
对第二个文件使用标识转换 使第一个文件的内容可以作为变量访问(如果您需要多个值。如果只需要更改一个值,您也可以使用变量providerMessageId)。
以及值的特殊模板,应根据第一个文件进行更改。 (例如:messageID / common:value)

试试这个:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types"
    xmlns:common="http://ServiceManagement/OIS_Services_v01.00/common"
    exclude-result-prefixes="ns2 common">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />

    <xsl:variable name="providerXMLpath">
        <xsl:value-of select="//filename" />
    </xsl:variable>

    <xsl:variable name="completePPCallback" select="CompletePPCallback" />

    <xsl:variable name="providerMessageId">
        <xsl:value-of select="document($providerXMLpath)//ns2:messageID/common:value" />
    </xsl:variable>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ns2:messageID/common:value">
        <xsl:copy>
            <xsl:value-of select="$completePPCallback/MessageId"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates select="document($providerXMLpath)/*" />
    </xsl:template>
</xsl:stylesheet>

更新生成的输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns2:completeProductionPlan xmlns="http://ServiceManagement/OIS_Services_v01.00/common" 
                                    xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types">
            <ns2:messageID>
                <value>9133235059913398501</value>
            </ns2:messageID>
        </ns2:completeProductionPlan>
    </soapenv:Body>
</soapenv:Envelope>