通过XSLT1.0将节点复制到XML中的其他节点时,如何删除命名空间?

时间:2019-02-01 05:52:23

标签: xslt-1.0

我是XSL的新手,我正尝试使用XSLT将XML的一个节点复制到同一XML中的另一个节点。我可以按预期方式转换文件,但是XMLNS属性已添加到不需要的目标位置。

我已经使用XSLT2.0尝试了所有copy-namespaces ='no'选项,但是那不起作用。另外,我不能在XSLT中使用名称空间的前缀,并使用排除名称空间来避免xmlns,因为传入的XML文件是动态的并且名称空间不断变化。我不能在XSLT中将所有名称空间都声明为前缀。

传入XML:

<?xml version="1.0" encoding="UTF-8"?>
<PropertySet>
    <Message>
        <NotificationHeader>
            <BusinessId></BusinessId>
            <CorrelationId>0201201916:21:24CKG3N</CorrelationId>
            <SourceName></SourceName>
            <SourceId></SourceId>
            <EventType></EventType>
            <SecurityIdentifierId></SecurityIdentifierId>
            <ClientRequestId></ClientRequestId>
            <TargetId>ESB</TargetId>
            <EventTime></EventTime>
            <RequestUser></RequestUser>
        </NotificationHeader>
        <EventList>
            <Event>
                <PayLoad>
                    <ListOfActionIo xmlns="http://www.test.com/IO">
                        <Action>
                            <ActivityId>4-309C7WV</ActivityId>
                            <ActivitySRId></ActivitySRId>
                            <ActivityTemplateId></ActivityTemplateId>
                            <ActivityUID>4-309C7WV</ActivityUID>
                            <Category> Notification</Category>
                            <Comment></Comment>
                            <Type>Action</Type>
                            <ListOfDestination>
                                <Destination>
                                    <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
                                </Destination>
                                <Destination>
                                    <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
                                </Destination>
                            </ListOfDestination>
                        </Action>
                    </ListOfActionIo>
                </PayLoad>
            </Event>
        </EventList>
        <DestinationList>
            <Destination>
                <DestinationName></DestinationName>
                <DestinationId></DestinationId>
            </Destination>
        </DestinationList>
    </Message>
</PropertySet>

XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//*[name()='Message']/*[name()='DestinationList']//*[name()='Destination']"></xsl:template>

    <!--Copy the destination from one node to another node-->

    <xsl:template match="//*[name()='Message']/*[name()='DestinationList']">
        <xsl:copy>
            <xsl:copy-of select="//*[name()='Message']/*[name()='EventList']//*[name()='Destination']"/>
        </xsl:copy>
    </xsl:template>

    <!--Remove the original node from where the destination was copied-->

    <xsl:template match="//*[name()='Message']/*[name()='EventList']//*[name()='ListOfDestination']"></xsl:template>


</xsl:stylesheet>

输出:

    <PropertySet>
   <Message>
      <NotificationHeader>
         <BusinessId/>
         <CorrelationId>0201201916:21:24CKG3N</CorrelationId>
         <SourceName/>
         <SourceId/>
         <EventType/>
         <SecurityIdentifierId/>
         <ClientRequestId/>
         <TargetId>ESB</TargetId>
         <EventTime/>
         <RequestUser/>
      </NotificationHeader>
      <EventList>
         <Event>
            <PayLoad>
               <ListOfActionIo xmlns="http://www.test.com/IO">
                  <Action>
                     <ActivityId>4-309C7WV</ActivityId>
                     <ActivitySRId/>
                     <ActivityTemplateId/>
                     <ActivityUID>4-309C7WV</ActivityUID>
                     <Category> Notification</Category>
                     <Comment/>
                     <Type>Action</Type>
                  </Action>
               </ListOfActionIo>
            </PayLoad>
         </Event>
      </EventList>
      <DestinationList>
         <Destination xmlns="http://www.test.com/IO">
            <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
         </Destination>
         <Destination xmlns="http://www.test.com/IO">
            <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
         </Destination>
      </DestinationList>
   </Message>
</PropertySet>

预期输出: 复制节点上没有命名空间。

 <PropertySet>
   <Message>
      <NotificationHeader>
         <BusinessId/>
         <CorrelationId>0201201916:21:24CKG3N</CorrelationId>
         <SourceName/>
         <SourceId/>
         <EventType/>
         <SecurityIdentifierId/>
         <ClientRequestId/>
         <TargetId>ESB</TargetId>
         <EventTime/>
         <RequestUser/>
      </NotificationHeader>
      <EventList>
         <Event>
            <PayLoad>
               <ListOfActionIo xmlns="http://www.test.com/IO">
                  <Action>
                     <ActivityId>4-309C7WV</ActivityId>
                     <ActivitySRId/>
                     <ActivityTemplateId/>
                     <ActivityUID>4-309C7WV</ActivityUID>
                     <Category> Notification</Category>
                     <Comment/>
                     <Type>Action</Type>
                  </Action>
               </ListOfActionIo>
            </PayLoad>
         </Event>
      </EventList>
      <DestinationList>
         <Destination>
            <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
         </Destination>
         <Destination>
            <DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
         </Destination>
      </DestinationList>
   </Message>
</PropertySet> 

1 个答案:

答案 0 :(得分:0)

您在问错问题。您不想“删除名称空间”-您想要更改已转换元素的名称空间。这类似于重命名元素。

现在,由于您的示例令人困惑(且时间太长),请考虑以下事项:

XML

<root>
    <colors>
        <color>red</color>
        <color>blue</color>
    </colors>
    <shapes xmlns="some-unknown-namespace">
        <shape>circle</shape>
        <shape>square</shape>
    </shapes>
    <sizes>
        <size>small</size>
        <size>large</size>
    </sizes>
</root>

要将shapes(及其继承者的继承人)的名称空间更改为无名称空间,而无需事先知道原始名称空间将是什么,可以执行以下操作:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="*[ancestor-or-self::*[name()='shapes']]">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <colors>
    <color>red</color>
    <color>blue</color>
  </colors>
  <shapes>
    <shape>circle</shape>
    <shape>square</shape>
  </shapes>
  <sizes>
    <size>small</size>
    <size>large</size>
  </sizes>
</root>

已添加:

  

让修改代码以将形状移动到尺寸并删除形状节点。

好的,我们这样做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="sizes">
    <xsl:copy>
        <xsl:apply-templates/>
        <!-- move the shapes into sizes -->
        <xsl:apply-templates select="../*[name()='shapes']/*"/>
    </xsl:copy>
</xsl:template>

<!-- remove the shapes element -->
<xsl:template match="*[name()='shapes']"/>

<xsl:template match="*[ancestor::*[name()='shapes']]">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <colors>
    <color>red</color>
    <color>blue</color>
  </colors>
  <sizes>
    <size>small</size>
    <size>large</size>
    <shape>circle</shape>
    <shape>square</shape>
  </sizes>
</root>