This question already has answers here:
How does XPath deal with XML namespaces?
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
我无法从架构中找到第一个元素simpleType

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:simpleType name="IdType">
        <xs:annotation>
            <xs:documentation>Уникальный идентификатор (ключ) объекта в АИС контрагента</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:token">
            <xs:minLength value="1"/>
            <xs:maxLength value="60"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="DateType">
        <xs:annotation>
            <xs:documentation>Дата</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:date">
            <xs:minInclusive value="1900-01-01"/>
            <xs:maxExclusive value="2100-01-01"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="DateTimeType">
        <xs:annotation>
            <xs:documentation>Дата и время</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:dateTime">
            <xs:minInclusive value="1900-01-01T00:00:00"/>
            <xs:maxExclusive value="2100-01-01T00:00:00"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="String_1_1000">
        <xs:annotation>
            <xs:documentation>Строка длиной от 1 до 1000 символов</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="1000"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="DocumentNumberType">
        <xs:annotation>
            <xs:documentation>Номер документа</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="25"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="OrganizationCodeType">
        <xs:annotation>
            <xs:documentation>Код организации</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="32"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="DepartmentCodeType">
        <xs:annotation>
            <xs:documentation>Код подразделения</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="32"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="CaseNumberType">
        <xs:annotation>
            <xs:documentation>Номер дела</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="25"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="DocumentDataType">
        <xs:annotation>
            <xs:documentation>Тип документа электронного документооборота</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="32"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="StateIdType">
        <xs:annotation>
            <xs:documentation>Состояние документа</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="90"/>
            <xs:enumeration value="91"/>
            <xs:enumeration value="92"/>
            <xs:enumeration value="93"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="AttachmentFilenameType">
        <xs:annotation>
            <xs:documentation>Имя файла вложения</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:ID">
            <xs:maxLength value="1024"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>


这是我尝试解析xml的方式:

public Node getFirstByExpression(final String expr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);

    try {

         DocumentBuilder builder = factory.newDocumentBuilder();
         document = builder.parse(source);
         document.getDocumentElement().normalize();

         XPathFactory factory = XPathFactory.newInstance();
         XPath xPath = factory.newXPath();
         NodeList nodeList = null;

         XPathExpression expression = xPath.compile(expr);
         nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
         return nodeList.getLength() == 0 ? null : nodeList.item(0);

    } catch (Exception ex) {
            log.error(ex);
            return null;
    }
}


因此,如果将参数simpleType传递给方法nodeList的长度等于0,并且该方法返回null。这些测试也可以帮助您:

assert parser.getFirstByExpression("schema") != null; // true

assert parser.getFirstByExpression("/schema") != null; // true

assert parser.getFirstByExpression("//schema") != null; // false, seems it must be true

assert parser.getFirstByExpression("simpleType") != null; // false, need to be true

更新

新增测试:

assert parser.getFirstByExpression("//simpleType") != null; // false
assert parser.getFirstByExpression("/schema/simpleType") != null; // true

我真正需要的是在架构中的任何位置找到simpleType

最佳答案

不带斜杠的XPath表达式是从当前Node开始的相对路径。在您的情况下,这是根节点,因此simpleType/simpleType是等效的,而不是您想要的。你想要的是

//simpleType
要么
/schema/simpleType

07-28 00:35