问题
I need to transform this XML and remove all the namespaces.
However I need a new namespace on any element named Id only if it is child of CompanyKey.
Additionally, if easily done, remove the highest level element.
I need it to work in XSLT 2.0. I think I can do most of this with separate transforms but it would be best if it all worked in one.
If changing the alias ns1 to mean a different namespace in the output is problematic, I could use a completely different one like 'xyz'.
Here is the source 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>
Desired output:
<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>
This is my current attempt:
<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:
I believe this should work for you:
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>
Demo: https://xsltfiddle.liberty-development.net/bnnZWE/1
Note that the third template is optional and for cosmetic purposes only; if you remove it, the result will be:
<?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>
which is semantically identical to the requested output.
来源:https://stackoverflow.com/questions/56471362/xslt-transform-to-remove-namespaces-from-all-elements-then-specify-namespace-on