Re: [xml] XML Schema Help

On 09/22/2011 06:26 PM, Syd Bauman wrote:
I'm not at all sure this is a proper list for asking schema

So I need to make sure that Policy -> Preferences ->
ServerPreferences -> preference -> name = TARGETS exists as a
preference in ServerPreferences, however, I need to allow other
preferences as well but TARGETS must be present.
any suggestions?
First, unless you really have to, don't use the W3C XML Schema

In RELAX NG, you could use something like:

| start = element serverPreferences {
|      others, TARGETS, others
|      }
| TARGETS = element preference {
|     element name { "TARGETS" },
|     element value { value }
|     }
| others = element preference {
|     element name { prefName },
|     element value { value }
|     }+
| value = ("B" | "C" | "D" )
| prefName = ("A" | "X" | "Y" | "Z")

But this is the sort of thing I would usually do by adding a
Schematron check, rather than using grammar-based validation. E.g.,

| <?xml version="1.0" encoding="UTF-8"?>
| <schema xmlns="";>
|   <pattern>
|     <rule context="ServerPreferences">
|       <assert test="preference/name[.='TARGETS']">
|         Server preferences should always have a TARGET preference.
|       </assert>
|     </rule>
|   </pattern>
| </schema>

Note: these code fragments are untested.
xml mailing list, project page
xml gnome org

I apologize if I posted to the wrong list. Anyway, thanks for your help.
The schematron rule appears to be working, however, here's my scenario.
I have an XSD written already and don't have the time to convert it to
Schematron or relax-NG. I know xmllint doesn't fully implement the XSD
specification nor does it support XSD 1.1 but without having to
introduce an alternative validator to the build process, I'm stuck with
xmllint and an XSD. Also, I'm dealing with very large .xml files at
times, in some instances close to 1GB, so memory is of importance here.
I'll break down the process below.

You import your XML file, validation is performed using xmllint, if
validation fails I simply discard the file and throw an error. However,
if validation succeeds, the XML file is parsed using PHP (the SAX
parser). I could use PHP to validate if the TARGET preference is set and
discard the file and throw an error if not found, however, I'd prefer
not to go that route. I'm also dealing with two separate formats of the
XML file but the problem exists with the schema validation for the newer

I've been trying to find the proper XSD rule, without any luck, to
validate the document. I'd prefer not to have to validate the XML file
twice using xmllint. I'm currently executing xmllint from the CLI,
without any luck using both the XSD and schematron (with the --stream
option). I need the --stream option so xmllint doesn't load the entire
document in memory. When executing xmllint with --schematron and the
--stream option, I get no validation messages back, whatsoever.

xmllint --noout --stream --schematron file.sch file.xml
xmllint --noout --stream --schema file.xsd --schematron file.sch file.xml


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]