org.w3c.dom.Document.getElementById не работает - PullRequest
0 голосов
/ 02 апреля 2019

У меня есть документ, строковое представление которого

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<model xmlns="http://magwas.rulez.org/zenta.3.0">
    <element id="thing"/>
</model>

xmlDocument.getElementById("thing") возвращает ноль.

Я пытался определить идентификатор как поле идентификатора в соответствии с этому ответу .

Моя схема выглядит так:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://magwas.rulez.org/zenta.3" 
        xmlns:zenta="http://magwas.rulez.org/zenta.3" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="model">
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="element" type="zenta:element"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="element">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:attribute name="id" use="required" type="xsd:ID"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:schema>

Соответствующий код:

final public static String XML_SCHEMA = "http://magwas.rulez.org/zenta.3.0";
private static Schema compiledSchema = createSchema();
private static DocumentBuilderFactory factory = createBuilderFactory();

public XmlUtils() {
    super();
}

public static Document createDocument() {

    DocumentBuilderFactory factory = createBuilderFactory();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
        Schema schema = builder.getSchema();
        System.out.printf("schema:%s\n",schema);
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new ReportedError("cannot create new document", e);
    }
}

private static DocumentBuilderFactory createBuilderFactory() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setSchema(compiledSchema);
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    factory.setNamespaceAware(true);
    return factory;
}

private static Schema createSchema() {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema compiledSchema;
    try {
        compiledSchema = schemaFactory.newSchema(XmlUtils.class.getClassLoader()
                .getResource("zenta.xsd"));
    } catch (SAXException e) {
        throw new ReportedError("cannot load zenta schema", e);
    }
    return compiledSchema;
}

Я проверил, что isId () имеет значение false для атрибута id элемента. Я также проверил, что схема фактически проанализирована: если я сделаю ее недопустимым xml, я получу ошибку.

Что я должен сделать, чтобы понять, что элемент id на самом деле должен быть идентификатором?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...