首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有属性和命名空间的复制元素xslt

具有属性和命名空间的复制元素xslt
EN

Stack Overflow用户
提问于 2015-03-14 16:52:44
回答 1查看 836关注 0票数 0

嗨,我有一个xml,我会用所有的atributes和命名空间复制xml的顶部根元素,并在同一个xml的UserArea标记下复制它。我的xml是

代码语言:javascript
复制
<?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>

如果我应用这个xslt

代码语言:javascript
复制
<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的根节点,如下所示

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2015-03-16 14:32:00

获取整个xml的原因是因为您正在执行一个xsl:copy-of。我要做的是使用一个经过修改的模板来输出根元素的副本,并对属性执行xsl:apply-templates

XML输入

代码语言:javascript
复制
<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>

XSLT1.0

代码语言:javascript
复制
<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输出

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29051575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档