嗨,我有一个 xml,我会复制带有所有属性和命名空间的 xml 的顶部根元素,并将其复制到同一 xml 的用户区域标签下。我的 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>
我在用户区中得到了另一个属性标签,但带有整个 XML 本身。 相反,我会有一个 XML,在用户区标签的第三个属性标签中,我只有 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>
您获得整个 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>