Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD Schema question: defining attributes based on parent element

Status
Not open for further replies.

suffecool

Programmer
Apr 23, 2007
1
US
Hello,

I have an XSD question to see whether or not it is possible to write schema rules for an .xsd file for the following scenario:

I have an element <parameter>. Here are my desired rules:

If it is used inside a <function> element, then it MUST have one and only one <type> element inside of it.

However, it if is used inside a <macro> element, then it cannot have ANY <type> element inside of it.

In other words, observe the following two XML examples:

<function name="sqrt">
<parameter name="x">
<type name="float"/>
</parameter>
<return value="float"/>
</function>

vs.

<macro name="SQRT">
<parameter name="X"/>
<expression value="sqrt(X)"/>
</macro>


As you can see, in the <function> case, the <parameter> element MUST have a <type> given inside of it. However, in the <macro> case, it must NOT have a <type>. That is, the following XML statement should fail:

<macro name="SQRT">
<parameter name="X">
<type name="int"/>
</parameter>
<expression value="sqrt(X)"/>
</macro>


Is there any way to specify in the XSD schema doc rules that basically say "if using <parameter> inside a <function>, then you must have one and only one <type> nested inside of it; however, if using <parameter> inside a <macro>, then you can have NO elements nested inside of it." In other words, I want the rules as to what can and cannot be nested inside an element to vary, based upon who its parent it.

I hope I have explained the question succinctly enough, and I thank you for any and all responses.

--K. Suffecool
 
That is allowed.

In the data xml document, the element node's name is not bounded to have the same structure. If the xml is to be validated in the sense of wxsl, it has to be validated against an xsd document. Now, in xsd, the element is xs:element, not the original xml's element node's name. That name is now appearing in the xsd as the name attribute's value. As long as the xs:element of the same name attribute can perfectly have different type attribute (or different sub-structure), they are not barred to be a "valid" xsd document.

This is how you do it supposing the root being "root". (The occurence indicator is there but free to be adjusted to the need.) I use top-level complexType to isolate the sub-structure. You can also do it in-line to the xs:element of the corresonding place.
[tt]
<?xml version="1.0" ?>
<xs:schema xmlns:xs=" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="function" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" type="[blue]funcparam[/blue]" />
<xs:element name="return">
<xs:complexType>
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="macro" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" type="[blue]macroparam[/blue]" />
<xs:element name="expression">
<xs:complexType>
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="[blue]funcparam[/blue]">
<xs:sequence>
<xs:element name="type" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>

<xs:complexType name="[blue]macroparam[/blue]">
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:schema>
[/tt]
Some detail may be tightened according to need (such as xs:string or occurence or sequence etc), but the structure needed is all that is sketched in the above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top