您好我有一个xml,我将使用所有属性和命名空间复制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标签的第3个属性标签中,我只有像这样的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>
答案 0 :(得分:0)
您获得整个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>
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输出
<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>