5

我在 XSD 中为我的应用程序使用 gml (3.1.1) XSD。我想下载 3.1.1 版中的所有 gml XSD,例如 zip 文件。换句话说:base xsd 在这里,我想下载这个 XSD,所有导入都在 zip 文件或类似 zip 文件的文件中。有没有支持它的应用程序?我找到了这个下载器,但它对我不起作用(我认为这个应用程序不支持在 gml.xsd 3.1.1 中出现的导入中的相对路径)。有任何想法吗?

4

6 回答 6

5

QTAssistant的 XSR(我与它相关联)有一个易于使用的功能,它允许将 XSD 内容自动导入和重构为来自各种来源的本地文件。在此过程中,它将更新架构位置引用等。

我对完成此类任务所涉及的步骤进行了简单的屏幕截图,以展示其可用性。

于 2012-11-21T02:39:41.477 回答
4

基于mschwehl的解决方案,我做了一个改进的类来实现fetch。它很适合这个问题。见https://github.com/mfalaize/schema-fetcher

于 2016-10-08T10:37:27.153 回答
3

您可以使用 SOAP UI 来实现这一点。

跟着这些步骤 :

  1. 使用 WSDL 创建项目。
  2. 选择您的界面并在界面查看器中打开。
  3. 导航到“WSDL 内容”选项卡。
  4. 使用“WSDL 内容”选项卡下的最后一个图标:“将整个 WSDL 和包含/导入的文件导出到本地目录”。
  5. 选择要将 XSD 导出到的文件夹。

注意:SOAPUI 将删除所有相对路径并将所有 XSD 保存到同一文件夹中。

于 2015-05-21T07:08:31.517 回答
2

我编写了一个简单的 java-main 来完成这项工作并更改为相对 url

