How to add more namespaces to one element by XSLT

Here should go questions about transforming XML with XSLT and FOP.
VlSkrbek
Posts: 2
Joined: Sat Feb 02, 2019 7:42 pm

How to add more namespaces to one element by XSLT

Post by VlSkrbek » Sat Feb 02, 2019 8:11 pm

Hi,

I am sorry but I am desparate. I am new to XSLT developing and I thought that it will be easy but the creating of XSLT that will do exactly what I want is not so easy.

I need to add more namespaces to one element by XSLT.

See. I need to create this XML file:

Code: Select all


<?xml version="1.0" encoding="Windows-1250"?>
<inv:invoice version="2.0" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd">
<inv:invoiceHeader xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" >

</inv:invoiceHeader>
</inv:invoice>
There is used the namespace prefix "inv" for invoice "inv:invoice" and then for invoiceHeader there are used two prefixes like "typ" and "rsp". Of cousre I can create it in text editor and it works but i need to create it automaticaly by XSLT template so I can automate the invoices that I have in XML file from our supplier.

I have created this XSLT file:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:attribute-set name="ver">
<xsl:attribute name="version">2.0</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/">
<xsl:element name="inv:invoice" namespace="http://www.stormware.cz/schema/version_2/invoice.xsd" use-attribute-sets="ver">
<xsl:element name="inv:invoiceHeader" namespace="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd">
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
But it does not generate the result I need. Please can somebody help say how to add more namespaces with prefixes to one element y XSLT?

Best Reagrds

Radu
Posts: 6393
Joined: Fri Jul 09, 2004 5:18 pm

Re: How to add more namespaces to one element by XSLT

Post by Radu » Mon Feb 04, 2019 9:52 am

Hi,

Maybe you can try to use the <xsl:namespace> element:

https://www.w3.org/TR/xslt20/#element-namespace

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

VlSkrbek
Posts: 2
Joined: Sat Feb 02, 2019 7:42 pm

Re: How to add more namespaces to one element by XSLT

Post by VlSkrbek » Thu Feb 14, 2019 7:20 pm

Thank you very much. It works. Actually I have tried it before but ti does not work because I tried it to add to the Element. Today I have test it again and it works but if I add the textuatl element not to xsl:element.

See this example:

Code: Select all


            <xsl:element name="inv:invoiceHeader" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">
<xsl:namespace name = "rsp" select = "'http://www.stormware.cz/schema/version_2/response.xsd'" />
<xsl:namespace name = "lst" select = "'http://www.stormware.cz/schema/version_2/list.xsd'" />
<inv:accounting>
<typ:ids>1Fp</typ:ids>
</inv:accounting>
</xsl:element>
It create the element name "inv:invoiceHeader" with namespace prefix "typ" and then under this element there is used <typ:ids> and it is known.

When I put xmlns:typ to the xsl:namespace name then this does not work, see:

Code: Select all


            <xsl:element name="inv:invoiceHeader">
<xsl:namespace name = "rsp" select = "'http://www.stormware.cz/schema/version_2/response.xsd'" />
<xsl:namespace name = "lst" select = "'http://www.stormware.cz/schema/version_2/list.xsd'" />
<xsl:namespace name = "typ" select = "'http://www.stormware.cz/schema/version_2/type.xsd'" />
<inv:accounting>
<typ:ids>1Fp</typ:ids>
</inv:accounting>
</xsl:element>
it does not work with error:
Error 1 at line 25:24 : Error reported by XML parser: The prefix "typ" for element "typ:ids" is not bound.

Best Regards

Radu
Posts: 6393
Joined: Fri Jul 09, 2004 5:18 pm

Re: How to add more namespaces to one element by XSLT

Post by Radu » Fri Feb 15, 2019 8:56 am

Hi,

If in the XSLT stylesheet you output elements using constructs like this:

Code: Select all

<typ:ids>1Fp</typ:ids>
you will still need to declare a namespace for the prefix xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" usually directly on the xsl:stylesheet root element.
So declaring prefix namespace mappings in the XSLT stylesheet using "xsl:namespace" is usually not needed because you already declare the prefix to namespace mappings in the XSLT stylesheets in order to properly output the elements.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply