2013-05-21 95 views
0

我的XML文件是这样的:编辑两个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> 

我想通过值从这个最后的代码示例替换“KEYB”标签值“KEYB”标签( “keyBkeyBkeyB”),如果其子标签'specificationName'等于Workforce_Recherche的值。我可以使用身份模板吗?我有几个标签'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> 
+0

你要保持你的第二个XML中生成的输出,因为它是除了一些在ns2中更改:messageID/value元素? –

+0

我想从第一个xml的MessageId标记值中替换ns2:messageID/value。一般来说,我想知道如何编辑复制xml标记和值:添加新标记,更改值等。 – turlife

+0

您能否为预期的输出xml添加示例。 –

回答

0

使用复制的没有办法改变任何事情之后。

因此:
对第二个文件使用标识变换
将第一个文件的内容作为变量存取(如果您需要多个值,如果只需更改一个值,您也可以使用变量providerMessageId)。
和值的特殊模板,应根据第一个文件进行更改。 (如NS2: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="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> 
+0

如果我运行此代码,它只返回我' 9133235059913398501_9133235059913398860'。我希望我可以复制我的XML并进行编辑((谢谢,我将尝试使用自己的身份转换 – turlife

+0

我看不出你的意思。输出是文件2的内容,但ns2:messageID /值是从MessageId表单文件1。 –

+0

生成的输出是' 9133235059913398501_9133235059913398860'我的例子 – turlife