0

I have below xml and want to validate if name = item1 (attribute) then value = 40000 (element) AND if name = item2 then value = 51000. I don't want to use xml 1.1 version.

I am beating my head from 3 days and found limitations of XSD. Is there other best way which can be incorporated in schema file itself to perform this metadata validation rather to write some code. Schematron is not a option too. Also there are 100 items to validate with exact value in value element

<?xml version="1.0" encoding="UTF-8"?>
<pref>
<item name="item1">
    <item_des> Controls ac </item_des>
        <context name="conx">
            <value>40000</value>
        </context>
    </item>
<item name="item2">
        <item_des>Determine dc</item_des>
    <context name="conx">
        <value>51000</value>
    </context>
    </item>
</pref>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Yogi
  • 3
  • 2
  • I'm pretty sure this isn't possible with XML Schema 1.0 or DTDs. As you correctly point out, XML Schema 1.1 (which I'm guessing you meant by XML 1.1, which however is an entirely different thing from XML 1.1) can be used to decide a type based on the value of an attribute, and Schematron allows arbitrary XPath predicates to be evaluated, so either of those should work. But XML Schema 1.0 only allows a very restricted form of XPath evaluation (in id/idref constraints) for validation, and even then can't couple or infer element content text to/from an attribute value. – imhotap Sep 10 '17 at 11:58
  • **Your constraint cannot be represented in XSD 1.0.** See duplicate link or many other examples on this site for how use assertions in XSD 1.1. If XSD 1.1 and Schematron etc are unacceptable, you'll have to check your constraint out-of-band wrt XSD/Schematron, or rethink what you want out of XML in the context of your greater system design. – kjhughes Sep 10 '17 at 13:00
  • thanks for quick reply @kjhughes, @ imhotap so after xml validation with schema I think I need to find out other way to cross validate each element value, as xpath xsd in 1.1 will not be solution. I checked with stylesheets but of no avail – Yogi Sep 10 '17 at 14:00
  • *I checked with stylesheets but of no avail.* If you mean XSLT, you could certainly perform the additional value validation via XSLT. – kjhughes Sep 10 '17 at 14:30
  • There are two possible ways I can suggest to others who found limitations in xsd, validate schemas through SQL and creat test db with valid values for each element, with joint query entire things can be validated, 2) python can be best used to put above things in one script. – Yogi Sep 12 '17 at 08:11

0 Answers0