Jump to content

XML element not renamed during XSLT transformation


Garfield1970

Recommended Posts

Hello!

 

My problem:

 

I have an input file containing XML MOCs with following structure:

 

<moi xsi:type="ABC">

<attributes>

<PARAMETER_A>1</PARAMETER_A>

</attributes>

</moi>

 

This XML MOCs must be converted to this structure:

 

<INTERN-ABC>

<attributes>

<PARAMETER_A>1</PARAMETER_A>

</attributes>

</INTERN-ABC>

 

For this purpose I have written a XSLT file with following content:

 

<xsl:function name="my:elementName"> <xsl:param name="name"/> <xsl:value-of select="'INTERN'"/> <xsl:value-of select="'-'"/> <xsl:value-of select="$name"/> </xsl:function> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="moi"> <xsl:element name="{my:elementName(@xsi:type)}"> <xsl:apply-templates/> </xsl:element> </xsl:template>

 

It doesn't work - all XML elements are copied unchanged, also the elements with name "moi". Obvious the template for the "moi" elements is ignored completely.

 

I have to do a transformation also in other direction:

 

<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[starts-with(local-name(),'INTERN-')]"> <xsl:element name="moi"> <xsl:attribute name="xsi:type"> <xsl:value-of select="substring(local-name(),8)"/> </xsl:attribute> <xsl:apply-templates/> </xsl:element> </xsl:template>

 

This transformation works. But the first one doesn't work.

 

Has someone any idea why the first transformation doesn't work?

 

Greetings,

Thomas

 

P.S.: For test purposes I have changed the name "moi" in input file and XSLT file to "xsi:moi". After this change the transformation worked. But this is no solution, of course. The name of the elements is "moi" and I cannot change it. Why the transformation doesn't work for XML elements with the name "moi"????

 

P.P.S: It seems to be a namespace problem. If I define a prefix for the namespace of the input file in my XSLT file and put this prefix before the name "moi" in the template then the transformation works. Obvious the XSLT processor can find the "moi" elements only if it knows that they are in the namespace of the input file. But now I have another problem. The result of the transformation looks like this:

 

<INTERN-ABC xmlns="">

<attributes xmlns="<namespace name of the input file>">

<PARAMETER_A>1</PARAMETER_A>

</attributes>

</INTERN-ABC>

 

How I can suppress the xmlns attributes in the output file? I have added my new defined prefix in the XSLT file to the exclude-result-prefixes list, but the xmlns attributes are always written to output file, no matter if the prefix is in the exclude list or not...

 

P.P.P.S: I have solved the problem. At first I had added the namespace of the input (and output) file to the xsl:stylesheet element in the XSLT file using the attribute xmlns. This didn't work. Then I have defined the prefix "hs" for the namespace of the input file and changed the attribute name to xmlns:hs. This did work, but the xmlns atrributes are added to the elements in the output file. Finally I added the xmlns attribute again, and after this the xmlns attributes are no more created in the output file.

 

But can any XSLT guru explain to me why a template with match="moi" doesn't work??? Althought the namespace of the input file was declared with the xmlns attribute in the XSLT file?

Edited by Garfield1970
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...