XML Schema - 找不到元素的声明

时间:2015-04-25 15:58:23

标签: xml xsd schema xml-validation xml-error

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- definition of simple elements -->
    <xs:element name="tid" type="xs:positiveInteger"/>
    <xs:element name="tname" type="xs:string"/>
    <xs:element name="reviewDate" type="xs:date"/>
    <xs:element name="note" type="xs:string"/>

    <!-- definition of attributes -->
    <xs:attribute name="category" type="xs:string"/>
    <xs:attribute name="type" type="xs:string"/>
    <xs:attribute name="currentlyOffered" type="xs:string"/>
    <xs:attribute name="staffID">
        <xs:simpleType>
            <!-- Some Rules -->
        </xs:simpleType>
    </xs:attribute>

    <!-- definition of complex elements -->
    <xs:element name="product">
        <xs:complexType>
            <xs:sequence>
                <!-- Some Rules -->
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rootItem">
        <xs:complexType>
            <!-- Some Rules -->
        </xs:complexType>
    </xs:element>

</xs:schema>

XML Schema:

"Cannot find the declaration of element 'rootItem'.

我得到的回应是:

    public class QueryContext<T>
    {
        void Execute(MethodCallExpression dsQueryExpression)
        {
            var orderByFinder = new OrderByFinder();
            var orderByExpression = orderByFinder.GetOrderBy(dsQueryExpression);
            // .. Continue on processing the OrderBy expression
        }
    }

    internal class OrderByFinder : ExpressionVisitor
    {
        MethodCallExpression _orderByExpression;

        public MethodCallExpression GetOrderBy(Expression expression)
        {
            Visit(expression);
            return _orderByExpression;
        }

        protected override Expression VisitMethodCall(MethodCallExpression expression)
        {
            if (expression.Method.Name == "OrderBy") _orderByExpression = expression;

            Visit(expression.Arguments[0]);

            return expression;
        }
    }

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的xml实例使用未在架构中定义的默认命名空间xmlns="http://www.w3schools.com"。我不知道您的数据的哪一部分是可更改的,但您必须删除XML中的命名空间或将其添加为架构中的<xs:element name="rootItem" xmlns="http://www.w3schools.com">

(请查看Michael Kay的评论,其中提到了纠正模式文件的正确操作)

答案 1 :(得分:0)

将目标名称空间添加到架构:

<xs:schema targetNamespace="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">