Как проверить XML-файл, используя Java с XSD, имеющим include?

Я использую Java 5 javax.xml.validation.Validator для проверки XML-файла. Я сделал это для одной схемы, которая использует только импорт, и все работает отлично. Теперь я пытаюсь проверить с другой схемой, которая использует импорт и один включить. У меня проблема в том, что элемент в главной схеме игнорируется, проверка утверждает, что не может найти их объявление.

Вот как я строю схему:

InputStream includeInputStream = getClass().getClassLoader().getResource("include.xsd").openStream();
InputStream importInputStream = getClass().getClassLoader().getResource("import.xsd").openStream();
InputStream mainInputStream = getClass().getClassLoader().getResource("main.xsd").openStream();
Source[] sourceSchema = new SAXSource[]{includeInputStream , importInputStream, 
mainInputStream };
Schema schema = factory.newSchema(sourceSchema);

Теперь вот выдержка из объявления в main.xsd

<xsd:schema xmlns="http://schema.omg.org/spec/BPMN/2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:import="http://www.foo.com/import" targetNamespace="http://main/namespace" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.foo.com/import" schemaLocation="import.xsd"/>
    <xsd:include schemaLocation="include.xsd"/>
    <xsd:element name="element" type="tElement"/>
    <...>
</xsd:schema>

Если я скопирую код моего XSD в main.xsd, он будет работать нормально. Если я этого не сделаю, проверка не найдет объявление "Элемент".

11 ответов

Решение

Вам нужно использовать LSResourceResolver для этого, чтобы работать. пожалуйста, посмотрите на пример кода ниже.

метод проверки:

