javax.xml.parsers.DocumentBuilder 对复杂类型的支持

发布于 2024-09-27 14:57:47 字数 1780 浏览 0 评论 0原文

我正在尝试整理一些验证代码。我正在尝试验证类似的模式:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:choice="http://example.com/SimpleChoice" targetNamespace="http://example.com/SimpleChoice" ecore:nsPrefix="choice" ecore:package="com.example.simple.choice">
    <xsd:complexType name="Plane">
        <xsd:sequence>
            <xsd:element name="freightDetails" type="xsd:string" minOccurs="0"/>
            <xsd:element name="passengers" type="xsd:int" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

使用以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<choice:Plane xmlns:choice="http://example.com/SimpleChoice">
    <freightDetails>Boxes</freightDetails>
</choice:Plane>

它似乎抱怨没有元素,但我正在尝试找到一种方法来验证类型。我收到以下错误:

[Error] :1:100: cvc-elt.1: Cannot find the declaration of element 'choice:Plane'.

当它尝试使用以下代码加载文档时:

SchemaFactory factory =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(schemaFile);

DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setSchema(schema);
parserFactory.setNamespaceAware(true);
DocumentBuilder parser = parserFactory.newDocumentBuilder();

Document document = parser.parse(inputSource);

Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));

当它到达: 时失败:

Document document = parser.parse(inputSource);

有谁知道我如何才能使其工作? (或者支持这种行为的验证器?)

谢谢

Rob

I'm trying to put together some validation code. I am trying to validate against a schema like:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:choice="http://example.com/SimpleChoice" targetNamespace="http://example.com/SimpleChoice" ecore:nsPrefix="choice" ecore:package="com.example.simple.choice">
    <xsd:complexType name="Plane">
        <xsd:sequence>
            <xsd:element name="freightDetails" type="xsd:string" minOccurs="0"/>
            <xsd:element name="passengers" type="xsd:int" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

With the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<choice:Plane xmlns:choice="http://example.com/SimpleChoice">
    <freightDetails>Boxes</freightDetails>
</choice:Plane>

It seems to complain that there is no element, but I'm trying to find a way to validate against the type. I get the following error:

[Error] :1:100: cvc-elt.1: Cannot find the declaration of element 'choice:Plane'.

When it tries to load the document with the following code:

SchemaFactory factory =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(schemaFile);

DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setSchema(schema);
parserFactory.setNamespaceAware(true);
DocumentBuilder parser = parserFactory.newDocumentBuilder();

Document document = parser.parse(inputSource);

Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));

It fails when it gets to the:

Document document = parser.parse(inputSource);

Does anyone have any ideas on how I would be able to get this working? (Or a validator that supports this sort of behaviour?)

Thanks

Rob

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

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

发布评论

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

评论(2

2024-10-04 14:57:47

这是因为您的架构已定义类型 (Plane),但尚未定义任何允许使用该类型的元素。类型本身在模式本身之外没有任何意义。

您需要将 添加到架构中。最简单的解决方案是在其中使用匿名复杂类型,例如:

<xsd:element name="Plane">
  <xsd:complexType>
    <xsd:sequence>
        <xsd:element name="freightDetails" type="xsd:string" minOccurs="0"/>
        <xsd:element name="passengers" type="xsd:int" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>    
</xsd:element>

This is because your schema has defined a type (Plane), but hasn't defined any permitted elements that use that type. A type on its own has no meaning outside of the schema itself.

You need to add an <xsd:element> to your schema. The simplest solution is to use an anonymous complex type within that, something like:

<xsd:element name="Plane">
  <xsd:complexType>
    <xsd:sequence>
        <xsd:element name="freightDetails" type="xsd:string" minOccurs="0"/>
        <xsd:element name="passengers" type="xsd:int" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>    
</xsd:element>
冷︶言冷语的世界 2024-10-04 14:57:47

在您的示例架构中,您仅定义了名为 Plane类型,而不是名为 Plane元素。将元素声明添加到您的架构中,看看会发生什么......

In your example schema, you have only defined a type named Plane, not an element named Plane. Add an element declaration to your schema and see what happens...

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