Solved

Adding attributes on-the-fly through XSL

Posted on 2003-11-20
3
387 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:xtremebytes2002
  • 2
3 Comments
 
LVL 9

Expert Comment

by:sparkplug
Comment Utility
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<
0
 
LVL 9

Accepted Solution

by:
sparkplug earned 50 total points
Comment Utility
Or maybe you can dynamically add the namespace too...

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

See also:

http://www.xslt.com/xsl-list/2002-09/msg00658.html

>S'Plug<
0
 
LVL 1

Author Comment

by:xtremebytes2002
Comment Utility
thanks a lot spark plug
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now