Sign In/My Account | View Cart  
advertisement

Article:
 The .NET Schema Object Model
Subject: Traversing the Schema Child with Group Reference
Date: 2003-08-01 11:51:14
From: Priyamvadha Lakshminarayanan
Response to: Traversing the Schema Child with Group Reference

Group references are resolved during compilation. If you want access to them, you should use complexType.Particle property which is the pre-compiled property instead of complexType.ContentTypeParticle which is a post-compilation property.


For the following schema,
<xsd:schema attributeFormDefault="qualified" targetNamespace="ns1" xmlns:x="ns1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">


<xsd:group name="g1">
<xsd:sequence>
<xsd:element name="GE1" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
<xsd:complexType name="CT">
<xsd:sequence>
<xsd:element name="E1" type="xsd:int"/>
<xsd:group ref="x:g1"/>
<xsd:element name="E2" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


If you iterate over the schema.SchemaTypes
and get the complex type CT,
complexType.Particle will be a XmlSchemaSequence with 3 items:
System.Xml.Schema.XmlSchemaElement (E1)
System.Xml.Schema.XmlSchemaGroupRef (G1)
System.Xml.Schema.XmlSchemaElement (E2)


complexType.ContentTypeParticle will be a XmlSchemaSequence with 3 items (the group ref and pointless sequence has been resolved):
System.Xml.Schema.XmlSchemaElement (E1)
System.Xml.Schema.XmlSchemaElement (GE1)
System.Xml.Schema.XmlSchemaElement (E2)


HTH,
Priya


No Previous Message Previous Message Move up to Parent Message Up Next Message No Next Message


