XSLT转换可从所有元素中删除名称空间,然后在一个元素上指定名称空间

时间:2019-06-06 05:29:19

标签: xslt

我需要转换此XML并删除所有名称空间。

但是,仅当它是CompanyKey的子元素时,我才需要在名为Id的任何元素上使用新的命名空间。

此外,如果容易完成,请删除最高级别的元素。

我需要它才能在XSLT 2.0中工作。我想我可以通过单独的转换来完成大多数操作,但是最好将它们全部合并在一起。

如果将别名ns1更改为在输出中表示不同的名称空间是有问题的,则可以使用完全不同的名称,例如“ xyz”。

以下是源XML:

<tns:DynamicsGP_CreateSalesOrder_In xmlns:tns="http://schemas.datacontract.org/2004/07/Microsoft.Dynamics.Common" xmlns:ns1="http://schemas.datacontract.org/2004/07/Microsoft.Dynamics.GP" >
    <tns:salesOrder>
        <ns1:Key>
            <ns1:CompanyKey>
                <tns:Id>2</tns:Id>
            </ns1:CompanyKey>
            <ns1:Id>WorkOrder1</ns1:Id>
        </ns1:Key>
        <ns1:Date>2019-12-31</ns1:Date>
        <ns1:DocumentTypeKey>
            <ns1:CompanyKey>
                <tns:Id>2</tns:Id>
            </ns1:CompanyKey>
            <ns1:Id>STDWORD</ns1:Id>
            <ns1:Type>Order</ns1:Type>
        </ns1:DocumentTypeKey>
        <ns1:Lines>
            <ns1:SalesOrderLine>
                <ns1:ItemKey>
                    <ns1:CompanyKey>
                        <tns:Id>2</tns:Id>
                    </ns1:CompanyKey>
                    <ns1:Id>WPT</ns1:Id>
                </ns1:ItemKey>
            </ns1:SalesOrderLine>
        </ns1:Lines>
    </tns:salesOrder></tns:DynamicsGP_CreateSalesOrder_In>

所需的输出:

<salesOrder xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">
<Key>
    <CompanyKey>
        <ns1:Id>2</ns1:Id>
    </CompanyKey>
    <Id>WorkOrder1</Id>
</Key>
<Date>2019-12-31</Date>
<DocumentTypeKey>
    <CompanyKey>
        <ns1:Id>2</ns1:Id>
    </CompanyKey>
    <Id>STDWORD</Id>
    <Type>Order</Type>
</DocumentTypeKey>
<Lines>
    <SalesOrderLine>
        <ItemKey>
            <CompanyKey>
                <ns1:Id>2</ns1:Id>
            </CompanyKey>
            <Id>WPT</Id>
        </ItemKey>
    </SalesOrderLine>
</Lines></salesOrder>

这是我目前的尝试

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">
<xsl:output method="xml" indent="yes" />

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

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

<xsl:template match="Id">
    <xsl:element name="{name()}" namespace="ns1">
        <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

我相信这应该对您有用:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="http://schemas.datacontract.org/2004/07/Microsoft.Dynamics.Common"
xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01"
exclude-result-prefixes="tns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- remove all namespaces -->
<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<!-- remove top wrapper -->
<xsl:template match="/tns:DynamicsGP_CreateSalesOrder_In">
    <xsl:apply-templates/>
</xsl:template>

<!-- declare new namespace on root element -->
<xsl:template match="tns:salesOrder">
     <salesOrder xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">
        <xsl:apply-templates/>
    </salesOrder>
</xsl:template>

<!-- change namespace on some elements -->
<xsl:template match="tns:Id">
     <ns1:Id>
        <xsl:apply-templates/>
     </ns1:Id>
</xsl:template>

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/bnnZWE/1


请注意,第三个模板是可选的,仅用于装饰目的;如果将其删除,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<salesOrder>
   <Key>
      <CompanyKey>
         <ns1:Id xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">2</ns1:Id>
      </CompanyKey>
      <Id>WorkOrder1</Id>
   </Key>
   <Date>2019-12-31</Date>
   <DocumentTypeKey>
      <CompanyKey>
         <ns1:Id xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">2</ns1:Id>
      </CompanyKey>
      <Id>STDWORD</Id>
      <Type>Order</Type>
   </DocumentTypeKey>
   <Lines>
      <SalesOrderLine>
         <ItemKey>
            <CompanyKey>
               <ns1:Id xmlns:ns1="http://schemas.microsoft.com/dynamics/2006/01">2</ns1:Id>
            </CompanyKey>
            <Id>WPT</Id>
         </ItemKey>
      </SalesOrderLine>
   </Lines>
</salesOrder>

从语义上讲与请求的输出相同。