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 架构中收到以下错误:

我在 XML 文件中收到以下错误:

我不知道为什么没有定义'childrenType',因为它在XSD文档中进一步定义。我怀疑这是一个命名空间问题。

我试图用 XSD 模式表示的内容:

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>

由于您的编辑,您的问题是可以回答的。但是还存在几个问题:

  1. 您将 <case_sensitive> 元素的类型定义为 xs:boolean,但您的内容用引号引起来,即 "true" 而不是 true.这是无效的;见 this SO answer regarding boolean values in XSD.

  2. 您缺少所有用户定义类型的命名空间。因此,将与目标名称空间具有相同值的 xmlns 定义添加到 xs:schema 元素。所以添加

    xmlns:tns="http://www.w3schools.com"
    

    xs:schema 元素。您需要将类型放入命名空间。

  3. 现在将命名空间前缀 tns 添加到所有 user-defined 类型以在命名空间中引用它们,即用 [= 引用类型 childrenType 25=].

  4. 最后,为您的根类型添加一个根元素。所以添加

    <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。如果你不能改变它,你有三个选择:

  1. 应用 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(.,'&quot;','')" />
        </xsl:template>
    
    </xsl:stylesheet>
    
  2. 将元素 <case_sensitive> 的类型更改为 xs:string

  3. 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" />
    

完成所有这些后,验证将成功