// note that if your XML already declares the XSD to which it has to conform, then there's no need to declare the schemaName here
void validate(String xml, String schemaName) throws Exception {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    DocumentBuilder parser = builderFactory
            .newDocumentBuilder();

    // parse the XML into a document object
    Document document = parser.parse(new StringInputStream(xml));

    SchemaFactory factory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    // associate the schema factory with the resource resolver, which is responsible for resolving the imported XSD's
    factory.setResourceResolver(new ResourceResolver());

            // note that if your XML already declares the XSD to which it has to conform, then there's no need to create a validator from a Schema object
    Source schemaFile = new StreamSource(getClass().getClassLoader()
            .getResourceAsStream(schemaName));
    Schema schema = factory.newSchema(schemaFile);

    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

реализация преобразователя ресурсов:

public class ResourceResolver  implements LSResourceResolver {

public LSInput resolveResource(String type, String namespaceURI,
        String publicId, String systemId, String baseURI) {

     // note: in this sample, the XSD's are expected to be in the root of the classpath
    InputStream resourceAsStream = this.getClass().getClassLoader()
            .getResourceAsStream(systemId);
    return new Input(publicId, systemId, resourceAsStream);
}

 }

Реализация Input, возвращаемая решателем ресурсов:

public class Input implements LSInput {

private String publicId;

private String systemId;

public String getPublicId() {
    return publicId;
}

public void setPublicId(String publicId) {
    this.publicId = publicId;
}

public String getBaseURI() {
    return null;
}

public InputStream getByteStream() {
    return null;
}

public boolean getCertifiedText() {
    return false;
}

public Reader getCharacterStream() {
    return null;
}

public String getEncoding() {
    return null;
}

public String getStringData() {
    synchronized (inputStream) {
        try {
            byte[] input = new byte[inputStream.available()];
            inputStream.read(input);
            String contents = new String(input);
            return contents;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Exception " + e);
            return null;
        }
    }
}

public void setBaseURI(String baseURI) {
}

public void setByteStream(InputStream byteStream) {
}

public void setCertifiedText(boolean certifiedText) {
}

public void setCharacterStream(Reader characterStream) {
}

public void setEncoding(String encoding) {
}

public void setStringData(String stringData) {
}

public String getSystemId() {
    return systemId;
}

public void setSystemId(String systemId) {
    this.systemId = systemId;
}

public BufferedInputStream getInputStream() {
    return inputStream;
}

public void setInputStream(BufferedInputStream inputStream) {
    this.inputStream = inputStream;
}

private BufferedInputStream inputStream;

public Input(String publicId, String sysId, InputStream input) {
    this.publicId = publicId;
    this.systemId = sysId;
    this.inputStream = new BufferedInputStream(input);
}
}

Как указывает пользователь "ulab" в комментарии к другому ответу, решение, описанное в этом ответе (к отдельному вопросу stackru), будет работать для многих. Вот приблизительная схема такого подхода:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL xsdURL = this.getResource("/xsd/my-schema.xsd");
Schema schema = schemaFactory.newSchema(xsdURL);

Ключ к этому подходу - избегать передачи фабрике схемы потока и вместо этого давать ему URL. Таким образом, он получает информацию о местонахождении файла XSD.

Здесь следует иметь в виду, что атрибут "schemaLocation" в элементах include и / или import будет обрабатываться как отношение к пути к classpath файла XSD, URL-адрес которого вы передали валидатору при использовании простых путей к файлам в форма "my-common.xsd" или "common/some-concept.xsd".

Примечания: - В приведенном выше примере я поместил файл схемы в файл jar в папке "xsd". - Начальная косая черта в аргументе "getResource" указывает Java начинаться с корня загрузчика классов, а не с имени пакета объекта "this".

Принятый ответ совершенно нормально, но не работает с Java 8 без некоторых модификаций. Также было бы неплохо иметь возможность указать базовый путь, с которого будут считываться импортированные схемы.

Я использовал в своей Java 8 следующий код, который позволяет указывать путь встроенной схемы, отличный от корневого пути:

import com.sun.org.apache.xerces.internal.dom.DOMInputImpl;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;

import java.io.InputStream;
import java.util.Objects;

public class ResourceResolver implements LSResourceResolver {

    private String basePath;

    public ResourceResolver(String basePath) {
        this.basePath = basePath;
    }

    @Override
    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
        // note: in this sample, the XSD's are expected to be in the root of the classpath
        InputStream resourceAsStream = this.getClass().getClassLoader()
                .getResourceAsStream(buildPath(systemId));
        Objects.requireNonNull(resourceAsStream, String.format("Could not find the specified xsd file: %s", systemId));
        return new DOMInputImpl(publicId, systemId, baseURI, resourceAsStream, "UTF-8");
    }

    private String buildPath(String systemId) {
        return basePath == null ? systemId : String.format("%s/%s", basePath, systemId);
    }
}

Эта реализация также дает пользователю значимое сообщение в случае, если схема не может быть прочитана.

Мне пришлось внести некоторые изменения в этот пост AMegmondoEmber

Мой основной файл схемы содержал некоторые включения из папок одного уровня, а включенные файлы также имели некоторые включения из своих локальных папок. Я также должен был отследить базовый путь ресурса и относительный путь текущего ресурса. Этот код работает, насколько мне известно, но имейте в виду, что он предполагает, что все файлы xsd имеют уникальное имя. Если у вас есть xsd-файлы с одинаковыми именами, но разным содержимым по разным путям, это, вероятно, вызовет проблемы

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;

/**
 * The Class ResourceResolver.
 */
public class ResourceResolver implements LSResourceResolver {

    /** The logger. */
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /** The schema base path. */
    private final String schemaBasePath;

    /** The path map. */
    private Map<String, String> pathMap = new HashMap<String, String>();

    /**
     * Instantiates a new resource resolver.
     *
     * @param schemaBasePath the schema base path
     */
    public ResourceResolver(String schemaBasePath) {
        this.schemaBasePath = schemaBasePath;
        logger.warn("This LSResourceResolver implementation assumes that all XSD files have a unique name. "
                + "If you have some XSD files with same name but different content (at different paths) in your schema structure, "
                + "this resolver will fail to include the other XSD files except the first one found.");
    }

