Solved

Adding attributes on-the-fly through XSL

Posted on 2003-11-20
3
391 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

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