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>
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
Just to clarify what Michael already posted, to get around this problem I first added this line to the top of my schema:
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/03/xml.xsd" />
And then to attach this to an element, simply add the attribute:
<xs:attribute ref="xml:base" />
Wow, this saved me a lot of headaches.
精彩评论