    /* (non-Javadoc)
     * @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public LSInput resolveResource(String type, String namespaceURI,
            String publicId, String systemId, String baseURI) {
        // The base resource that includes this current resource
        String baseResourceName = null;
        String baseResourcePath = null;
        // Extract the current resource name
        String currentResourceName = systemId.substring(systemId
                .lastIndexOf("/") + 1);

        // If this resource hasn't been added yet
        if (!pathMap.containsKey(currentResourceName)) {
            if (baseURI != null) {
                baseResourceName = baseURI
                        .substring(baseURI.lastIndexOf("/") + 1);
            }

            // we dont need "./" since getResourceAsStream cannot understand it
            if (systemId.startsWith("./")) {
                systemId = systemId.substring(2, systemId.length());
            }

            // If the baseResourcePath has already been discovered, get that
            // from pathMap
            if (pathMap.containsKey(baseResourceName)) {
                baseResourcePath = pathMap.get(baseResourceName);
            } else {
                // The baseResourcePath should be the schemaBasePath
                baseResourcePath = schemaBasePath;
            }

            // Read the resource as input stream
            String normalizedPath = getNormalizedPath(baseResourcePath, systemId);
            InputStream resourceAsStream = this.getClass().getClassLoader()
                    .getResourceAsStream(normalizedPath);

            // if the current resource is not in the same path with base
            // resource, add current resource's path to pathMap
            if (systemId.contains("/")) {
                pathMap.put(currentResourceName, normalizedPath.substring(0,normalizedPath.lastIndexOf("/")+1));
            } else {
                // The current resource should be at the same path as the base
                // resource
                pathMap.put(systemId, baseResourcePath);
            }
            Scanner s = new Scanner(resourceAsStream).useDelimiter("\\A");
            String s1 = s.next().replaceAll("\\n", " ") // the parser cannot understand elements broken down multiple lines e.g. (<xs:element \n name="buxing">)
                    .replace("\\t", " ") // these two about whitespaces is only for decoration
                    .replaceAll("\\s+", " ").replaceAll("[^\\x20-\\x7e]", ""); // some files has a special character as a first character indicating utf-8 file
            InputStream is = new ByteArrayInputStream(s1.getBytes());

            return new LSInputImpl(publicId, systemId, is); // same as Input class
        }

        // If this resource has already been added, do not add the same resource again. It throws
        // "org.xml.sax.SAXParseException: sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of ..."
        // return null instead.
        return null;
    }

    /**
     * Gets the normalized path.
     *
     * @param basePath the base path
     * @param relativePath the relative path
     * @return the normalized path
     */
    private String getNormalizedPath(String basePath, String relativePath){
        if(!relativePath.startsWith("../")){
            return basePath + relativePath;
        }
        else{
            while(relativePath.startsWith("../")){
                basePath = basePath.substring(0,basePath.substring(0, basePath.length()-1).lastIndexOf("/")+1);
                relativePath = relativePath.substring(3);
            }
            return basePath+relativePath;
        }
    }
}

