从模式中确定元素和属性的基本类型

发布于 2024-10-18 06:35:02 字数 675 浏览 1 评论 0原文

我已经给出了元素或属性的本地名称以及文档的架构。确定元素或属性的基本数据类型的最简单方法是什么。我所说的基本数据类型是指 xs:string、xs:date 等。(xml 模式的内置数据类型。)

我面临的问题之一是元素类型很少成为基本内置数据类型之一 。类型。 99% 的情况下它是一个复杂类型,50% 的情况下它引用另一个复杂类型,而另一个复杂类型又引用另一个复杂类型,依此类推。

此模式的一个简单示例:我想查找 Employee/Person/Name/LastName 的基本类型(确定 LastName 为 xs:normalizedString)。 在架构中,Employee 被定义为 xs:element 且 type="bns:EmployeeType"

EmplyeeType 定义了一个 Person 元素,但它是类型“PersonType”,然后 Person 中的 Name 是 NameType,它是扩展 GeneralName 类型的复杂类型,即type BasicNameType 并且该类型最终定义了 LastName,其类型为“LastNameType”等等。还有定义等。

我目前正在使用 linq-to-xml 编写一个解析器来解决这个问题,但这并不容易或漂亮。我已经搜索过其他解决方案,但没有找到任何解决方案,但我完全承认我对 XML/schema/XPath 的无知。

有没有一种简单的方法来获取元素的基本类型?

I have given the local name of an element or attribute and the schema for the document. What is the easiest way to determine the basic datatype of the element or attribute. By basic datatype I mean the xs:string, xs:date etc. (The built in data types for the xml schema.)

One of the problems I face is that it is rare for the elements type to be one of the basic built in types. 99% of the time it is a complex type that 50% of the time refers to another complex type that refers to another complex type and so on.

A simple Example for this schema: I want to find the basic type for Employee/Person/Name/LastName (determine that LastName is xs:normalizedString).
In the schema Employee is defined as an xs:element and type="bns:EmployeeType"

EmplyeeType has a Person element defined but it is type "PersonType" and then Name in person is NameType which is a complex type that extends GeneralName type that is type BasicNameType and that type finally defines the LastName which is of type "LastNameType" and on and on. There also definitions etc.

I am currently writing a parser using linq-to-xml to get at this but it isn't easy or pretty. I have searched for other solutions and haven't found any but I fully admit my XML/schema/XPath ignorance.

Is there an easy way to get the basic type for elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-10-25 06:35:02

.NET 框架有一个架构对象模型 (SOM),并且有在使用 XmlReader 进行验证时或在验证 System.Xml.XmlDocument 或 System.Xml.Linq.XDocument 后访问架构类型的挂钩。下面是一个示例,展示了如何验证 System.Xml.Linq.XDocument 以及如何访问架构信息:

    Dim doc As XDocument = XDocument.Load("..\..\XMLFile1.xml")
Dim schemaSet As New XmlSchemaSet()
schemaSet.Add(Nothing, "..\..\XMLFile1.xsd")

doc.Validate(schemaSet, Nothing, True)

For Each leafElement As XElement In doc.Descendants().Where(Function(d) Not (d.Elements().Any()))
    Console.WriteLine("Element named {0} has type {1}", leafElement.Name, DirectCast(leafElement.GetSchemaInfo().SchemaType, XmlSchemaSimpleType).Datatype.TypeCode)

Next

XML 文件

<?xml version="1.0" encoding="utf-8" ?>
<persons>
  <person>
    <last-name>  Watson  </last-name>
    <foo>false</foo>
  </person>
</persons>

和架构是

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="persons">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="person" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="last-name" type="xs:normalizedString" />
              <xs:element name="foo" type="xs:boolean"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

示例输出

Element named last-name has type NormalizedString
Element named foo has type Boolean

因此,请浏览 MSDN 或本地 VS 文档中的 SOM 文档,您应该能够通过这种方式找到信息。

The .NET framework has a schema object model (SOM) and there are hooks to access schema types while validating with an XmlReader or after validating a System.Xml.XmlDocument or a System.Xml.Linq.XDocument. Here is a sample that show how to validate a System.Xml.Linq.XDocument and how to then access schema information:

    Dim doc As XDocument = XDocument.Load("..\..\XMLFile1.xml")
Dim schemaSet As New XmlSchemaSet()
schemaSet.Add(Nothing, "..\..\XMLFile1.xsd")

doc.Validate(schemaSet, Nothing, True)

For Each leafElement As XElement In doc.Descendants().Where(Function(d) Not (d.Elements().Any()))
    Console.WriteLine("Element named {0} has type {1}", leafElement.Name, DirectCast(leafElement.GetSchemaInfo().SchemaType, XmlSchemaSimpleType).Datatype.TypeCode)

Next

With the XML file being

<?xml version="1.0" encoding="utf-8" ?>
<persons>
  <person>
    <last-name>  Watson  </last-name>
    <foo>false</foo>
  </person>
</persons>

and the schema being

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="persons">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="person" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="last-name" type="xs:normalizedString" />
              <xs:element name="foo" type="xs:boolean"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

that sample outputs

Element named last-name has type NormalizedString
Element named foo has type Boolean

So explore the SOM documentation on MSDN or in your local VS documentation, you should be able to find the information that way.

揽月 2024-10-25 06:35:02

不要尝试手动执行此操作。

我不熟悉 Linq,因此可能还有其他方法可以做到这一点,但一种方法是使用模式感知 XSLT 或 XQuery。如果您使用模式感知的 XSLT 或 XQuery 处理器处理经过验证的文档,那么您将能够执行以下测试:

if (. instanceof attribute(*, xs:normalizedString)) ...

如果上下文节点是针对控制类型为 xs:normalizedString 的属性声明进行验证的属性节点,则哪个为 true。

Don't attempt to do this by hand.

I'm not familiar with Linq, so there might be other ways of doing this, but one way is to use schema-aware XSLT or XQuery. If you process your validated document using a schema-aware XSLT or XQuery processor then you will be able to do tests like

if (. instanceof attribute(*, xs:normalizedString)) ...

which is true if the context node is an attribute node that was validated against an attribute declaration whose governing type is xs:normalizedString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文