我有一个XSD文件:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="someNameSpace"
xmlns="someNameSpace"
elementFormDefault="qualified">
...
</xs:schema>
在Java代码中:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
dbf.setIgnoringComments(true);
dbf.setFeature("http://apache.org/xml/features/validation/schema", true);
dbf.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation", "someNameSpace file:///home/.../schema.xsd");
DocumentBuilder builder = dbf.newDocumentBuilder();
builder.setErrorHandler(new SomeHandlerImpl());
Document doc = builder.parse(input);
输入是一个以:
开头的文件<?xml version="1.0" encoding="UTF-8"?>
<projekt xmlns="someNameSpace">
如果我删除了namsepaces并使用http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
属性而不是http://apache.org/xml/properties/schema/external-schemaLocation
,那么它可以正常工作。但是对于命名空间我完全不知道,我该怎么做。
答案 0 :(得分:0)
我已经解决了。架构应该像这样开始:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://some/namespace/identifier"
xmlns="http://some/namespace/identifier"
elementFormDefault="qualified">
root start标签是这样定义的:
<rootElement xmlns="http://some/namespace/identifier"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://some/namespace/identifier whatever.xsd">
最后我更新了我的代码:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
SchemaFactory scf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
URL schemaUrl = getClass().getResource(SCHEMAFILE);
dbf.setNamespaceAware(true);
dbf.setSchema(schema);
DocumentBuilder builder = dbf.newDocumentBuilder();
builder.setErrorHandler(errHandler);
Document doc = builder.parse(input);