views:

49

answers:

2
<Unit Number="1">
 <Identifier Type="ABC" Text="STO0001"/>
 <Identifier Type="DEF" Text="Some Value"/>
 <Identifier Type="GHI" Text="20070805"/>
 <Disposition Unit="Accept"/>
</Unit>

I need to validate that Type="DEF" Text="Some Value" is not empty

Something Like:

<xs:complexType name="requiredValue" abstract="true"/>

<xs:complexType name="Identifier">
    <xs:complexContent>
        <xs:extension base="requiredValue">
            <xs:attribute name="Type" use="required" fixed="DEF"/>
            <xs:attribute name="Text" type="NonEmptyString"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
A: 

Using the xsd:minLength restriction:

<xsd:attribute name="Type">
  <xsd:simpleType>
    <xsd:restriction base="xsd:string">
      <xsd:minLength value="1"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>
Obalix
Wont this also validate the other Types? I only need to validate the "DEF" type.
If what I think your saying is only make sure that Text is not empty when Type="DEF"... I'm pretty sure that type of thing can't be accomplished with XML schema validation.. That is not really part of the "structure" of a document. XML schemas are not the best at specifying "conditional" values for structures.
Harley Green
If you need to validate dependent values and such, look at Schematron - it allows you to specify a bunch of asserts in XPath, and it can be embedded in XSD annotations (with a few tools being able of locating it there and running it when schema is being validated).
Pavel Minaev
+1  A: 

This is not exactly what you are after, but it might help you to do the bulk of the schema.

This allows you to upload a xml file and it will create a xsd schema or a DTD.

http://www.hitsw.com/xml_utilites/

This does the same sort of thing.

http://www.flame-ware.com/products/xml-2-xsd/Default.aspx

Pavel mentioned Schematron. To help construct these schemas you might like to use pyang.

http://code.google.com/p/pyang/
philcolbourn