[xsl] Referencing namespace prefixes within an XSD Schema generated by XSL transform

Subject: [xsl] Referencing namespace prefixes within an XSD Schema generated by XSL transform
From: Matt Zumwalt <matt.zumwalt@xxxxxxxxxxxxxxxxxx>
Date: Fri, 11 Aug 2006 14:41:25 -0500
Hi all,

Here's hoping there's an obvious answer to this.

I need to generate minimal XSD schemas from data models that are defined in XML documents. I've got everything working except one little detail. I don't know how to reference the generated schema's target namespace when I'm defining its elements' types.

Example: The generated schema defines one complexType named "metadataSet". I want to create an xsd:element with that as its type. In a normal xsd file I would declare a namespace xmlns:tns and then set the element's type to "tns:metadataSet".

Here's the gotcha: I can't declare xmlns:tns up in the <xsl:stylesheet> tag because parts of the namespace URI come from my source XML file, which means that they are not available until I start an <xsl:template> tag. Within the template, I construct the namespace URI and store it as an xsl:param, but this doesn't help much. XSL has this nifty setup with declaring namespaces for elements and attributes, but these namespaces get extra little inserts like xmlns:ns0 that only appear during the transform process. This leaves me in the lurch because I need to reference that namespace when I declare the vale of an attribute. As far as I can tell, there is no way for my xsl document to know exactly what that auto-generated namespace prefix will look like in the output document. That would be fine if I only needed to append the prefix to attribute and element names because I could basically ignore the whole thing and let the processor handle it, but I need to put the namespace prefix into the VALUE of a "type" attribute.

There's got to be either a way to:
1) programmatically reference the auto-generated namespace prefixes,
2) stop the processor from generating those little inserts in the namespace prefixes, or
3) bypass the whole headache in some way that I didn't notice.

# My XSL Document (Currently the metadataSet element's type is set to "tns:metadataSet", but the tns prefix is not declared anywhere.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:param name="targetNS">http://www.mediashelf.com/xsd/<xsl:value- of select="metadataModel/@owner"/>/<xsl:value-of select="metadataModel/@name"/></xsl:param>
<schema targetNamespace="{$targetNS}">

<complexType name="metadataSet">
<xsl:for-each select="metadataModel/metadataElement">
<element name="{@name}" type="{valueType}" xmlns="http:// www.w3.org/2001/XMLSchema"/>

<element name="metadataSet" type="tns:metadataSet"/>



# Example of the (ideal) schema that I want to generate:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema";
	<complexType name="metadataSet">
        	<element type="xsd:string" name="myTextField"></element>
        <attribute name="model" type="xsd:string"></attribute>

	<element name="metadataSet" type="tns:metadataSet"></element>

#	Example of the XML Files that get Transformed

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="SchemaFromModelDef.xsl"?>
<metadataModel name="myModel" owner="MSHLF"
xsi:schemaLocation="http://www.mediashelf.com/xsd/ ModelDefinitionSchema ModelDefinitionSchema.xsd ">
<metadataElement name="myTextField" type="tns:textfield">
<label>Text Field Label</label>
<description>Text Field Description</description>
<defaultValue>Enter something!</defaultValue>
<stringValue>"first value used"</stringValue>
<stringValue>"second value used"</stringValue>

Matt Zumwalt

Visit http://www.yourmediashelf.com

Current Thread