- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 Spring 项目,分为三个模块,例如:
就像您在 module2 中看到的那样,我创建了 XSD 文件来验证所有三个模块中的 XML 文件。为了使用 module2 中的 XSD 验证 XML 文件,我只需添加以下行:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Modules.xsd">
但是我不知道如何使用 XSD 验证 Module1Test.xml
和 Module3Test.xml
文件。我无法在 XML 文件中使用 XSD 的绝对路径,例如:
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:\\project\\module2\\src\\main\\java\\com\\test\\Modules.xsd">
我想使用相对路径,例如:
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project\\module2\\src\\main\\java\\com\\test\\Modules.xsd">
或者以其他更好的方式,只是不使用绝对路径。
你知道这是否可能吗?怎么解决呢?
最佳答案
我认为“这确实取决于”,取决于您的编码方式和引用方式,因此我会重点介绍这两种方法:
您可以将 XSD 作为输入流直接传递到程序,这样即使 XML 文档中的元素没有命名空间,也无需依赖“noNamespaceSchemaLocation”属性。
下面是一个示例程序:
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class XmlSchemaValidationHelper {
public static void main(String[] argv) {
XmlSchemaValidationHelper schemaValidationHelper = new XmlSchemaValidationHelper();
schemaValidationHelper.validateAgainstSchema(new File(argv[0]), new File(argv[1]));
}
public void validateAgainstSchema(File xmlFile, File xsdFile) {
try {
System.out.println("### Starting...");
// parse an XML document into a DOM tree
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
Document document = parser.parse(xmlFile);
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(xsdFile);
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an
// instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
System.out.println("### Finished...");
} catch (FileNotFoundException ex) {
throw new OpenClinicaSystemException("File was not found", ex.getCause());
} catch (IOException ioe) {
throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
} catch (SAXParseException spe) {
spe.printStackTrace();
throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
} catch (SAXException e) {
throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
} catch (ParserConfigurationException pce) {
throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
}
}
public class OpenClinicaSystemException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
private String errorCode;
private Object[] errorParams;
public OpenClinicaSystemException(String code, String message) {
this(message);
this.errorCode = code;
}
public OpenClinicaSystemException(String code, String message, Throwable cause) {
this(message, cause);
this.errorCode = code;
}
public OpenClinicaSystemException(String message, Throwable cause) {
super(message, cause);
}
public OpenClinicaSystemException(Throwable cause) {
super(cause);
}
public OpenClinicaSystemException(String message) {
super(message);
this.errorCode = message;
}
public OpenClinicaSystemException(String code, Object[] errorParams) {
this.errorCode = code;
this.errorParams = errorParams;
}
public OpenClinicaSystemException(String code, Object[] errorParams, String message) {
this(message);
this.errorCode = code;
this.errorParams = errorParams;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getErrorParams() {
return errorParams;
}
public void setErrorParams(Object[] errorParams) {
this.errorParams = errorParams;
}
}
}
像这样运行:E:\xmlValidator>java XmlSchemaValidationHelper po.xml test/po.xsd
并且您不需要依赖“noNamespaceSchemaLocation”属性,因为您直接传递 XSD作为 validator/解析器的输入流。
这是根据 XSD 验证 XML 的快速但肮脏的方法。
您只需要正确转义 XSD 路径,因此请使用以下任一选项:
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C://project//module2//src//main//java//com//test//Modules.xsd">
或者
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C://project//module2//src//main//java//com//test//Modules.xsd">
阅读MSDN official article差不多。
<小时/>我建议您应该使用 XML 命名空间和 targetNamespace 来正确地将 XML 实例与 XML 架构相关联。考虑下面的示例 XSD 和 XML。
对于任何不理解 XML 命名空间和 targetNamespace 概念的人,我希望以下要点会有所帮助:
targetNamespace="http://www.books.org"
,它只不过是为实际 XML 定义命名空间实例。您需要在 XML 实例中使用相同的命名空间,即 http://www.books.org
。xmlns="http://www.books.org"
xsi:schemaLocation="http://www.books.组织”
示例 XML:
<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper & Row</Publisher>
</Book>
</BookStore>
示例 XSD:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:simpleType name="ISBN-type">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:gYear"/>
<xsd:element name="ISBN" type="ISBN-type"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
关于java - XML - XSD 的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41433044/
我知道我以前在某个地方看到过这个,但我再也找不到它了。我需要一个从另一个 xsd 中调用 xsd 文件的示例。这在生成大量 xml 文件的情况下非常有用,但在这些 xml 文件之间存在大量公共(pub
有人知道供应商中立的 XSD 来描述关系数据库模式吗?我们的系统需要获取有关数据库结构的信息: 表 列和类型 主键和外键约束 索引 等 以独立于供应商的方式并将其存储在 XML 文件中以供以后处理。
我在 XSD 中使用 gml (3.1.1) XSD 作为我的应用程序。我想下载版本 3.1.1 中的所有 gml XSD,例如 zip 文件。换句话说:基本 xsd 是 here我想用 zip 文件
我想要一个 XSD 来验证包含具有许多别名但每个别名具有不同值的文件元素的 XML。 这是我的 XML: Document1
我看到一个 xml 架构 ( EPP ) 将 xsd:choice 与一个元素一起使用,即使我们可以使用 xsd:enumeration 代替:
我目前工作的公司将架构或契约(Contract)版本编入根节点。例如, ... 我正在寻找人们对这种设计方法的意见,因为我不相信它是合理的。例如,它要求所有使用此模式作为消息传递契约的服务都能够发
我在处理 Web 服务响应时遇到了 Apache CXF 解析错误。归结为一个空元素被返回: 元素定义如下: 现在我在 CXF 邮件列表上看到了 empty value is not allowe
XSD 可以为比较两个元素添加约束吗? 假设我在 DataRangeType 下有 Begin End 我想添加一个约束说 Begin 非常
我想声明一个要包含在复杂类型声明中的元素,并且该元素有一个强制属性:“option=MyOption”,但是“选项”属性的值可以是任何值,具体取决于上下文。 也就是说:在使用包含该元素的复杂类型的任何
我需要能够将简单元素类型设置为整数,但也可以将其设置为空。如果此示例为空且空白字段不是整数,则此示例将发送错误。我该如何解决? 最佳答案 您要做的是在同一元素上分配限制,并对其进行合并,例如以下示例
对于这个 xml: 我有这个模式,它似乎可以根据 w3 schema validation service 进行验证,并且该架构可以很好地验证上述 XML。遗憾的是,xsd.exe
我有两个 XSD 文件(源文件和目标文件)...我应该在什么基础上映射这两个文件以获得 XSLT?我知道 MapForce 如何帮助映射,但我使用示例项目..现在我想知道我应该在什么基础上映射我的客户
我有一个 .cs 文件,其中包含 XTypedElement 和 IXMetaData 的子类。微软有一个 tool that generates XSD files automatically来自托
这个问题在这里已经有了答案: XML Schema to validate XML Schemas? (3 个答案) 关闭 9 年前。 是否存在可验证其他 XML 架构的 Xml 架构? 我想做的是
假设我正在处理一个 xsd:simpleType,它是一个字符串,需要具有特定的字符集和特定的最大长度,类似于以下代码: 所以我的 xsd 类型将是一个只
JAXB 同时映射 xsd:base64Binary和 xsd:hexBinary类型为 byte[] . 鉴于我有一个架构/DOM 元素代表这些类型中的每一个,例如: ABCD对于 xsd:hexB
我非常确定我在这里遗漏了一些简单的东西。 我正在使用 netbeans 在两个单独的项目中创建一个 web jax-ws web 服务和一个客户端,并且我有一些自定义绑定(bind)已使用 net b
将字节数组表示为 XSD 架构的最佳方式是什么?我有一个字节输入,我需要解析它并将其提供给 JAXB 从 XSD 模式生成的 Java 对象以供将来验证。我输入中的每条信息都由偏移量和长度定义。我想将
我的架构的这一部分给我带来了麻烦:
我需要定义元素“MyData”的名为“DataValue”的属性。但要求是“DataValue”的类型可以动态更改,即数据值在一个实例中可以是字符串,而在其他实例中它可以是 int 或 bool。它可
我是一名优秀的程序员,十分优秀!