2017-05-22 44 views
0

我有以下XSD:我们可以提供XSD属性元数据吗?

<?xml version="1.0"?> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="https://www.w3schools.com" 
      xmlns="https://www.w3schools.com" 
      elementFormDefault="qualified"> 
    <xs:element name="rootNode" type="records" /> 
    <xs:complexType name="records"> 
     <xs:sequence> 
      <xs:element name="element1" type="type-attrbute-grp" /> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="type-attrbute-grp"> 
     <xs:attributeGroup ref="attribute-grp" /> 
    </xs:complexType> 
    <xs:attributeGroup name="attribute-grp"> 
     <xs:attribute name="scale" type="xs:int" use="required" /> 
     <xs:attribute name="date" type="xs:date" use="required" /> 
    </xs:attributeGroup> 
</xs:schema> 

而且我在下面创建XML:

<?xml version="1.0" encoding="UTF-8"?> 
<p:rootNode xmlns:p="https://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com test2.xsd "> 
    <p:element1 date="2001-01-01" scale="7"/> 
</p:rootNode> 

我们可以提供关于与属性的元素的详细信息。但是,我的问题是我们能否提供关于属性的元数据? 我的目标是在UI中将“element1”显示为表格行,将“date”/“scale”显示为表格中的列。另外,我想为缩放和日期列添加一些验证,以及我想在XSD中提供的信息。即什么验证器应该适用于缩放以及我想在缩放单元格上显示哪个小部件?

回答

1

您可以将自己的元数据添加到大多数xsd实体。你用它做什么然后由应用程序使用它。

在以下示例中,MyColumnInfo元素已附加到scale属性。

<xs:attribute name="scale" type="xs:int" use="required"> 
    <xs:annotation> 
    <xs:appinfo> 
     <MyColumnInfo width="10" xmlns="" /> 
    </xs:appinfo> 
    </xs:annotation> 
</xs:attribute> 

大多数SOM(模式对象模型)分析器将允许您访问此信息。但解析模式往往有点棘手,所以看起来像一个快速的工作可以迅速变成一个相当大的任务。

在.Net中,您有XsdSchema类,您可以将模式读入并导航它。在Java中,你可以使用xerces。

+0

谢谢Sprotty !!我尝试了2个解析器,一个来自“membrane-soa.org”,它没有正确解析注释,第二个来自“http://ws.apache.org/xmlschema/xmlschema-core/”,但它有点复杂。你知道除了jaxb以外的其他解析器吗? –

+1

Xerces有一个模型(https://xerces.apache.org/xerces2-j/javadocs/xs/org/apache/xerces/xs/XSModel.html),它是一个非常薄的层,可以覆盖实际的XML数据记得。 JABX是一个XML数据绑定器(还有其他一些),您可以自定义它以获取appInfo数据并对其执行操作。这取决于你试图达到的目标,以及你是否希望自己编写代码或希望得到一个开箱即用的解决方案。 – Sprotty

相关问题