Removing Names from XSLT

Hi, I am writing XSLT, and in this I am using the <xsl:copy-of> function. Now that it is running, the namesapce from xml is also copied. To remove this, I used a function like <xsl:copy-of select="$RootNode/Child" copy-namespaces="no"/> . But if the child has a few more children, then namespaces will appear in it. So who can tell me how I can remove this. Below is a snippet of my xslt and the XML that I use.

 <xsl:template match="/"> <xsl:element name="Parent"> <xsl:copy-of select="Child" copy-namespaces="no"/> </xsl:element> 

And XML:

 <Child> <GrandChild> <PhoneNumberType>DayPhone</PhoneNumberType> </GrandChild></Child> 

namespaces are not displayed in CustomerParty, but they are present in Child, but they are present in GrandChild.

+4
source share
2 answers

The copy-namespaces="no" attribute does not delete all namespace nodes - as specified in the XSLT 2.0 specification

>

If it takes the value no, then none of the namespace nodes is copied: however, the namespace nodes will still be created in the result tree, as the namespace fixing process requires: see 5.7.3 Correcting the namespace. This attribute affects all elements copied by this statement: both elements selected directly by the select statement and elements that are descendants of the nodes selected by the select statement.

Here is an example of how to get rid of all (optional) namespace nodes :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" 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:stylesheet> 

When this general conversion is applied to this XML document:

 <x:nums xmlns:x="my:x"> <x:num>01</x:num> <x:num>02</x:num> <x:num>03</x:num> <x:num>04</x:num> <x:num>05</x:num> <x:num>06</x:num> <x:num>07</x:num> <x:num>08</x:num> <x:num>09</x:num> <x:num>10</x:num> </x:nums> 

the desired, correct result is output:

 <nums> <num>01</num> <num>02</num> <num>03</num> <num>04</num> <num>05</num> <num>06</num> <num>07</num> <num>08</num> <num>09</num> <num>10</num> </nums> 

Please note :

  • The conversion is not specific to XSLT-2.0 and can also be used with XSLT 1.0.

  • Removing all nodes in the namespace is usually an unsafe process, because nodes from different namespaces are placed in "no namespace." In this process, some attributes may be lost, and the process is usually not reversible (not 1: 1).

+9
source

With this template, you can remove namespaces:

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

Also link to: http://www.devang-gandhi.net/blog/remove-namespace-attribute-from-xml-root-xslt/

Does this help you solve your problem?

Regards, Peter

0
source

Source: https://habr.com/ru/post/1347243/


All Articles