Solved

Adding attributes on-the-fly through XSL

Posted on 2003-11-20
3
392 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
ID: 9788276
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
ID: 9788616
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
ID: 9789033
thanks a lot spark plug
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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