Edit two xml files

I have an xml file:

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

Where the tag 'filename' is the path to another XML file. An example of this file:

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

Now I need to create an xsl file that will copy the second xml file and change the messageID value from the first XML file. Something like that:

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

How to change the value of xml copy file?

Change The xml output I want to get is:

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

Second update:

 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> 

I want to replace the value of the "keyB" tag from this last code example with the "keyB" ("keyBkeyBkeyB") tag from the first xml example if its child tag "specificationName" is equal to the value of Workforce_Recherche. Can I do this using an identity template? I have several keywords 'keyB' and 'specificationName', but only one value needs to be changed with the properties described above.

I want to receive:

 <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> 
0
source share
1 answer

After using the copy, it is impossible to change any thing.

In this way:
For the second file, use identity conversion .
Make the contents of the first file available as a variable (if you need more than one value from this. If you need to change only one value, you can also use the providerMessageId variable).
And special patterns for values ​​that need to be changed depending on the first file. (For example, ns2: messageID / common: value)

Try the following:

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

Update generated output:

 <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> 
0
source

Source: https://habr.com/ru/post/945421/


All Articles