Given an XML file and associated XSD, I'd like to find the XSD for elements in that XML instance

我怕爱的太早我们不能终老 提交于 2019-12-24 13:27:46

问题


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>

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 -

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>

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.

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

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?"


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/17280282/given-an-xml-file-and-associated-xsd-id-like-to-find-the-xsd-for-elements-in-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!