给定一个 XML 文件和关联的 XSD,我想找到该 XML 实例中元素的 XSD [英] Given an XML file and associated XSD, I'd like to find the XSD for elements in that XML instance

查看:28
本文介绍了给定一个 XML 文件和关联的 XSD,我想找到该 XML 实例中元素的 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些软件来生成用户界面,用于将数据输入到非常复杂的 XML 文件中.我有一个输入文本文件,它描述了要呈现给用户的 GUI 字段,以及 XML 文件中元素存储的 XPath.因此,例如,给定以下 XML:

I am developing some software that generates a user interface for inputting data into a very complicated XML file. I have an input text file that describes what GUI fields to present to the user, and the XPath to the element's storage in the XML file. So, for example, given the following XML:

<rootDoc>
    <A>
        <B someAttr="Hello"/>
   </A>
</rootDoc>

我可能会显示一个带有文本框的 GUI 字段,其 XPath 为/A/B/@someAttr".通过这种方式,我知道从哪里读取原始值以显示在文本框中,然后我还知道当用户单击确定"时将其存储回哪里.这一切都很好,而且我已经开始工作了.现在的问题 -

I might show a GUI field with a text box whose XPath is "/A/B/@someAttr". In this way, I know where to read the original value from to show in the text box, and then I also know where to store it back when the user clicks "OK." This is all good and well, and I have it working. Now for the question -

有大量 XSD 描述了 XML 文件的结构、需要哪些字段以及有关字符串字段的数据(即最大字段长度等...)所以,对于我上面的非常简单的示例, 可能有如下所示的 XSD(如果这不是有效的 XSD,我深表歉意,我只是想说明这一点......):

There is a great deal of XSD that describes how the XML file is structured, what fields are required, and data about the string fields (ie, max field length, etc...) So, for my very simple example above, there might be an XSD like the following (I apologize if this is not valid XSD, I am just trying to get the point across...):

<schema>
    <complexType name="B">
        <complexContent>
            <attribute name="someAttr" use="required"/>
        </complexContent>
    </complexType>

    <complexType name="A">
        <sequence>
            <element name="B" type="B" minOccurs="0"/>
        </sequence>
     </complexType>
</schema>

现在,鉴于我知道 GUI 字段的 XPath 是/A/B/@someAttr,在 .NET 中是否有某种方法可以获取与 @someAttr 关联的 XSD 元素?例如,我会使用它来验证该字段的格式是否正确;我还可以根据需要标记"GUI 字段等.请注意,我不是在寻找如何针对 XSD 验证 XML——我希望在显示 GUI 时进行处理",以便我可以立即在 GUI 上标记所需字段,或将最大字段长度设置为 XSD 中指定的长度等.

Now, given that I know the XPath to the GUI field is /A/B/@someAttr, is there some way in .NET to get the XSD element associated with @someAttr? I'd use this, for example, to then validate that the field is formatted correctly; I could also "mark" the GUI field as required, etc. Note that I'm not looking for how to validate the XML against the XSD -- I am looking to do my "processing" when the GUI is displayed, so that I can immediately mark required fields on the GUI, or set the max field length to the one denoted in the XSD, etc.

请注意,我是用 C# 编写代码的,所以希望能提供 .NET 特定的指针.

Please note that I am coding this in C# so would appreciate .NET-specific pointers.

已另一种表述方式可能是,给定一个 XSD 和一个表示从该 XSD 描述的 XML 中选择的元素或属性的 xpath,是否有办法获取 XSD 块?表示将由 xpath 表达式选择的元素或属性的架构?"

Edited: Another way to state this might be, "given an XSD, and an xpath that represents a selection of an element or attribute from XML that is described by that XSD, is there a way to get the chunk of XSD that represents the schema for that element or attribute that would be selected by the xpath expression?"

推荐答案

这是看起来很简单但其实不然的问题之一.将模式类型分配给标签的过程称为类型关联",它对标签出现的上下文敏感.事实证明,除非您从一开始到标签出现点对文档进行模式验证,否则您无法预测哪种模式类型适用于特定标签.

This is one of those problems that looks simple, but is not. The process of assigning a schema type to a tag is called 'type association' and it is sensitive to the context in which the tag appears. It turns out that you cannot predict the which schema type applies to a particular tag unless you perform schema validation of the document from the beginning up to the point where the tag appears.

W3C 联盟(XML 和 XML 架构规范的作者)已经指定了 XML 处理器应该如何公开其类型关联信息.该信息称为Post Schema Validation Infoset"或简称 PSVI.

The W3C consortium ( authors of XML and XML Schema specifications ) have specified how an XML processor should expose its type association information. The information is called 'Post Schema Validation Infoset' or PSVI for short.

看起来好像在 .Net 中支持 PSVI:https://msdn.microsoft.com/en-us/library/system.xml.schema.extensions%28v=vs.110%29.aspx

Looks as if there is support in .Net for PSVI: https://msdn.microsoft.com/en-us/library/system.xml.schema.extensions%28v=vs.110%29.aspx

这篇关于给定一个 XML 文件和关联的 XSD,我想找到该 XML 实例中元素的 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