Link to home
Start Free TrialLog in
Avatar of xtremebytes2002
xtremebytes2002

asked on

Adding attributes on-the-fly through XSL

After generating an XML through an XSL transformation on another XSL, if I want to add (on-the-fly) the namespace and schema location attributes to the root element of the generated XML file, then how do I go about it? I used <xsl:attribute> as follows.

<xsl:attribute name="xmlns:xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
<xsl:attribute name="xsi:noNamespaceSchemaLocation">myschema.xsd</xsl:attribute>

This generates warning/error on the XML transformation engine (using javax.xml.transform.*) saying that I have used illegal attribute names. How to insert those attributes then?

Thanks a lot in advance.
Avatar of sparkplug
sparkplug

You can't add the namespace of the fly. You can however add all the namespaces you might use to the <xsl:stylesheet> element. These will be replicated in the output. The noNamespaceSchemaLocation attribute can be dynamically generated providing the namespace has been defined.

The following is an example of how this would be done:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
      <root>
      <xsl:attribute name="xsi:noNamespaceSchemaLocation"><xsl:value-of select="@schemaFileName"/></xsl:attribute>
      </root>
</xsl:template>
</xsl:stylesheet>

Hope this helps.

>S'Plug<
ASKER CERTIFIED SOLUTION
Avatar of sparkplug
sparkplug

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xtremebytes2002

ASKER

thanks a lot spark plug