Принятый ответ очень многословен, и сначала он строит DOM в памяти, но мне кажется, что включает в себя функцию "из коробки", включая относительные ссылки.

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File("../foo.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new File("./foo.xml")));

Все, что вам нужно, это использоватьStreamSourceс подписью из 2 аргументов, например:

      val schemaResource = Thread.currentThread().contextClassLoader.getResource("path/to/main.xsd") ?: throw RuntimeException("todo")
val schema = schemaFactory.newSchema(StreamSource(schemaResource.openStream(), schemaResource.toExternalForm()))
schema.newValidator().validate(source)

Я и мой коллега боролись с этим несколько часов, и, похоже, ничего не помогало. Мы будем получать такие ошибки, как

      cvc-elt.1: Cannot find the declaration of element 'Login'.

Самым важным в нашем случае было использование URL-адреса в качестве ресурса вместо InputStream при настройке схемы с помощью метода newSchema(...) SchemaFactory . Наши XSD-файлы находятся в пути к классам, поэтому мы используем ClassPathResource для получения XSD-файлов. См. ниже пример кода XSD + XML + Java. XSD были доставлены нам как есть.

Основной XSD (Login.xsd):

      <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ns0="xmnls"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           ns0:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">
   <xs:include schemaLocation="CustomTypes.xsd"/>
   <xs:element name="Login">      
      <xs:complexType>
         <xs:sequence>
            <xs:element name="Username" type="Char20">               
            </xs:element>            
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

XSD ( CustomTypes.xsd ), включенный в основной XSD (Login.xsd):

      <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ns0="xmnls"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           jaxb:extensionBindingPrefixes="xjc"
           jaxb:version="2.0"
           ns0:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">   
   <xs:simpleType name="Char20">      
      <xs:restriction base="xs:string">
         <xs:pattern value=".{1,20}">            
         </xs:pattern>
      </xs:restriction>
   </xs:simpleType>   
</xs:schema>

XML для проверки:

      <?xml version="1.0" encoding="UTF-8"?>
<Login>
 <Username>This username exceeds 20 character</Username>
</Login>

Java-код для проверки XML-строки на соответствие XSD:

      class XsdXmlJaxbValidationExample {
     validateXml() {

          String loginXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
              + "<Login>"
              + "<Username>This username exceeds 20 character</Username>"
              + "</Login>";
 
          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
          URL mainXsd = new ClassPathResource("Login.xsd").getURL();
          Schema mainSchema = schemaFactory.newSchema(mainXsd);
          Validator validator = mainSchema.newValidator();
          validator.validate(new StreamSource(new StringReader(loginXml)));

     }
 }

Этот поток был очень полезен для разбора сложных XML-схем в нескольких файлах.

Я также должен был добавить:

              SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        factory.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);

для обработки нескольких файлов с одним и тем же целевым пространством имен.

Для нас resolResource выглядел следующим образом. После некоторого прологического исключения и странного типа элемента "xs:schema" должны следовать либо спецификации атрибутов, ">" или "/>". Тип элемента "xs:element" должен сопровождаться спецификациями атрибутов, ">" или "/>". (из-за разбивки нескольких строк)

История пути была необходима из-за структуры включений

main.xsd (this has include "includes/subPart.xsd")
/includes/subPart.xsd (this has include "./subSubPart.xsd")
/includes/subSubPart.xsd

Итак, код выглядит так:

String pathHistory = "";

@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
    systemId = systemId.replace("./", "");// we dont need this since getResourceAsStream cannot understand it
    InputStream resourceAsStream = Message.class.getClassLoader().getResourceAsStream(systemId);
    if (resourceAsStream == null) {
        resourceAsStream = Message.class.getClassLoader().getResourceAsStream(pathHistory + systemId);
    } else {
        pathHistory = getNormalizedPath(systemId);
    }
    Scanner s = new Scanner(resourceAsStream).useDelimiter("\\A");
    String s1 = s.next()
            .replaceAll("\\n"," ") //the parser cannot understand elements broken down multiple lines e.g. (<xs:element \n name="buxing">) 
            .replace("\\t", " ") //these two about whitespaces is only for decoration
            .replaceAll("\\s+", " ") 
            .replaceAll("[^\\x20-\\x7e]", ""); //some files has a special character as a first character indicating utf-8 file
    InputStream is = new ByteArrayInputStream(s1.getBytes());

    return new LSInputImpl(publicId, systemId, is);
}

private String getNormalizedPath(String baseURI) {
    return baseURI.substring(0, baseURI.lastIndexOf(System.getProperty("file.separator"))+ 1) ;
}

Если вы не найдете элемент в xml, вы получите исключение xml:lang. Элементы чувствительны к регистру

SchemaFactory schemaFactory = SchemaFactory
                                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(getClass().getClassLoader()
                                .getResourceAsStream("cars-fleet.xsd"));
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
StreamSource source = new StreamSource(xml);
validator.validate(source);
Другие вопросы по тегам