xml xsd:boolean 元素类型接受“true”但不接受“True”。我怎样才能让它接受它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1308491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xsd:boolean element type accept "true" but not "True". How can I make it accept it?
提问by Soichi Hayashi
I am using xmllint --schema option to validate my XML that looks like this
我正在使用 xmllint --schema 选项来验证我的 XML,看起来像这样
<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
当我运行 xmllint 时,我收到错误消息说
/tmp/schema_validation.xml:73: element Active: Schemas validity error : Element 'Active': 'True' is not a valid value of the atomic type 'xs:boolean'.
/tmp/schema_validation.xml:73:元素活动:架构有效性错误:元素“活动”:“真”不是原子类型“xs:boolean”的有效值。
When I change the XML to
当我将 XML 更改为
<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.
所以,它看起来像 xsd:boolean 意味着它都是小写的“真/假”而不是“真/假”到 xmllint ..我的问题是,我怎样才能让 xmllint 接受 xsd:boolean 类型的“真”?或者我可以使用不同的工具来验证这个 XML?此时更改 XML 或架构不是我的选择。
Thanks!
谢谢!
回答by Cheeso
You cannot.
你不能。
According to the XML Schema specification, a boolean is trueor false. Trueis not valid:
根据XML Schema 规范,布尔值是trueor false。 True无效:
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.
如果您使用的工具确实根据 XML 模式标准进行了验证,那么您无法说服它接受布尔值的 True。
回答by Pavel Minaev
xs:booleanis predefined with regard to what kind of input it accepts. If you need something different, you have to define your own enumeration:
xs:boolean关于它接受什么样的输入是预定义的。如果您需要不同的东西,则必须定义自己的枚举:
<xs:simpleType name="my:boolean">
<xs:restriction base="xs:string">
<xs:enumeration value="True"/>
<xs:enumeration value="False"/>
</xs:restriction>
</xs:simpleType>
回答by Anonymous Coward
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:
如果您在 Linux 上,或者在 Windows 上有 cygwin,您可以通过一个简单的 sed 脚本运行输入 XML,该脚本将替换<Active>True</Active>为<Active>true</Active>,如下所示:
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.
如果不是,您仍然可以使用非验证 xslt 处理程序(xalan、saxon 等)对输入运行简单的 xslt 转换,然后才将其通过管道传输到 xmllint。
What the xsl should contain something like below, for the example you listed above (the xslt processor should be 2.0 capable):
对于上面列出的示例,xsl 应包含的内容如下所示(xslt 处理器应支持 2.0):
<?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>

