我有 XSD,它描述了自定义模式并导入了 XLink(另一个模式)。
使用以下声明 ix main XSD 进行导入:
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
xlink.xsd
文件实际上位于主 XSD 附近。
然后我使用以下代码配置构建器
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String MY_SCHEMA_FILENAME = "mydir/myfile.xsd";
static final String MY_DATA_FILENAME = "mydir/myfile.xml";
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
try {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(MY_SCHEMA_FILENAME));
}
catch (IllegalArgumentException e) {
throw new AssertionError(e);
}
try {
builder = factory.newDocumentBuilder();
}
catch(ParserConfigurationException e) {
throw new AssertionError(e);
}
当我在内存中准备文档时,我通过以下方式设置属性
imageElement.setAttribute("xlink:href", mypathvariable);
我希望这将创建以下方式描述的标签XSD
<xs:element name="image">
<xs:complexType>
<xs:attribute ref="xlink:href" use="required"/>
</xs:complexType>
</xs:element>
虽然创建一切都没有任何错误,但同时使用代码保存
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(MY_DATA_FILENAME));
transformer.transform(source, result);
出现以下错误:
ERROR: 'Namespace for prefix 'xlink' has not been declared.'
我的错误在哪里?