0

I tried to valid two XML files: One file file1.xml has a element with an xs:ID type attribute and in the other file2.xml has an element with an xs:IDREF type attribute and for validate that, i use a file.xsd.

this is my try:

<!-- file1.xml -->
<book id="l1">
  <title>El señor de los anillos</title>
  <author>J.R.R. Tolkien</author>
</book>
<!-- file2.xml -->
<review ref="l1">
  <comment>Un clásico de la literatura</comment>
  <score>5</score>
</review>
<!-- file.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- book -->
  <xs:element name="book" type="bookType">
  </xs:element>
  
  <!-- review -->
  <xs:element name="review" type="reviewType">
    <xs:unique name="bookID" >
      <xs:selector xpath="book"/>
      <xs:field xpath="@id"/>
    </xs:unique>
    <xs:keyref name="reviewRef" refer="bookID">
      <xs:selector xpath="review"/>
      <xs:field xpath="@ref"/>
    </xs:keyref>
  </xs:element>

  <!-- book -->
  <xs:complexType name="bookType">
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="author" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID"/>
  </xs:complexType>

  <!-- review -->
  <xs:complexType name="reviewType">
    <xs:sequence>
      <xs:element name="comment" type="xs:string"/>
      <xs:element name="score" type="xs:integer"/>
    </xs:sequence>
    <xs:attribute name="ref" type="xs:IDREF" use="required"/>
  </xs:complexType>
  
</xs:schema>

Try to see the xmlschema documentation but the only thing I could do is this:

from xmlschema import XMLSchema

schema = XMLSchema('file.xsd')

schema.validate('file1.xml')
schema.validate('file2.xml')
Usuario86
  • 1
  • 1
  • https://stackoverflow.com/questions/299588/validating-with-an-xml-schema-in-python might provide some alternatives for you. – j_b Jan 18 '23 at 23:20
  • In the end that schema allows for two different root elements (e.g. `book` or `review`) but declares a reference constraint between these elements. As any XML document has a single root it doesn't really make sense to allow for two different but expect to have constraints between them. – Martin Honnen Jan 19 '23 at 07:02

0 Answers0