复制xml +添加根节点,并删除中间的一些父节点



按照Martin的要求(复制xml +删除一些位置节点),我为我的问题创建了一个新问题。

事情是。输入是上一题的输出。问题是,我需要添加另一个根节点。在xml中,我需要删除OtherDoc节点。在目标中,它们只期望内部节点。

输入

<ns0:parentnode xmlns:ns0="http://schemas.microsoft.com/namespace1" xmlns:s0="http://schemas.microsoft.com/BizTalk/2003/aggschema">
   <ns0:childNode attribute1="test" attribute2="test">
      <levelA xmlns="http://schemas.microsoft.com/namespace2"/>
   </ns0:childNode>
   <ns0:childNode2 Name="childNode2">
      <levelA xmlns="http://schemas.microsoft.com/namespace3"/>
   </ns0:childNode2>
   <ns0:ChildNode3 Name="ChildNode3" id="2015-10-08-12.07.37.218960">
      <ns0:levelA Name="levelA">
         <ns0:data Name="Data" id="123456" type="A"/>
         <ns0:data Name="Data" id="654321" type="B"/>
         <ns0:levelAA id="910038265">
            <ns0:repeatingItem id="910568755">
               <ns0:comm Name="Communication" id="910041726" numberOfDocuments="3">
                  <ns0:group Name="Group" id="12">
                     <ns0:doc xmlns:s7="http://Schemas.RepeatingItemMessageInformation" id="123" archived="0"/>
                     <ns0:doc xmlns:s7="http://Schemas.RepeatingItemMessageInformation" id="321" archived="0"/>
                  </ns0:group>
               </ns0:comm>
            </ns0:repeatingItem>
         </ns0:levelAA>
         <ns0:doc id="123">
            <ns0:externalDoc content="base64string"/>
         </ns0:doc>
         <ns0:doc id="321">
            <ns0:externalDoc content="base64string"/>
         </ns0:doc>
      </ns0:levelA>
   </ns0:ChildNode3>
</ns0:parentnode>

输出
<ns0:First xmlns:ns0="http://schemas.microsoft.com/namespace1">
    <ns0:parentnode xmlns:ns0="http://schemas.microsoft.com/namespace1" xmlns:s0="http://schemas.microsoft.com/BizTalk/2003/aggschema">
       <ns0:childNode attribute1="test" attribute2="test">
          <levelA xmlns="http://schemas.microsoft.com/namespace2"/>
       </ns0:childNode>
       <ns0:childNode2 Name="childNode2">
          <levelA xmlns="http://schemas.microsoft.com/namespace3"/>
       </ns0:childNode2>
       <ns0:ChildNode3 Name="ChildNode3" id="2015-10-08-12.07.37.218960">
          <ns0:levelA Name="levelA">
             <ns0:data Name="Data" id="123456" type="A"/>
             <ns0:data Name="Data" id="654321" type="B"/>
             <ns0:levelAA id="910038265">
                <ns0:repeatingItem id="910568755">
                   <ns0:comm Name="Communication" id="910041726" numberOfDocuments="3">
                      <ns0:group Name="Group" id="12">
                         <ns0:doc xmlns:s7="http://Schemas.RepeatingItemMessageInformation" id="123" archived="0"/>
                         <ns0:doc xmlns:s7="http://Schemas.RepeatingItemMessageInformation" id="321" archived="0"/>
                      </ns0:group>
                   </ns0:comm>
                </ns0:repeatingItem>
             </ns0:levelAA>
             <ns0:externalDoc content="base64string"/>
             <ns0:externalDoc content="base64string"/>
          </ns0:levelA>
       </ns0:ChildNode3>
    </ns0:parentnode>
</ns0:First>

所以添加rootnamespace不是问题。我还可以更改名称空间。因为也源& &;目标命名空间不同。我唯一需要做的就是删除OtherDoc。而不会失去复制完整消息的功能。因为它们比这个例子xml

要多得多XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
                exclude-result-prefixes="msxsl var s0 " version="1.0"
                xmlns:ns0="http://schemas.microsoft.com/namespace1"
                xmlns:s0="http://schemas.microsoft.com/BizTalk/2003/aggschema"
                xmlns:s7="http://Schemas.RepeatingItemMessageInformation"
                xmlns:ns1="http://schemas.microsoft.com/namespace2">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" indent="yes">     
<xsl:template match="/">
    <xsl:apply-templates select="/ns0:parentnode" />
  </xsl:template>
  <xsl:template match="/ns0:parentnode">
    <ns1:First>
      <ns1:parentnode>
        <xsl:for-each select="ns0:ChildNode3">
        <ns1:ChildNode3>
            <xsl:copy-of select="./@*" />
            <xsl:apply-templates select="./*" />
        </ns1:ChildNode3>
        </xsl:for-each>
      </ns1:parentnode>
    </ns0:First>
  </xsl:template>
  <xsl:template match="ns0:*">
    <xsl:element name="ns1:{local-name()}">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy-of select="."/>
  </xsl:template>  
</xsl:stylesheet>

如果您想要删除doc节点,但仍然输出子节点(在不同的名称空间中),您只需要添加另一个模板来显式匹配doc,它将通过处理其子节点继续运行,但不输出节点本身。

将此模板添加到XSLT

 <xsl:template match="ns0:doc[ns0:externalDoc]">
     <xsl:apply-templates select="node()"/>
 </xsl:template>

这将只匹配具有子externalDocdoc元素。

或者,您可以在LevelA元素中只匹配doc元素。你也可以试试这个,在这个例子中:

 <xsl:template match="ns0:levelA/ns0:doc">
     <xsl:apply-templates select="node()"/>
 </xsl:template>

最新更新