XML 文件,XSD 模式错误:无法将名称解析为 a(n) 'type definition',找不到元素的声明
XML file, XSD schema Errors: Cannot resolve the name to a(n) 'type definition', Cannot find the declaration of element
我是 XML 和 XML 模式的新手。我设置了一个 XML 文件和一个 XML 模式。我不是 100% 熟悉命名空间。
我在 XML 架构中收到以下错误:
- src-resolve: 无法将名称 'childrenType' 解析为 a(n) 'type definition' component.xsd(src-resolve)
- src-resolve.4.1:解析组件 'childrenType' 时出错。检测到 'childrenType' 没有命名空间,但没有目标命名空间的组件无法从架构文档 'file:///C:/XML/file_root.xsd' 中引用。如果 'childrenType' 打算有一个命名空间,也许需要提供一个前缀。如果打算 'childrenType' 没有名称空间,则应将没有 "namespace" 属性的 'import' 添加到 'file:///C:/XML/file_root.xsd'.xsd(src-resolve .4.1)
我在 XML 文件中收到以下错误:
- cvc-elt.1.a: 找不到元素 'BinaryExpressionTreeBoolean'.xml(cvc-elt.1.a)[=46 的声明=]
我不知道为什么没有定义'childrenType',因为它在XSD文档中进一步定义。我怀疑这是一个命名空间问题。
我试图用 XSD 模式表示的内容:
- 表示一棵树 (BinaryExpressionTreeBoolean)
- 树只有一个根节点
- 树中的任何节点只能是两种类型(NodeOperandType 或 NodeStringExpressionType)
- 节点类型NodeOperandType是唯一可以有children的节点,要求至少有一个child。 NodeStringExpressionType 不能有任何 children.
- 节点类型 NodeOperandType 具有以下元素:操作数(必需)、测试(必需)和任何 children(如前所述)。
- 节点类型NodeStringExpressionType有以下必需元素:字符串表达式,测试,测试字符串,测试是否区分大小写。
XSD 文件:
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified">
<xs:complexType name="NodeOperandType">
<xs:sequence>
<!-- <xs:element name="operand" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="children" type="childrenType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeStringExpressionType">
<xs:sequence>
<!-- <xs:element name="string_expression" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="test_string" type="xs:string" />
<xs:element name="case_sensitive" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeType">
<xs:choice>
<xs:element name="NodeOperand" type="NodeOperandType" minOccurs="1" maxOccurs="1" />
<xs:element name="NodeStringExpression" type="NodeStringExpressionType" minOccurs="1" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
<xs:complexType name="childrenType">
<xs:sequence>
<xs:element name="Node" type="NodeType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BinaryExpressionTreeBoolean">
<xs:sequence>
<xs:element name="Node" type="NodeType" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
XML 文件:
<?xml version="1.0" ?>
<BinaryExpressionTreeBoolean xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com file:///C:/XML/file_root.xsd">
<Node>
<NodeOperandType>
<test>"or"</test>
<children>
<Node>
<NodeStringExpressionType>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>"true"</case_sensitive>
</NodeStringExpressionType>
</Node>
<Node>
<NodeStringExpressionType>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>"true"</case_sensitive>
</NodeStringExpressionType>
</Node>
</children>
</NodeOperandType>
</Node>
</BinaryExpressionTreeBoolean>
接受答案后更新:
最终的 XML 文件如下所示,供访问此问题的其他人将来参考。
<?xml version="1.0" ?>
<BinaryExpressionTreeBoolean xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com file:///C:/XML/file_root.xsd">
<Node>
<NodeOperand>
<test>"or"</test>
<children>
<Node>
<NodeStringExpression>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>true</case_sensitive>
</NodeStringExpression>
</Node>
<Node>
<NodeStringExpression>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>true</case_sensitive>
</NodeStringExpression>
</Node>
</children>
</NodeOperand>
</Node>
</BinaryExpressionTreeBoolean>
由于您的编辑,您的问题是可以回答的。但是还存在几个问题:
您将 <case_sensitive>
元素的类型定义为 xs:boolean
,但您的内容用引号引起来,即 "true"
而不是 true
.这是无效的;见 this SO answer regarding boolean values in XSD.
您缺少所有用户定义类型的命名空间。因此,将与目标名称空间具有相同值的 xmlns
定义添加到 xs:schema
元素。所以添加
xmlns:tns="http://www.w3schools.com"
到 xs:schema
元素。您需要将类型放入命名空间。
现在将命名空间前缀 tns
添加到所有 user-defined 类型以在命名空间中引用它们,即用 [= 引用类型 childrenType
25=].
最后,为您的根类型添加一个根元素。所以添加
<xs:element name="BinaryExpressionTreeBoolean" type="tns:BinaryExpressionTreeBooleanType" />
到 XSD 文件,这需要您将 xs:complexType
的名称从 BinaryExpressionTreeBoolean
更改为 BinaryExpressionTreeBooleanType
。
结合所有这些更改,您的 XSD 可能如下所示:
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified" xmlns:tns="http://www.w3schools.com">
<xs:complexType name="NodeOperandType">
<xs:sequence>
<!-- <xs:element name="operand" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="children" type="tns:childrenType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeStringExpressionType">
<xs:sequence>
<!-- <xs:element name="string_expression" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="test_string" type="xs:string" />
<xs:element name="case_sensitive" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeType">
<xs:sequence>
<xs:choice>
<xs:element name="NodeOperandType" type="tns:NodeOperandType" minOccurs="1" maxOccurs="1" />
<xs:element name="NodeStringExpressionType" type="tns:NodeStringExpressionType" minOccurs="1" maxOccurs="unbounded" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="childrenType">
<xs:sequence>
<xs:element name="Node" type="tns:NodeType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BinaryExpressionTreeBooleanType">
<xs:sequence>
<xs:element name="Node" type="tns:NodeType" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="BinaryExpressionTreeBoolean" type="tns:BinaryExpressionTreeBooleanType" />
</xs:schema>
并且此 XSD-1.0 文件可以在您将所有布尔字符串值从 "true"
更改为 true
以及从 [=31 之后验证您的上述 XML =] 到 false
。如果你不能改变它,你有三个选择:
应用 pre-processing XSLT 文件将这些值更改为有效值。例如,这可以通过以下模板实现:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.w3schools.com" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tns:case_sensitive/text()">
<xsl:value-of select="translate(.,'"','')" />
</xsl:template>
</xsl:stylesheet>
将元素 <case_sensitive>
的类型更改为 xs:string
。
用xs:pattern
表示类型。为此,请添加以下类型:
<xs:simpleType name="caseType">
<xs:restriction base="xs:string">
<xs:pattern value='true|false|\"true\"|\"false\"|1|0'></xs:pattern>
</xs:restriction>
</xs:simpleType>
然后在相关元素中引用此类型:
<xs:element name="case_sensitive" type="tns:caseType" />
完成所有这些后,验证将成功。
我是 XML 和 XML 模式的新手。我设置了一个 XML 文件和一个 XML 模式。我不是 100% 熟悉命名空间。
我在 XML 架构中收到以下错误:
- src-resolve: 无法将名称 'childrenType' 解析为 a(n) 'type definition' component.xsd(src-resolve)
- src-resolve.4.1:解析组件 'childrenType' 时出错。检测到 'childrenType' 没有命名空间,但没有目标命名空间的组件无法从架构文档 'file:///C:/XML/file_root.xsd' 中引用。如果 'childrenType' 打算有一个命名空间,也许需要提供一个前缀。如果打算 'childrenType' 没有名称空间,则应将没有 "namespace" 属性的 'import' 添加到 'file:///C:/XML/file_root.xsd'.xsd(src-resolve .4.1)
我在 XML 文件中收到以下错误:
- cvc-elt.1.a: 找不到元素 'BinaryExpressionTreeBoolean'.xml(cvc-elt.1.a)[=46 的声明=]
我不知道为什么没有定义'childrenType',因为它在XSD文档中进一步定义。我怀疑这是一个命名空间问题。
我试图用 XSD 模式表示的内容:
- 表示一棵树 (BinaryExpressionTreeBoolean)
- 树只有一个根节点
- 树中的任何节点只能是两种类型(NodeOperandType 或 NodeStringExpressionType)
- 节点类型NodeOperandType是唯一可以有children的节点,要求至少有一个child。 NodeStringExpressionType 不能有任何 children.
- 节点类型 NodeOperandType 具有以下元素:操作数(必需)、测试(必需)和任何 children(如前所述)。
- 节点类型NodeStringExpressionType有以下必需元素:字符串表达式,测试,测试字符串,测试是否区分大小写。
XSD 文件:
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified">
<xs:complexType name="NodeOperandType">
<xs:sequence>
<!-- <xs:element name="operand" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="children" type="childrenType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeStringExpressionType">
<xs:sequence>
<!-- <xs:element name="string_expression" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="test_string" type="xs:string" />
<xs:element name="case_sensitive" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeType">
<xs:choice>
<xs:element name="NodeOperand" type="NodeOperandType" minOccurs="1" maxOccurs="1" />
<xs:element name="NodeStringExpression" type="NodeStringExpressionType" minOccurs="1" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
<xs:complexType name="childrenType">
<xs:sequence>
<xs:element name="Node" type="NodeType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BinaryExpressionTreeBoolean">
<xs:sequence>
<xs:element name="Node" type="NodeType" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
XML 文件:
<?xml version="1.0" ?>
<BinaryExpressionTreeBoolean xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com file:///C:/XML/file_root.xsd">
<Node>
<NodeOperandType>
<test>"or"</test>
<children>
<Node>
<NodeStringExpressionType>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>"true"</case_sensitive>
</NodeStringExpressionType>
</Node>
<Node>
<NodeStringExpressionType>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>"true"</case_sensitive>
</NodeStringExpressionType>
</Node>
</children>
</NodeOperandType>
</Node>
</BinaryExpressionTreeBoolean>
接受答案后更新:
最终的 XML 文件如下所示,供访问此问题的其他人将来参考。
<?xml version="1.0" ?>
<BinaryExpressionTreeBoolean xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com file:///C:/XML/file_root.xsd">
<Node>
<NodeOperand>
<test>"or"</test>
<children>
<Node>
<NodeStringExpression>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>true</case_sensitive>
</NodeStringExpression>
</Node>
<Node>
<NodeStringExpression>
<test>"include"</test>
<test_string>"simple"</test_string>
<case_sensitive>true</case_sensitive>
</NodeStringExpression>
</Node>
</children>
</NodeOperand>
</Node>
</BinaryExpressionTreeBoolean>
由于您的编辑,您的问题是可以回答的。但是还存在几个问题:
您将
<case_sensitive>
元素的类型定义为xs:boolean
,但您的内容用引号引起来,即"true"
而不是true
.这是无效的;见 this SO answer regarding boolean values in XSD.您缺少所有用户定义类型的命名空间。因此,将与目标名称空间具有相同值的
xmlns
定义添加到xs:schema
元素。所以添加xmlns:tns="http://www.w3schools.com"
到
xs:schema
元素。您需要将类型放入命名空间。现在将命名空间前缀
tns
添加到所有 user-defined 类型以在命名空间中引用它们,即用 [= 引用类型childrenType
25=].最后,为您的根类型添加一个根元素。所以添加
<xs:element name="BinaryExpressionTreeBoolean" type="tns:BinaryExpressionTreeBooleanType" />
到 XSD 文件,这需要您将
xs:complexType
的名称从BinaryExpressionTreeBoolean
更改为BinaryExpressionTreeBooleanType
。
结合所有这些更改,您的 XSD 可能如下所示:
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified" xmlns:tns="http://www.w3schools.com">
<xs:complexType name="NodeOperandType">
<xs:sequence>
<!-- <xs:element name="operand" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="children" type="tns:childrenType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeStringExpressionType">
<xs:sequence>
<!-- <xs:element name="string_expression" type="xs:string" /> -->
<xs:element name="test" type="xs:string" />
<xs:element name="test_string" type="xs:string" />
<xs:element name="case_sensitive" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="NodeType">
<xs:sequence>
<xs:choice>
<xs:element name="NodeOperandType" type="tns:NodeOperandType" minOccurs="1" maxOccurs="1" />
<xs:element name="NodeStringExpressionType" type="tns:NodeStringExpressionType" minOccurs="1" maxOccurs="unbounded" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="childrenType">
<xs:sequence>
<xs:element name="Node" type="tns:NodeType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BinaryExpressionTreeBooleanType">
<xs:sequence>
<xs:element name="Node" type="tns:NodeType" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="BinaryExpressionTreeBoolean" type="tns:BinaryExpressionTreeBooleanType" />
</xs:schema>
并且此 XSD-1.0 文件可以在您将所有布尔字符串值从 "true"
更改为 true
以及从 [=31 之后验证您的上述 XML =] 到 false
。如果你不能改变它,你有三个选择:
应用 pre-processing XSLT 文件将这些值更改为有效值。例如,这可以通过以下模板实现:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.w3schools.com" version="1.0"> <xsl:output indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="tns:case_sensitive/text()"> <xsl:value-of select="translate(.,'"','')" /> </xsl:template> </xsl:stylesheet>
将元素
<case_sensitive>
的类型更改为xs:string
。用
xs:pattern
表示类型。为此,请添加以下类型:<xs:simpleType name="caseType"> <xs:restriction base="xs:string"> <xs:pattern value='true|false|\"true\"|\"false\"|1|0'></xs:pattern> </xs:restriction> </xs:simpleType>
然后在相关元素中引用此类型:
<xs:element name="case_sensitive" type="tns:caseType" />
完成所有这些后,验证将成功。