package dl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class SchemaPersister {

    private static final String EXPORT_FILESYSTEM_ROOT = "C:/export/xsd";


    // some caching of the http-responses
    private static Map<String,String> _httpContentCache = new HashMap<String,String>();


    public static void main(String[] args) {
        try {
            new SchemaPersister().doIt();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private void doIt() throws Exception {


//      // if you need an inouse-Proxy
//      final String authUser = "xxxxx";
//      final String authPassword = "xxxx"
//
//      System.setProperty("http.proxyHost", "xxxxx");
//      System.setProperty("http.proxyPort", "xxxx");
//      System.setProperty("http.proxyUser", authUser);
//      System.setProperty("http.proxyPassword", authPassword);
//
//      Authenticator.setDefault(
//        new Authenticator() {
//          public PasswordAuthentication getPasswordAuthentication() {
//            return new PasswordAuthentication(authUser, authPassword.toCharArray());
//          }
//        }
//      );
//      

        Set <SchemaElement> allElements = new HashSet<SchemaElement>() ;

//      URL url = new URL("file:/C:/xauslaender-nachrichten-administration.xsd");
        URL url = new URL("http://www.osci.de/xauslaender141/xauslaender-nachrichten-bamf-abh.xsd");


        allElements.add ( new  SchemaElement(url));


        for (SchemaElement e: allElements) {

            System.out.println("processing " + e);
            e.doAll();
        }


        System.out.println("done!");

    }


    class SchemaElement {

        private URL    _url;
        private String _content;

        public List <SchemaElement> _imports ;
        public List <SchemaElement> _includes ;

        public SchemaElement(URL url) {
            this._url = url;
        }



        public void checkIncludesAndImportsRecursive() throws Exception {

            InputStream in = new ByteArrayInputStream(downloadContent() .getBytes("UTF-8"));

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document   doc = builder.parse(in);
            List<Node> includeNodeList = null;
            List<Node> importNodeList  = null;


            includeNodeList = getXpathAttribute(doc,"/*[local-name()='schema']/*[local-name()='include']");
            _includes = new ArrayList <SchemaElement> ();

            for ( Node element: includeNodeList) {

                Node sl = element.getAttributes().getNamedItem("schemaLocation");
                if (sl == null) {
                    System.out.println(_url + " defines one import but no schemaLocation");
                    continue;
                }

                String asStringAttribute = sl.getNodeValue();

                URL url = buildUrl(asStringAttribute,_url);

                SchemaElement tmp = new SchemaElement(url);
                tmp.setSchemaLocation(asStringAttribute);

                tmp.checkIncludesAndImportsRecursive();
                _includes.add(tmp);



            }

            importNodeList =  getXpathAttribute(doc,"/*[local-name()='schema']/*[local-name()='import']");
            _imports = new ArrayList <SchemaElement> ();

            for ( Node element: importNodeList) {

                Node sl = element.getAttributes().getNamedItem("schemaLocation");
                if (sl == null) {
                    System.out.println(_url + " defines one import but no schemaLocation");
                    continue;
                }

                String asStringAttribute = sl.getNodeValue();
                URL url = buildUrl(asStringAttribute,_url);

                SchemaElement tmp = new SchemaElement(url);
                tmp.setSchemaLocation(asStringAttribute);

                tmp.checkIncludesAndImportsRecursive();

                _imports.add(tmp);
            }

            in.close();


        }   


        private String schemaLocation;

        private void setSchemaLocation(String schemaLocation) {
            this.schemaLocation = schemaLocation;

        }

        // http://stackoverflow.com/questions/10159186/how-to-get-parent-url-in-java
        private URL buildUrl(String asStringAttribute, URL parent) throws Exception {

            if (asStringAttribute.startsWith("http")) {
                return new URL(asStringAttribute);
            }

            if (asStringAttribute.startsWith("file")) {
                return new URL(asStringAttribute);
            }

            // relative URL
            URI parentUri = parent.toURI().getPath().endsWith("/") ? parent.toURI().resolve("..") : parent.toURI().resolve(".");
            return new URL(parentUri.toURL().toString() + asStringAttribute );

        }




        public void doAll() throws Exception {


            System.out.println("READ ELEMENTS");
            checkIncludesAndImportsRecursive();

            System.out.println("PRINTING DEPENDENCYS");
            printRecursive(0);

            System.out.println("GENERATE OUTPUT");

            patchAndPersistRecursive(0);

        }




        public void patchAndPersistRecursive(int level) throws Exception {


            File f = new File(EXPORT_FILESYSTEM_ROOT + File.separator + this.getXDSName()  );

            System.out.println("FILENAME: " + f.getAbsolutePath());


            if (_imports.size() > 0) {

                for (int i = 0; i < level; i++) {
                    System.out.print("   ");
                }

                System.out.println("IMPORTS");
                for (SchemaElement kid : _imports) {
                    kid.patchAndPersistRecursive(level+1);
                }

            }

            if (_includes.size() > 0) {

                for (int i = 0; i < level; i++) {
                    System.out.print("   ");
                }

                System.out.println("INCLUDES");
                for (SchemaElement kid : _includes) {
                    kid.patchAndPersistRecursive(level+1);
                }

            }




            String contentTemp = downloadContent();

            for (SchemaElement i : _imports ) {

                if (i.isHTTP()) {
                    contentTemp = contentTemp.replace(
                            "<xs:import schemaLocation=\"" + i.getSchemaLocation() ,        
                            "<xs:import schemaLocation=\"" + i.getXDSName() );
                }

            }


            for (SchemaElement i : _includes ) {

                if (i.isHTTP()) {
                    contentTemp = contentTemp.replace(
                            "<xs:include schemaLocation=\"" + i.getSchemaLocation(),        
                            "<xs:include schemaLocation=\"" + i.getXDSName() );
                }

            }


            FileOutputStream fos = new FileOutputStream(f);     
            fos.write(contentTemp.getBytes("UTF-8"));
            fos.close();

            System.out.println("File written: " + f.getAbsolutePath() );




        }



        public void printRecursive(int level) {

            for (int i = 0; i < level; i++) {
                System.out.print("   ");
            }

            System.out.println(_url.toString());

            if (this._imports.size() > 0) {

                for (int i = 0; i < level; i++) {
                    System.out.print("   ");
                }

                System.out.println("IMPORTS");
                for (SchemaElement kid : this._imports) {
                    kid.printRecursive(level+1);
                }

            }

            if (this._includes.size() > 0) {

                for (int i = 0; i < level; i++) {
                    System.out.print("   ");
                }

                System.out.println("INCLUDES");
                for (SchemaElement kid : this._includes) {
                    kid.printRecursive(level+1);
                }

            }
        }



        String getSchemaLocation() {
            return schemaLocation;
        }



        /**
         * removes html:// and replaces / with _
         * @return
         */

        private String getXDSName() {


            String tmp = schemaLocation;

            // Root on local File-System -- just grap the last part of it
            if (tmp == null) {
                tmp = _url.toString().replaceFirst(".*/([^/?]+).*", "$1");
            }


            if ( isHTTP() ) {

                tmp = tmp.replace("http://", "");
                tmp = tmp.replace("/", "_");

            } else {

                tmp = tmp.replace("/", "_");
                tmp = tmp.replace("\\", "_");

            }

            return tmp;

        }



        private boolean isHTTP() {
            return _url.getProtocol().startsWith("http");
        }




        private String downloadContent() throws Exception {


            if (_content == null) {

                System.out.println("reading content from " + _url.toString());

                if (_httpContentCache.containsKey(_url.toString())) {
                    this._content = _httpContentCache.get(_url.toString());
                    System.out.println("Cache hit! " + _url.toString());
                } else {

                    System.out.println("Download " + _url.toString());
                    Scanner scan =  new Scanner(_url.openStream(), "UTF-8");

                    if (isHTTP()) {
                        this._content = scan.useDelimiter("\\A").next();    
                    } else {
                        this._content = scan.useDelimiter("\\Z").next();
                    }

                    scan.close();

                    if (this._content != null) {
                        _httpContentCache.put(_url.toString(), this._content);
                    }

                }


            }

            if (_content == null) {
                throw new NullPointerException("Content of " + _url.toString() + "is null ");
            }

            return _content;

        }






        private List<Node> getXpathAttribute(Document doc, String path) throws Exception {

            List <Node> returnList = new ArrayList <Node> ();

            XPathFactory xPathfactory = XPathFactory.newInstance();

            XPath xpath = xPathfactory.newXPath();

            {
                XPathExpression expr = xpath.compile(path);

                NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET );

                for (int i = 0 ; i < nodeList.getLength(); i++) {

                    Node n = nodeList.item(i);

                    returnList.add(n);

                }
            }

            return returnList;

        }


        @Override
        public String toString() {

            if (_url != null) {
                return _url.toString();
            }

            return super.toString();

        }

    }


}
于 2014-10-14T08:12:15.317 回答
1

import我创建了一个 python 工具来递归下载标签中具有相对路径的 XSD (例如<import schemaLocation="../../../../abc:)

https://github.com/nate/xsd_download

下载架构后,您可以使用它xmllint来验证 XML 文档

于 2019-10-18T20:48:52.077 回答
0

我正在使用 xmlbeans 项目中的 org.apache.xmlbeans.impl.tool.SchemaResourceManager。这个类快速且易于使用。

例如:

    SchemaResourceManager manager = new SchemaResourceManager(new File(dir));
    manager.process(schemaUris, emptyArray(), false, true, true);
    manager.writeCache();

此类有一个 main 方法,用于记录可用的不同选项。

于 2019-08-28T13:06:36.550 回答