解析架构并在同一架构中导入

发布于 2024-10-15 11:54:43 字数 1108 浏览 3 评论 0原文

只是想知道是否有任何方法可以解析 xsd 文件和同时导入到原始 xsd 中的 xsd,以便我可以直接访问导入的 xsd 中的元素。有没有任何框架可以实现这一点?

只是我的意思的一个例子

来自我想要解析的 XSD:


来自解析文件中导入的 XSD














所以,我想要的是当我通过某种内联或其他方式解析原始 xsd 时访问导入的 xsd 中的元素:- )

这在某种程度上可能吗?

Just wondering if there are any ways to parse a xsd file AND an xsd that's imported in the original xsd at the same time, so I can get direct access to the elements in the imported xsd. Are there any frameworks that makes this possible?

Just an example of what I mean

From the XSD that I want to parse:

<xsd:import namespace="..." schemaLocation="anotherFile.xsd">

<xsd:element ref="anElement" />

From the XSD that's imported in the parsed file

<xsd:element name="anElement">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="THIS" />
<xsd:enumeration value="IS" />
<xsd:enumeration value="THE" />
<xsd:enumeration value="ELEMENTS" />
<xsd:enumeration value="I" />
<xsd:enumeration value="WANT" />
<xsd:enumeration value=":-)" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

So, What I want is to get access to the elements in the imported xsd when I'm parsing the original xsd by some kind of inlining or something :-)

Is this possible in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

做个少女永远怀春 2024-10-22 11:54:43

是的,您只需要实现一个 LSResourceResolver 类,该类将能够从您指定的模式位置读取:

 /**
 * This function validates a DomResult. T
 *
 * @param domResult
 * @param schemaFile path to the schmea file to validate against.
 * @throws org.xml.sax.SAXException
 * @throws java.io.IOException
 *
 */
protected void validateDomResult(DOMResult domResult, String schemaFile) throws SAXException, IOException, Exception {

    Schema schema = createSchema(schemaFile);
    javax.xml.validation.Validator validator = schema.newValidator();
    ErrorHandler mySchemaErrorHandler = new LoggingErrorHandler();
    validator.setErrorHandler(mySchemaErrorHandler);
    DOMSource domSource = new DOMSource(domResult.getNode());
    validator.validate(domSource);
    if (((LoggingErrorHandler) mySchemaErrorHandler).isError()) {
        throw new Exception("Validation Error");
    }
}

/**
 *
 * @param baseSchemaFilePath
 * @return
 * @throws java.lang.Exception
 *
 */
protected Schema createSchema(String baseSchemaFilePath) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    LSResourceResolver resourceResolver = (LSResourceResolver) new LocalSchemaLSResourceResolver();
    factory.setResourceResolver(resourceResolver);
    Schema schema = factory.newSchema(new File(baseSchemaFilePath));

    return schema;
}

这是一个简单的 LSResourceResolver 实现,它在类路径上的 xsd 目录中查找模式:

public class LocalSchemaLSResourceResolver implements LSResourceResolver{

    protected final Log logger = LogFactory.getLog(getClass());

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

        LSInput input = new DOMInputImpl();
        try {
            FileInputStream fis = new FileInputStream(new File("classpath:xsd/" + systemId));

            input.setByteStream(fis);
            return input;
        } catch (FileNotFoundException ex) {
            logger.error("File Not found", ex);
            return null;
        }

    }
}

Yes, you just need to implement a LSResourceResolver class that will able to read from the schema locations you specify:

 /**
 * This function validates a DomResult. T
 *
 * @param domResult
 * @param schemaFile path to the schmea file to validate against.
 * @throws org.xml.sax.SAXException
 * @throws java.io.IOException
 *
 */
protected void validateDomResult(DOMResult domResult, String schemaFile) throws SAXException, IOException, Exception {

    Schema schema = createSchema(schemaFile);
    javax.xml.validation.Validator validator = schema.newValidator();
    ErrorHandler mySchemaErrorHandler = new LoggingErrorHandler();
    validator.setErrorHandler(mySchemaErrorHandler);
    DOMSource domSource = new DOMSource(domResult.getNode());
    validator.validate(domSource);
    if (((LoggingErrorHandler) mySchemaErrorHandler).isError()) {
        throw new Exception("Validation Error");
    }
}

/**
 *
 * @param baseSchemaFilePath
 * @return
 * @throws java.lang.Exception
 *
 */
protected Schema createSchema(String baseSchemaFilePath) throws Exception {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    LSResourceResolver resourceResolver = (LSResourceResolver) new LocalSchemaLSResourceResolver();
    factory.setResourceResolver(resourceResolver);
    Schema schema = factory.newSchema(new File(baseSchemaFilePath));

    return schema;
}

Here is a simple LSResourceResolver implementation that looks for schemas in an xsd directory on the class path:

public class LocalSchemaLSResourceResolver implements LSResourceResolver{

    protected final Log logger = LogFactory.getLog(getClass());

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

        LSInput input = new DOMInputImpl();
        try {
            FileInputStream fis = new FileInputStream(new File("classpath:xsd/" + systemId));

            input.setByteStream(fis);
            return input;
        } catch (FileNotFoundException ex) {
            logger.error("File Not found", ex);
            return null;
        }

    }
}
紫南 2024-10-22 11:54:43

看起来您需要 JAXB 才能将模式(包括导入的模式)放入 Java 类中。

It look like you need JAXB to get your schema (including imported schema) into Java classes.

对岸观火 2024-10-22 11:54:43

当 XSD 文件位于本地 /src/main/resources 目录或打包在 jar 中时,这对我有用:

final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
final URL url = this.getClass().getClassLoader().getResource("META-INF/parent.xsd");
final Schema schema = sf.newSchema(url);

parent.xsd 包含“kid.xsd”——两个 XSD 文件都在同一目录下

This works for me when the XSD files are either on local /src/main/resources directory or packed in a jar:

final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
final URL url = this.getClass().getClassLoader().getResource("META-INF/parent.xsd");
final Schema schema = sf.newSchema(url);

parent.xsd has included "kid.xsd" -- both XSD files are under the same directory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文