简体   繁体   中英

copy element with attribute and namespace xslt

Hi i have an xml and i would copy the top root element of the xml with all atributes and namespace and copy it under the UserArea tag of the same xml. my xml is this

<?xml version="1.0" encoding="UTF-8"?>
<SyncShipment xmlns="http://schema.infor.com/InforOAGIS/2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2  http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" 
versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US">
<ApplicationArea>
    <Sender>
        <LogicalID schemeVersionID="VMSHPENT:7.0.0.156:AC7726D2">lid://infor.visual.visual</LogicalID>
        <ComponentID>Visual</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2015-03-04T09:25:12.107Z</CreationDateTime>
    <BODID location="Site~Gouda" schemeAgencyName="Visual">infor-nid:infor:NKFEX:Site~Gouda:Shipper~S14-01709~1:?Shipment&#38;verb=Sync</BODID>
</ApplicationArea>
<DataArea>
    <ShipmentHeader>
        <UserArea>
                <Property>
                    <NameValue name="visual.UserDefined1" type="String"/>
                </Property>
                <Property>
                    <NameValue name="visual.UserDefined2" type="String"/>
                </Property>
                </UserArea>     
        </ShipmentHeader>
    </DataArea>
</SyncShipment>

if i apply this xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schema.infor.com/InforOAGIS/2"   xmlns:a="http://schema.infor.com/InforOAGIS/2" exclude-result-prefixes="a" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="*|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="//a:ShipmentHeader/a:UserArea/a:Property[last()]">
    <xsl:copy-of select="."/>
    <xsl:variable name="apparea" select="//a:SyncShipment"/>
    <Property>
    <NameValue name="app" type="xml">
<xsl:copy-of select="$apparea"/>
    </NameValue>
    </Property>
</xsl:template>

i got another property tag within the UserArea but with the entire xml itself. on the contrary i would have an xml where in the 3rd property tag in the UserArea tag i have just the root node of the SyncShipment like this

<?xml version="1.0"?>
<SyncShipment xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US">
  <ApplicationArea>
    <Sender>
      <LogicalID schemeVersionID="VMSHPENT:7.0.0.156:AC7726D2">lid://infor.visual.visual</LogicalID>
      <ComponentID>Visual</ComponentID>
      <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2015-03-04T09:25:12.107Z</CreationDateTime>
    <BODID location="Site~Gouda" schemeAgencyName="Visual">infor-nid:infor:NKFEX:Site~Gouda:Shipper~S14-01709~1:?Shipment&amp;verb=Sync</BODID>
  </ApplicationArea>
  <DataArea>
    <ShipmentHeader>
      <UserArea>
        <Property>
          <NameValue name="visual.UserDefined1" type="String"/>
        </Property>
        <Property>
          <NameValue name="visual.UserDefined2" type="String"/>
        </Property>
        <Property>
          <NameValue name="app" type="xml">
            <SyncShipment xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US">
          </NameValue>
        </Property>
      </UserArea>
    </ShipmentHeader>
  </DataArea>
</SyncShipment>

The reason you're getting the entire xml is because you're doing an xsl:copy-of . What I would do is use a moded template that would output a copy of the root element and do an xsl:apply-templates on the attributes.

XML Input

<SyncShipment xmlns="http://schema.infor.com/InforOAGIS/2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2  http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" 
    versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US">
    <ApplicationArea>
        <Sender>
            <LogicalID schemeVersionID="VMSHPENT:7.0.0.156:AC7726D2">lid://infor.visual.visual</LogicalID>
            <ComponentID>Visual</ComponentID>
            <ConfirmationCode>OnError</ConfirmationCode>
        </Sender>
        <CreationDateTime>2015-03-04T09:25:12.107Z</CreationDateTime>
        <BODID location="Site~Gouda" schemeAgencyName="Visual">infor-nid:infor:NKFEX:Site~Gouda:Shipper~S14-01709~1:?Shipment&#38;verb=Sync</BODID>
    </ApplicationArea>
    <DataArea>
        <ShipmentHeader>
            <UserArea>
                <Property>
                    <NameValue name="visual.UserDefined1" type="String"/>
                </Property>
                <Property>
                    <NameValue name="visual.UserDefined2" type="String"/>
                </Property>
            </UserArea>     
        </ShipmentHeader>
    </DataArea>
</SyncShipment>

XSLT 1.0

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:i="http://schema.infor.com/InforOAGIS/2">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

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

    <xsl:template match="/*" mode="single">
        <xsl:element name="Property" namespace="http://schema.infor.com/InforOAGIS/2">
            <xsl:element name="NameValue" namespace="http://schema.infor.com/InforOAGIS/2">
                <xsl:attribute name="name">app</xsl:attribute>
                <xsl:attribute name="type">xml</xsl:attribute>
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                </xsl:copy>
            </xsl:element>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML Output

<SyncShipment xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2  http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US">
   <ApplicationArea>
      <Sender>
         <LogicalID schemeVersionID="VMSHPENT:7.0.0.156:AC7726D2">lid://infor.visual.visual</LogicalID>
         <ComponentID>Visual</ComponentID>
         <ConfirmationCode>OnError</ConfirmationCode>
      </Sender>
      <CreationDateTime>2015-03-04T09:25:12.107Z</CreationDateTime>
      <BODID location="Site~Gouda" schemeAgencyName="Visual">infor-nid:infor:NKFEX:Site~Gouda:Shipper~S14-01709~1:?Shipment&amp;verb=Sync</BODID>
   </ApplicationArea>
   <DataArea>
      <ShipmentHeader>
         <UserArea>
            <Property>
               <NameValue name="visual.UserDefined1" type="String"/>
            </Property>
            <Property>
               <NameValue name="visual.UserDefined2" type="String"/>
            </Property>
            <Property>
               <NameValue name="app" type="xml">
                  <SyncShipment xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2  http://schema.infor.com/2.6.3/InforOAGIS/BODs/Developer/SyncShipment.xsd" versionID="2.6.3" releaseID="9.2" systemEnvironmentCode="Production" languageCode="en-US"/>
               </NameValue>
            </Property>
         </UserArea>
      </ShipmentHeader>
   </DataArea>
</SyncShipment>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM