1

I can't find the first element simpleType from the schema:

<?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>

This is how I try to parse the 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;
    }
}

So if to pass parameter simpleType to the method nodeList has length equals 0 and the method returns null. Also these tests might help you:

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

UPDATE

Added tests:

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

What I exactly need is to find simpleType just anywhere in the schema.

nllsdfx
  • 900
  • 14
  • 30

1 Answers1

1

An XPath expression without a leading slash is a relative path that starts from the current Node. In your case that is the root node, so simpleType and /simpleType are equivalent, and not what you want. What you want is

//simpleType or /schema/simpleType

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • 1
    Some XPath tutorials are saying that without a leading slash, all nodes in the document are found, not only children of the current node ( aka context node). But if I understand https://www.w3.org/TR/2010/REC-xpath20-20101214/#abbrev correctly this is not the case, only children are selected. – kutschkem May 17 '18 at 14:32