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

后端 未结 3 2038
余生分开走
余生分开走 2020-12-15 02:20

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


True

In my

相关标签:
3条回答
  • 2020-12-15 03:07

    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.

    0 讨论(0)
  • 2020-12-15 03:10

    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>
    
    0 讨论(0)
  • 2020-12-15 03:11

    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>
    
    0 讨论(0)
提交回复
热议问题