Groups | Blog | Home
all groups > dotnet xml > april 2004 >

dotnet xml : Parse Xml Schema


shearne NO[at]SPAM tssg.org
4/21/2004 3:25:50 PM
I have a Xml schema like the this:

....
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.ipdr.org/namespaces/ipdr"
xmlns:ipdr = "http://www.ipdr.org/namespaces/ipdr"
version = "3.0"
elementFormDefault = "qualified"
attributeFormDefault = "unqualified">
<include schemaLocation = "IPDRDoc3.0.xsd"/>
<element name = "day" type = "string"/>
<element name = "size" type = "int"/>
<complexType name = "IPDR-TEST-Type">
<complexContent>
<extension base = "ipdr:IPDRType">
<sequence>
<element ref = "ipdr:day"/>
<element ref = "ipdr:size" minOccurs="0"/>
</sequence>
</extension>
</complexContent>
</complexType>
</schema>

I would like to be able write code which would output the following to me:

IPDR-TEST-TYPE
NAME DATA-TYPE MIN-OCCURANCE
Day string 1
size int 0

I've tried the following:

reader = new XmlTextReader(getIPDRStructure.FileName);
schema = XmlSchema.Read(reader,null);

foreach(XmlSchemaObject elem in schema.Items)
{
if(elem is XmlSchemaComplexType)
{
XmlSchemaComplexType complexType = (XmlSchemaComplexType)elem;

// but complexType.Particle here is null


solex
4/22/2004 5:17:43 PM
Steve,
There is a problem with your schema definition. If you change your schema
definition to just <schema> then the following XSLT will work:
Regards,
Dan

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!-- begin -->
<xsl:template match="//schema">
<xsl:apply-templates select="complexType"/>
</xsl:template>
<!-- complexType -->
<xsl:template match="complexType">
<xsl:value-of select="@name"/>
<xsl:text>&#10;&#9;</xsl:text>
<xsl:text>NAME&#9;</xsl:text>
<xsl:text>DATA TYPE&#9;</xsl:text>
<xsl:text>MIN-OCCURANCE&#9;</xsl:text>
<xsl:for-each select="complexContent/extension/sequence/element">
<xsl:text>&#10;&#9;</xsl:text>
<xsl:value-of select="substring-after(@ref,':')"/>
<xsl:text>&#9;</xsl:text>
<xsl:call-template name="GetDataType">
<xsl:with-param name="elementName" select="substring-after(@ref,':')"/>
</xsl:call-template>

<xsl:text>&#9;</xsl:text>
<xsl:value-of select="@minOccurs"/>
</xsl:for-each>
</xsl:template>

<!-- get the data type -->
<xsl:template name="GetDataType">
<xsl:param name="elementName"/>
<xsl:value-of select="preceding::element[@name=$elementName]/@type"/>
</xsl:template>

</xsl:stylesheet>

[quoted text, click to view]

AddThis Social Bookmark Button