XSLT复制/复制而不保留名称空间

时间:2015-02-24 22:26:52

标签: xslt

您好我在默认的命名空间声明中有问题。输出xml元素附加默认命名空间。

输入XML看起来像

  <m:Request xmlns:m="http://www.NeededNamespace/1.4.0">
         <Details>
            <Records>50</Records>
            <Start>1</Start>
            <sortName>sortName</sortName>
         </Details>
         <search>
            <criteria>
               <comparative>
                  <Comparative>exactMatch</Comparative>
               </comparative>
               <name>STATECODE</name>
               <value>CO</value>
            </criteria>
            <criteria>
               <comparative>
                  <Comparative>exactMatch</Comparative>
               </comparative>
               <name>Version</name>
               <value>4.0</value>
            </criteria>
            <criteria>
               <comparative>
                  <Comparative>contains</Comparative>
               </comparative>
               <name>LEGALNAME</name>
               <value>Citizens State Bank</value>
            </criteria>
         </search>
      </m:Request>

XSLT看起来像

<xsl:stylesheet version="1.0" exclude-result-prefixes="t" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.NotRequirednamespace.com">
   <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>
   <!--Stylesheet to remove all namespaces from a document-->
   <!--NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix-->
   <!--Nodes that cannot have a namespace are copied as such-->
   <xsl:template match="/">
      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <soap:Header>
            <xys:To xmlns:xys="http://services.xys.com/framework/xysHeader/v2">
               <xys:version>9.0</xys:version>
               <xys:serviceName>DetailsManagement</xys:serviceName>
               <xys:QOS>DEFAULT</xys:QOS>
               <xys:operation>GetDetails</xys:operation>
            </xys:To>
            <ConsumerInfo xmlns="http://services.xys.com/framework/xysHeader/v2">
               <xysApplicationName>SAP</xysApplicationName>
               <xysCheckPermission>-1</xysCheckPermission>
               <xysConsumerPlatform>CS</xysConsumerPlatform>
               <xysLanguage>en</xysLanguage>
               <xysLocale>US</xysLocale>
               <xysLogLevel>false</xysLogLevel>
            </ConsumerInfo>
            <HeaderMetadata xmlns="http://services.xys.com/framework/xysHeader/v2">
               <metadataContractVersion>2.0</metadataContractVersion>
               <Id>414</Id>
               <Timestamp>2014-11-20T14:17:30.908-0500</Timestamp>
            </HeaderMetadata>
            <xys:favouriteSausage xmlns:xys="http://services.xys.com/framework/xysHeader/v2">cumberland</xys:favouriteSausage>
         </soap:Header>
         <soap:Body>
            <GetDetails xmlns="http://www.NeededNamespace/1.4.0">
               <Message id="" version="" bodyType="FS-XML" timeStampCreated="2015-10-11T10:15:25.9144403-04:00" sourceLogicalId="" xmlns="http://www.ibm.com/industries/xys">
                  <ACGroup bodyCategory="" TPMode="RespondError"/>
                  <COMMAND>

                     <xsl:apply-templates/>

                  </COMMAND>
               </Message>
            </GetDetails>
         </soap:Body>
      </soap:Envelope>
   </xsl:template>

   <!--template to copy elements-->
   <xsl:template match="*">
      <xsl:element name="{local-name()}" namespace="http://www.NeededNamespace/1.4.0">
         <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
   </xsl:template>

   <!--template to copy attributes-->
   <xsl:template match="@*">
      <xsl:attribute name="{local-name()}">
         <xsl:value-of select="."/>
      </xsl:attribute>
   </xsl:template>

   <!--template to copy the rest of the nodes-->
   <xsl:template match="comment() | text() | processing-instruction()">
      <xsl:copy/>
   </xsl:template>

</xsl:stylesheet>

我得到的输出XML是

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<xys:To xmlns:xys="http://services.xys.com/framework/xysHeader/v2">
<xys:version>9.0</xys:version>
<xys:serviceName>DetailsManagement</xys:serviceName>
<xys:QOS>DEFAULT</xys:QOS>
<xys:operation>GetDetails</xys:operation>
</xys:To>
<ConsumerInfo xmlns="http://services.xys.com/framework/xysHeader/v2">
<xysApplicationName>SAP</xysApplicationName>
<xysCheckPermission>-1</xysCheckPermission>
<xysConsumerPlatform>CS</xysConsumerPlatform>
<xysLanguage>en</xysLanguage>
<xysLocale>US</xysLocale>
<xysLogLevel>false</xysLogLevel>
</ConsumerInfo>
<HeaderMetadata xmlns="http://services.xys.com/framework/xysHeader/v2">
<metadataContractVersion>2.0</metadataContractVersion>
<Id>414</Id>
<Timestamp>2014-11-20T14:17:30.908-0500</Timestamp>
</HeaderMetadata>
<xys:favouriteSausage xmlns:xys="http://services.xys.com/framework/xysHeader/v2">cumberland</xys:favouriteSausage>
</soap:Header>
<soap:Body>
<GetDetails xmlns="http://www.NeededNamespace/1.4.0">
<Message xmlns="http://www.ibm.com/industries/xys" id="" version="" bodyType="FS-XML" timeStampCreated="2015-10-11T10:15:25.9144403-04:00" sourceLogicalId="">
<ACGroup bodyCategory="" TPMode="RespondError"/>
<COMMAND>
<Request xmlns="http://www.NeededNamespace/1.4.0">
<Details>
<Records>50</Records>
<Start>1</Start>
<sortName>sortName</sortName>
</Details>
<search>
<criteria>
<comparative>
<Comparative>exactMatch</Comparative>
</comparative>
<name>STATECODE</name>
<value>CO</value>
</criteria>
<criteria>
<comparative>
<Comparative>exactMatch</Comparative>
</comparative>
<name>Version</name>
<value>4.0</value>
</criteria>
<criteria>
<comparative>
<Comparative>contains</Comparative>
</comparative>
<name>LEGALNAME</name>
<value>Citizens State Bank</value>
</criteria>
</search>
</Request>
</COMMAND>
</Message>
</GetDetails>
</soap:Body>
</soap:Envelope>

但是在结果中我得到了元素

<Request xmlns="http://www.NeededNamespace/1.4.0">

但我希望结果标记如下所示

<Request>

我不想重新声明已在相同根标记中声明的命名空间。我已经尝试了我所知道的所有选项,并在最近几天尝试过。能帮到我吗?

1 个答案:

答案 0 :(得分:2)

input元素具有扩展名称(local-part =&#34; Request&#34 ;, namespace =&#34; http://www.NeededNamespace/1.4.0")。如果您不希望输出Request元素具有名称空间声明,那么您可能希望它与其父元素位于同一名称空间中,即您希望其扩展名称为(local-part =&#34) ; Request&#34;,namespace =&#34; http://www.ibm.com/industries/xys")。 xsl:copy或xsl:copy-of指令永远不会(甚至在2.0中)更改要复制的元素的扩展名称。因此,您无法使用xsl:copy或xsl:copy-of实现所需的输出。您需要使用<xsl:element name="{local-name()}" namespace="http://www.ibm.com/industries/xys"/>创建一个具有相同本地名称但与原始名称不同的命名空间的新元素。