1

I have an XML file which has to be transformed using XSLT, I am adding a node to a subtree. The input file is as follows:

 <?xml version="1.0" encoding="UTF-8"?>
    <Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Context>
    <ContextExtra>
        <ns3:timestampList xmlns:ns3="http://www.mydomain.com/myAdapter/basicMessage/1.0" 
                           xmlns:ns2="http://www.mydomain.com/myAdapter/tbMessage/1.0" 
                           xmlns="http://www.mydomain.com/myAdapter/timestamp/1.0" 
                           xmlns:ns10="http://www.mydomain.com/myAdapter/coMessage/1.0" 
                           xmlns:ns11="http://www.mydomain.com/myAdapter/lolMessage/1.0" 
                           xmlns:ns12="http://www.mydomain.com/myAdapter/tcMessage/1.0" 
                           xmlns:ns13="http://www.mydomain.com/myAdapter/bMessage/1.0" 
                           xmlns:ns4="http://www.mydomain.com/myAdapter/tiMessage/1.0" 
                           xmlns:ns5="http://www.myDomain.com/myAdapter/oriMessage/1.0" 
                           xmlns:ns6="http://www.myDomain.com/myAdapter/eiMessage/1.0" 
                           xmlns:ns7="http://www.myDomain.com/myAdapter/fplscMessage/1.0" 
                           xmlns:ns8="http://www.myDomain.com/myAdapter/psMessage/1.0" 
                           xmlns:ns9="http://www.myDomain.com/myAdapter/stMessage/1.0">
            <timestampInfo>
                <timestampID>START</timestampID>
                <timestamp>2012-02-25T00:30:18.705+01:00</timestamp>
                <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            </timestampInfo>
            <timestampInfo>
                <timestampID>END</timestampID>
                <timestamp>2012-02-25T00:30:23.675+01:00</timestamp>
                <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            </timestampInfo>
        </ns3:timestampList>

    </ContextExtra>
</Context>

The transformation looks like this

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:output method="xml" indent="yes" />    

<xsl:param name="newTimeStamp">
    <timestampInfo>
        <timestampID>
            <xsl:text>TEST TEST</xsl:text>
        </timestampID>
        <timestamp>
            <xsl:text>2012-02-25T00:30:23.654+01:00</xsl:text>
        </timestamp>
        <description>
            <xsl:text>this is a test</xsl:text>
        </description>
    </timestampInfo>
</xsl:param>

<xsl:template match="//ts:timestampInfo[position()=last()]">
    <xsl:call-template name="identity" />
    <xsl:copy-of select="$newTimeStamp" />
</xsl:template>

<xsl:template match="node()|@*" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

and the result is as follows:

<?xml version="1.0" encoding="UTF-8"?>
 <Envelope>
<Context>
    <ContextExtra>
        <ns3:timestampList xmlns="http://www.myDomain.com/myAdapter/timestamp/1.0" xmlns:ns3="http://www.myDomain.com/myAdapter/basicMessage/1.0">
            <timestampInfo>
                <timestampID>MTA_START</timestampID>
                <timestamp>2012-02-25T00:30:18.705+01:00</timestamp>
                <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            </timestampInfo>
            <timestampInfo>
                <timestampID>MTA_END</timestampID>
                <timestamp>2012-02-25T00:30:23.675+01:00</timestamp>
                <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            </timestampInfo>
            <timestampInfo xmlns="">
                <timestampID>TEST TEST</timestampID>
                <timestamp>2012-02-25T00:30:23.654+01:00</timestamp>
                <description>This is a test</description>
            </timestampInfo>
        </ns3:timestampList>
    </ContextExtra>
</Context>

As you can see timestampInfonodes have as attribute xmlns="". That is, it is an empty namespace generated. Instead of <timestampInfo xmlns=""> I would like to have nodes of the form <timestampInfo>. The input file has this format and I have no control over it.

How I can achieve this?

Thanks in advance

Luixv
  • 8,590
  • 21
  • 84
  • 121

2 Answers2

0

Specify the namespace of the timestampInfo you are adding:

<xsl:param name="newTimeStamp"> 
    <timestampInfo xmlns="http://www.myDomain.com/myAdapter/timestamp/1.0"> 
        <timestampID> 
            <xsl:text>TEST TEST</xsl:text> 
        </timestampID> 
        <timestamp> 
            <xsl:text>2012-02-25T00:30:23.654+01:00</xsl:text> 
        </timestamp> 
        <description> 
            <xsl:text>this is a test</xsl:text> 
        </description> 
    </timestampInfo> 
</xsl:param> 
MiMo
  • 11,793
  • 1
  • 33
  • 48
0

Remember that namespace declarations exist only in the serialized output. In the result tree which your transformation is generating the important thing is not what namespace declarations exist (because they don't), but rather, what is the name of each element? The problem with your transformation is that the timestampInfo elements have the wrong name - more particularly, they have the right local name, but the wrong namespace. Get the element names right, and the serializer will take care of generating the namespace declarations. And of course, when you use a literal result element, the name of the element in the result tree is the same as the name of the element in the stylesheet (namespace and all).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164