0
votes

Source XML:

<books xmlns="www.flipkart.com" xmlns:sport="www.flipkart.com/sports">
    <book xmlns:sport="www.flipkart.com/sports" xmlns:bio="www.flipkart.com/sports/bio">
        <name>Open</name>
        <author>Agassi</author>
    </book>
    <book xmlns:sport="www.flipkart.com/sports" xmlns:bio="www.flipkart.com/sports/bio">
        <name>Bounce</name>
        <author>Matthew Syed</author>
    </book>
</books>

XSLT:

Copy the source xml without any change

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">

        <xsl:copy-of  select="/"/>

    </xsl:template>  
</xsl:stylesheet>

Output:

Common namespace between parent and child does not appear

<books xmlns="www.flipkart.com" xmlns:sport="www.flipkart.com/sports">
    <book xmlns:bio="www.flipkart.com/sports/bio">
        <name>Open</name>
        <author>Agassi</author>
    </book>
    <book xmlns:bio="www.flipkart.com/sports/bio">
        <name>Bounce</name>
        <author>Matthew Syed</author>
    </book>
</books>

My desired behavior is copy source xml without any change.After transformation common namespace(xmlns:sport="www.flipkart.com/sports") between parent "books" and child "book" is not appearing in output

Expected:

<book xmlns:sport="www.flipkart.com/sports" xmlns:bio="www.flipkart.com/sports/bio">

Actual:

<book xmlns:bio="www.flipkart.com/sports/bio">

How to include child namespace in the output ?

Thanks

2

2 Answers

2
votes

The XSLT/XQuery/XPath data model does not allow you to distinguish between

<foo xmlns:pf="http://example.com/"><bar>...</bar></foo>

and

<foo xmlns:pf="http://example.com/"><bar xmlns:pf="http://example.com/">...</bar></foo>

If you need to distinguish such cases you will need to look into DOM where namespace declarations are modeled as attributes, but you will of course also need to find or write a serializer for DOM trees that makes sure any namespace declaration attribute is serialized.

1
votes

You are not talking about "child namespace" but about a namespace declaration which is included three times in your source document - and not used anywhere. In these circumstances the processor may decide to do either one of the following:

  1. copy all instances of the declaration as is;
  2. leave one instance of the declaration in and remove the subsequent repeating declarations;
  3. remove all instances of the declaration altogether.

All of these actions are "correct".

My desired behavior is copy source xml without any change.

Why do the transformation at all, then?