Jump to content

HELP!! I don't want to validate an element's name. How can I do this???


Guest nico4269

Recommended Posts

Guest nico4269

Please help!! I need this fast!I don't want to validate an element's name, how ca I do this??For example, if I have this XML:<ComplexTypeA> <ComplexTypeB> ........ </ComplexTypeB></ComplexTypeA><ComplexTypeA> <ComplexTypeB> ........ </ComplexTypeB></ComplexTypeA>As you can see I want a complexTypeA that contains a complexTypeB, but I want to allow the name of complexTypeA to be different every time. THANKS!!

Link to comment
Share on other sites

  • 3 weeks later...

I do not believe this can be done, nor should it be done. Explanation:In HTML, you have a completely predefined set of tags. There is the HTML tag, the body tag, and the head tag (for starters). Each of those tags represents a real object of unchanging definition. When my browser loads up an HTML file, it reads that file. It finds an HTML tag, and it knows what it is. It finds a head tag, and it knows what it is, and then it finds a body tag, and knows what it is. There is no question for my browser: an HTML tag is one thing, a head tag is another, and both are concretely defined.A tag is defined, by the definition of XML by its name and its name alone. Therefore all tags of name X are assumed to be tags representing the same type of object (a schema, in this example, is used to define the type...).What you are trying to do, I will assume, is to have a long list of somewhat related objects, each of which with the same name. For example, you are trying to do something like this:

<?xml version="1.0"?><Toys>  <ActionFigure name="Godzilla Action Figure" owner="Timmy">	<Arm/>	<Arm/>	<Leg/>	<Leg/>	<Head/>  </ActionFigure>  <Doll name="Barbie" owner="Sally">	<Arm/>	<Arm/>	<Leg/>	<Leg/>	<Head/>	<Hair/>  </Doll></Toys>

<?xml version="1.0"?><schema xmlns="...schema ns...">  <element name="Toys">	<complexType>	  <sequence minOccurs="0" maxOccurs="unbounded">		<element><!--No Name-->		  <complexType>			<sequence minOccurs="0" maxOccurs="unbounded">			  <element><!--No Name-->				<complexType />			  </element>			</sequence>			<attribute name="name" type="string"/>			<attribute name="owner" type="string"/>		  </complextype>		</element>	  </complexType>	</element> <!--end Toys-->  </element></schema>

And you might use something like this xpath statement to find all toys which are action figures:

/Toys//ActionFigure

So why doesn't this work?It all has do do with the definition of XML. Each tag is defined in its type not by its scope, but by its name. The scope of an element is only relevant when trying to find it. It is not relevant when trying to define it. Back to the HTML example, this is why every <a> tag is the same, no matter whether it is a child of a <p> tag, a <span> tag, or even just a straight <body> tag. XML is the same way.In this schema, there is nothing do distinguish either of the unnamed elements, so in effect, we just created an element that cannot be identified in the XML file, has two or more types, and is therefore illegal.///////////////////////////////////////////////////////////////////////////////////////////////////////////There are two correct ways of solving this problem, and which you chose will depend on the situation you find yourself in.The first situation is to define typed names for EACH type you want. The advantage of this is that it is compatible with the current XML and XPath. The schema would look like this:

<?xml language="1.0"?><schema xmlns="...schema ns...">  <complexType name="Part" abstract="true"/>  <complexType name="Toy" abstract="true">	<sequence minOccurs="0" maxOccurs="unbounded">	  <element name="Arm">		<complexContent>		  <Restriction base="target:Part" />		</complexContent>	  </element>	  <element name="Leg">		<complexContent>		  <Restriction base="target:Part" />		</complexContent>	  </element>	  <element name="Head">		<complexContent>		  <restriction base="target:Part" />		</complexContent>	  </element> 	</sequence>	<attribute name="owner" type="string">	<attribute name="name" type="string">  <complexType>   <element name="Toys">	<complexType>	  <choice minOccurs="0" maxOccurs="unbounded">		<element name="ActionFigure">		  <complexContent>			<restriction base="target:Toy"/>		  </complexContent>		</element>		<element name="Doll">		  <complexContent>			<extension base="target:Toy">			  <sequence>				<element name="Leg">				  <complexContent>					<restriction base="target:Part" />				  </complexContent>				</element>			  </sequence>			</extension>		  </complexContent>		</element>	  </choice>	</complexType>  </element></schema>

This solution works well under two conditions:

  1. You have relatively few discrete types
  2. All of the discrete types are of your definition. There are no user-defined types.

If this is not the case, you will not be able to do this, since either:

  1. You will have to spend ages creating a very large (and therefore slow) schema file.
  2. You will not be able to account for user-types.

/////////////////////////////////////////////////////////////////////////////////////////////////////////The second solution evolves in this case instead of the first. It involves reshaping the XML. Instead of declaring separate discrete types of toys, declare a type toy, and give it an attribute type. This allows for both the declaration of large numbers of types, since at most a type is a one line enum value. It also allows you to leave the type as a user-defined string, giving them the flexibility to declare their own types.While I don't currently have time to add examples of this solution, I will come back later today and add them (hey-I'm a college student-I have to pay tuition somehow, and while answering XML questions is interesting and helpful to me (you never know a subject until you can teach it to someone else...I just learned about abstract types now, in fact), it doesn't help pay for school).You would then search out different types of elements by their type attribute, instead of their name. The XPath statement thus becomes:

/Toys//Toy[@type='ActionFigure']

////////////////////////////////////////////////////////////////////////////////////////////////////////////////Well ok, good luck!//Mattps-yes, I know, another long post, but this one is not long due to completeness but rather due to examples, which are at least easy to skip over.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...