xsl-list
[Top] [All Lists]

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

2006-08-11 12:41:56
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"
        xmlns="http://www.w3.org/2001/XMLSchema";
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
        <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">
                                <sequence>
                                        <xsl:for-each 
select="metadataModel/metadataElement">
<element name="{(_at_)name}" type="{valueType}" xmlns="http:// www.w3.org/2001/XMLSchema"/>
                                        </xsl:for-each>
                                </sequence>
                        </complexType>
                        
                        <element name="metadataSet" type="tns:metadataSet"/>
                
                </schema>
        </xsl:template>

</xsl:stylesheet>



#####
# 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";
        targetNamespace="http://www.mediashelf.com/xsd/MSHLF/myModel";
        xmlns:xsd="http://www.w3.org/2001/XMLSchema";
        xmlns:tns="http://www.mediashelf.com/xsd/MSHLF/myModel";>
        
        <complexType name="metadataSet">
        <sequence>
                <element type="xsd:string" name="myTextField"></element>
        </sequence>
        <attribute name="model" type="xsd:string"></attribute>
        </complexType>

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



#####
#       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"
        xmlns:tns="http://www.mediashelf.com/xsd/ModelDefinitionSchema";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
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>
    <valueType>xsd:string</valueType>
    <defaultValue>Enter something!</defaultValue>
    <valuesUsedToDate>
        <stringValue>"first value used"</stringValue>
        <stringValue>"second value used"</stringValue>
    </valuesUsedToDate>
  </metadataElement>
</metadataModel>



Matt Zumwalt
matt(_dot_)zumwalt(_at_)yourmediashelf(_dot_)com
917-687-8551

Visit http://www.yourmediashelf.com




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>