xsd:boolean element type accept "true" but not "True". How can I make it accept it?

XmlXsdBoolean

Xml Problem Overview


I am using xmllint --schema option to validate my XML that looks like this

<XML>
<Active>True</Active>
</XML>

In my schema file, I have following line that describes Active element.

<xsd:element name="Active" type="xs:boolean" />

When I run xmllint, I get error messages that says

> /tmp/schema_validation.xml:73: element > Active: Schemas validity error : > Element 'Active': 'True' is not a > valid value of the atomic type > 'xs:boolean'.

When I change the XML to

<Active>true</Active>

Then the error message disappears.

So, it looks like xsd:boolean means it's all lowercase "true/false" but not "True/False" to xmllint.. My question is, how can I make xmllint to accept "True" for xsd:boolean type? Or is there different tools that I can use that will validate this XML? Changing the XML or schema is not my option at this point.

Thanks!

Xml Solutions


Solution 1 - Xml

You cannot.

According to the XML Schema specification, a boolean is true or false. True is not valid:

3.2.2.1 Lexical representation An instance of a datatype that is defined as ·boolean· can have the following legal literals {true, false, 1, 0}.

3.2.2.2 Canonical representation The canonical representation for boolean is the set of literals {true, false}.

If the tool you are using truly validates against the XML Schema standard, then you cannot convince it to accept True for a boolean.

Solution 2 - Xml

xs:boolean is predefined with regard to what kind of input it accepts. If you need something different, you have to define your own enumeration:

 <xs:simpleType name="my:boolean">
    <xs:restriction base="xs:string">
      <xs:enumeration value="True"/>
      <xs:enumeration value="False"/>
    </xs:restriction>
  </xs:simpleType>

Solution 3 - Xml

If you're on Linux, or have cygwin available on Windows, you can run the input XML through a simple sed script that will replace <Active>True</Active> with <Active>true</Active>, like so:

cat <your XML file> | sed 'sX<Active>True</Active>X<Active>true</Active>X' | xmllint --schema -

If you're not, you can still use a non-validating xslt pocessor (xalan, saxon etc.) to run a simple xslt transformation on the input, and only then pipe it to xmllint.

What the xsl should contain something like below, for the example you listed above (the xslt processor should be 2.0 capable):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
    <xsl:for-each select="XML">
        <xsl:for-each select="Active">
            <xsl:value-of select=" replace(current(), 'True','true')"/>
        </xsl:for-each>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoichi HayashiView Question on Stackoverflow
Solution 1 - XmlCheesoView Answer on Stackoverflow
Solution 2 - XmlPavel MinaevView Answer on Stackoverflow
Solution 3 - XmlAnonymous CowardView Answer on Stackoverflow