Titles Only Titles Only Newest First
  • Traversing the Schema Child with Group Reference
    2003-08-01 14:02:57 Matt Frame

    Priya,


    Sorry I forgot to append the test schema I am trying to use now with the same problem.


    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="TopLevel">
    <xs:sequence>
    <xs:element name="Elm1"/>
    <xs:element name="Elm2"/>
    <xs:group ref="GroupOne"/>
    </xs:sequence>
    </xs:complexType>
    <xs:group name="GroupOne">
    <xs:sequence>
    <xs:element name="GO_Elm1"/>
    <xs:element name="GO_Elm2"/>
    <xs:group ref="SubGroupOne"/>
    </xs:sequence>
    </xs:group>
    <xs:group name="SubGroupOne">
    <xs:sequence>
    <xs:element name="SGO_Elm1"/>
    <xs:element name="SGO_Elm2"/>
    </xs:sequence>
    </xs:group>
    </xs:schema>


    As you can see the first group has a reference to a another group, here SubGroupOne, this is the level I am trying to get an cannot. I cannot find the SubGroupOne name or any information about it when traversing the tree.


    Thanks,


    Matt

    • Traversing the Schema Child with Group Reference
      2003-08-01 15:57:36 Priyamvadha Lakshminarayanan

      XmlSchemaGroupRef does not have the pre-compiled particle on it. Hence you need to look up the group from the RefName and then drill down further:
      The following output is generated by the code sample below for the schema you posted:


      Seq, All or Choice
      XmlSchemaElement: Elm1
      XmlSchemaElement: Elm2
      Group ref
      Seq, All or Choice
      XmlSchemaElement: GO_Elm1
      XmlSchemaElement: GO_Elm2
      Group ref
      Seq, All or Choice
      XmlSchemaElement: SGO_Elm1
      XmlSchemaElement: SGO_Elm2


      Notice the two groupRefs printed.


      private void PrintParticles(string url) {
      XmlSchema schema = XmlSchema.Read(new XmlTextReader(url, new NameTable()), new ValidationEventHandler(ValidationCallback));
      schema.Compile(new ValidationEventHandler(ValidationCallback));
      foreach(XmlSchemaType type in schema.SchemaTypes.Values) {
      XmlSchemaComplexType complexType = type as XmlSchemaComplexType;
      if (complexType != null) {
      TraverseParticle(complexType.Particle, schema);
      }
      }

      foreach(XmlSchemaElement elem in schema.Elements) {
      XmlSchemaComplexType complexType = elem.ElementType as XmlSchemaComplexType;
      if (complexType != null && complexType.QualifiedName == XmlQualifiedName.Empty) { //Only local types of an element
      TraverseParticle(complexType.Particle, schema);
      }
      }
      }

      private void TraverseParticle(XmlSchemaParticle particle, XmlSchema schema) {
      if (particle is XmlSchemaElement) {
      XmlSchemaElement elem = particle as XmlSchemaElement;
      Console.WriteLine("XmlSchemaElement: " + elem.QualifiedName.ToString());
      }
      else if (particle is XmlSchemaGroupRef) {
      XmlSchemaGroupRef groupRef = particle as XmlSchemaGroupRef;
      Console.WriteLine("Group ref");
      XmlSchemaGroup group = (XmlSchemaGroup)schema.Groups[groupRef.RefName]; //Get the group
      TraverseParticle(group.Particle, schema);
      }
      else if (particle is XmlSchemaGroupBase) {
      Console.WriteLine("Seq, All or Choice");
      XmlSchemaGroupBase baseParticle = particle as XmlSchemaGroupBase;
      foreach(XmlSchemaParticle subParticle in baseParticle.Items) {
      TraverseParticle(subParticle, schema);
      }
      }
      }

      • Traversing the XML Schema
        2006-05-02 20:48:01 Forum

        Hi friends


        this is my xsd file


        <?xml version="1.0" encoding="utf-8"?>
        <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <!-- -->
        <xs:element name="Plan">
        <xs:complexType>
        <xs:sequence>
        <xs:element name="Patient_Data">
        <xs:complexType>
        <xs:sequence>
        <xs:element name="Name_of_Patient" type="xs:string" />
        <xs:element name="Patient_ID" type="xs:string" />
        <xs:element name="Clinical_Record" type="xs:string" />
        <xs:element name="Site_of_Target_Vol" type="xs:string" />
        </xs:sequence>
        </xs:complexType>
        </xs:element>
        <xs:element name="General_Data">
        <xs:complexType>
        <xs:sequence>
        <xs:element name="Planned_on" type="xs:date" />
        <xs:element name="Planned_by" type="xs:string" />
        <xs:element name="Date_of_Protocol" type="xs:dateTime" />
        <xs:element name="Number_of_Pages_Protocol" type="xs:int" />
        <xs:element name="Image_Series_ID" type="xs:int" />
        <xs:element name="Target_Point_Mode" type="xs:string" />
        <xs:element name="Device_Coord_Syst" type="xs:string" />
        </xs:sequence>
        </xs:complexType>
        </xs:element>
        </xs:sequence>
        </xs:complexType>
        </xs:element>
        </xs:schema>


        i want to display or print all the elements in the schema., the elements are like "plan" under this "Patient_data and General_data" and under patient_data it has some sub elements. How can i traverse all the elemetns in the file. I tried like this


        XmlTextReader reader = new XmlTextReader(@"E:\XmlTest\Test\PatientData.xsd");
        try
        {
        XmlSchema schema = XmlSchema.Read(reader, null);
        schema.Compile(null);
        if (schema.IsCompiled)
        {
        MessageBox.Show("Inside");
        foreach (XmlSchemaElement parentElement in schema.Elements.Values)
        {
        MessageBox.Show(parentElement.Name.ToString());
        XmlSchemaComplexType ct = parentElement.ElementType as XmlSchemaComplexType; //Casting to complex type
        if (ct != null)
        {
        XmlSchemaSequence seq = (XmlSchemaSequence)ct.ContentTypeParticle;
        foreach (XmlSchemaElement elem in seq.Items)
        {
        MessageBox.Show(elem.Name.ToString());
        }
        }
        }
        it displays only plan and patient_data and General_data it is not displaying sub elements. Can u please tell me where the problem is? share ur ideas


        Thanks in advance


        Regards


      • Traversing the Schema Child with Group Reference
        2003-08-02 22:36:39 Matt Frame

        Priya,


        Thank you so much for helping me out with this. I am getting a much better understanding of schema's and the SOM because of your help. I have one last question though.


        Is it possible to be in SubGroupOne and find out which parent you came from? I guess I am asking can you traverse the tree in reverse if need be.


        Thanks,


        Matt

  • Traversing the Schema Child with Group Reference
    2003-08-01 13:48:54 Matt Frame

    Priya,


    Thanks so much for getting back with me. I have to admit I am new to XML Schemas and the SOM. I see a big difference in the way you created a schema and the way I had, of course I am using XMLSPy and I am new at this. Here is a sample of what I had put together.


    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="TopLevel">
    <xs:annotation>
    <xs:documentation>Test Schema for SOM Development</xs:documentation>
    </xs:annotation>
    <xs:complexType>
    <xs:group ref="GroupOne"/>
    </xs:complexType>
    </xs:element>
    <xs:group name="GroupOne">
    <xs:sequence>
    <xs:element name="GO_Elm1"/>
    <xs:group ref="SubGroupOne"/>
    </xs:sequence>
    </xs:group>
    <xs:group name="SubGroupOne">
    <xs:sequence>
    <xs:element name="SGO_Elm1"/>
    <xs:element name="SGO_Elm2"/>
    </xs:sequence>
    </xs:group>
    </xs:schema>


    This schema doesn't have any schema types so I guess that is why I was having such a hard time traversing the tree, although I was using the Particle and not the ContentParticle.


    I am still having difficulty putting the correct code together to traverse the tree using SchemaTypes. If you have a sample that would be great if you don't that's fine to. I just hate that I have spent a week traversing the tree one way only to find that was completely wrong and now I have to learn how to do it differently.


    I really appreciate your help.


    Thanks,


    Matt


Sponsored By: