0

我有这样的xml文件:

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

其中标记“文件名”是另一个 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>

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

我想得到:

<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>
4

1 回答 1

0

使用 copy-of 后,无法更改任何内容。

因此:对第二个文件
使用恒等变换
使第一个文件的内容可作为变量访问(如果您需要其中的多个值。如果只需要更改一个值,您也可以使用变量 providerMessageId)。
以及应根据第一个文件更改的值的特殊模板。(例如 ns2: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>
于 2013-05-21T13:24:41.140 回答