Given a document such as the following:
<patch xmlns="http://example.com/ns/lxfs"
xml:base="http:/example.com/publ/lxfs"
id="http://example.com/lxfs/patches/3">
<!-- ... -->
</patch>
How do I write an XML Schema to require (or even allow) the presence of the xml:base
attribute with the fixed value of "http://example.com/publ/lxfs" on <patch>
?
This is what I'd consider the "obvious" solution but xs:attribute[@name]
is supposed to an NCName
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:lxfs="http://example.com/ns/lxfs"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
targetNamespace="http://example.com/ns/lxfs">
<xs:element name="patch" type="lxfs:Patch" />
<xs:complexType name="Patch">
<xs:attribute name="id" type="xs:anyURI" use="required" />
<xs:attribute name="xml:base" form="qualified" fixed="http://example.com/publ/lxfs" use="required" />
</xs:complexType>
</xs:schema>
Restrictions on a Set of Values To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint. Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
Using schemaLocation The xsi:schemaLocation attribute works well in situations where namespace prefixes are explicitly declared and used in the XML document you want to validate. The following example shows an XML document that references an external XSD schema, MyData.
The declaration of a target namespace gives us the possibility of defining elements and attributes that belong to the target namespace (called "qualified") and elements and attributes that don't belong to any namespace (called "unqualified").
Change <xs:attribute name="xml:base">
to <xs:attribute ref="xml:base">
, and add an xs:import for the schema for the XML namespace which can be found at http://www.w3.org/2001/03/xml.xsd
. (Use a local copy rather than a reference to the one on the W3C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With