嗨,我有一个xml,我会用所有的atributes和命名空间复制xml的顶部根元素,并在同一个xml的UserArea标记下复制它。我的xml是
<?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&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
<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>
我在UserArea中得到了另一个属性标记,但是使用了整个xml本身。相反,我会有一个xml,在UserArea标记中的第三个属性标记中,我只有SyncShipment的根节点,如下所示
<?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&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>发布于 2015-03-16 14:32:00
获取整个xml的原因是因为您正在执行一个xsl:copy-of。我要做的是使用一个经过修改的模板来输出根元素的副本,并对属性执行xsl:apply-templates。
XML输入
<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&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>XSLT1.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输出
<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&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>https://stackoverflow.com/questions/29051575
复制相似问题