我正在尝试使用 XmlSchema.Read 加载 xsd 文件。它运行没有错误,我可以看到它从文件中读取命名空间,但我无法从 xsd 文件中读取任何其他数据。
这是我的代码:
public XmlSchema GetXSDFileAsXMLSchema()
{
FileStream fs = new FileStream(path, FileMode.Open);
XmlSchema schema = XmlSchema.Read(fs, new ValidationEventHandler(ValidationCallBack));
return schema;
}
private void ValidationCallBack(object sender, ValidationEventArgs args)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
有人知道为什么元素数是0吗?
我已经给出了元素或属性的本地名称以及文档的架构。确定元素或属性的基本数据类型的最简单方法是什么。我所说的基本数据类型是指 xs:string、xs:date 等(xml 模式的内置数据类型。)
我面临的问题之一是元素类型很少成为基本的内置类型之一。99% 的时间它是一个复杂类型,50% 的时间引用另一个复杂类型,另一个复杂类型引用另一个复杂类型等等。
此模式的一个简单示例:我想找到 Employee/Person/Name/LastName 的基本类型(确定 LastName 是 xs:normalizedString)。在架构 Employee 中定义为 xs:element 和 type="bns:EmployeeType"
EmplyeeType 定义了一个 Person 元素,但它是“PersonType”类型,然后 Name in person 是 NameType,这是一个复杂类型,它扩展了 GeneralName 类型,即 BasicNameType 类型,该类型最终定义了 LastName 类型,其类型为“LastNameType”,然后在。还有定义等。
我目前正在使用 linq-to-xml 编写一个解析器来解决这个问题,但这并不容易或漂亮。我已经搜索了其他解决方案,但没有找到任何解决方案,但我完全承认我对 XML/模式/XPath 的无知。
有没有一种简单的方法来获取元素的基本类型?
给定一个xsd:dateTime 格式的字符串,我想创建一个 python datetime 对象。我特别需要能够解析例如像这样的字符串 '2012-09-23T09:55:00',而且所有其他定义的示例都应该正确解析,并且还使用时区。
XML文件的结构大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="url1" xsi:schemaLocation="url2 url3">
<b>
<c></c>
<c></c>
<c></c>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
我的目标是选择所有“c”元素,但以下 xpath 表达式不起作用:“//a/b/c”。
IE:
XmlDocument doc= new XmlDocument();
doc.Load(filepath);
XmlNodeList l = doc.SelectNodes("//a/b/c"); // 0 nodes
Run Code Online (Sandbox Code Playgroud)
我测试过的唯一有效的 xpath 表达式是 /*(1 个节点)和 //*(所有节点)。
这个问题是否与 XML 命名空间有关?如果是这样,设置 XMLDocument 对象的正确方法是什么?
XmlDocument doc= new XmlDocument();
doc.Load(filepath);
XmlNamespaceManager m = new XmlNamespaceManager(doc.NameTable);
m.AddNamespace(/* what goes here? */);
XmlNodeList l = doc.SelectNodes("//a/b/c", m);
Run Code Online (Sandbox Code Playgroud) 我想根据此模式(即在 zip 中)验证 XML 文件;它导入另外两个 XSD 文件。
<import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation="xmldsig-core-schema.xsd"/>
<import namespace="http://www.w3.org/2001/04/xmlenc#"
schemaLocation="xenc-schema.xsd"/>
Run Code Online (Sandbox Code Playgroud)
这两个文件也可以在这里找到:
在验证时,我收到此错误:
Src-resolve: Cannot Resolve The Name 'xenc:EncryptedData' To A(n) 'element Declaration' Component.
Run Code Online (Sandbox Code Playgroud)
我的验证/解组代码如下所示(使用 moxy 作为 JAXB 提供程序):
jaxbContext = JAXBContext.newInstance(type.getRequestType().getPackage().getName());
Unmarshaller um = jaxbContext.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(this.getClass().getResourceAsStream("/xsd/" + type.getXsdName())));
um.setSchema(schema);
root = um.unmarshal(new StreamSource(new ByteArrayInputStream(xmlData)), type.getRequestType());
Run Code Online (Sandbox Code Playgroud)
在您询问类型有什么作用之前:我编写了可以从http://www.forum-datenaustausch.ch/导入所有类型发票的代码。但是 4.3 及更高版本使用了两个额外的架构文件。如何验证 XML 文件?
我正在寻找一种不使用正则表达式来提取模式的本地路径的最佳方法。
样本:
<?xml version="1.0"?>
<ord:order xmlns:ord="http://example.org/ord"
xmlns:prod="http://example.org/prod"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/prod chapter05prod.xsd
http://example.org/ord chapter05ord.xsd">
<items>
<prod:product>
<number xsi:type="xs:short">557</number>
<name>Short-Sleeved Linen Blouse</name>
<size xsi:nil="true"/>
</prod:product>
</items>
Run Code Online (Sandbox Code Playgroud)
或者
xsi:schemaLocation="http://example.org/prod \\RandomFolder\New\chapter05prod.xsd">
Run Code Online (Sandbox Code Playgroud)
或者
xsi:schemaLocation="chapter05prod.xsd">
Run Code Online (Sandbox Code Playgroud)
我想获取 *.xsd 文件的本地路径。有没有办法使用 xml 解析器或 xmlResolver 或其他不使用正则表达式的方式来做到这一点?
编辑:我正在寻找一种最通用的方法来获取外部 .xsd 路径引用的路径。
另一个例子:
xsi:noNamespaceSchemaLocation="file://C://Documents and Settings//All Users//Application Data//My Application//MyData.xsd"
Run Code Online (Sandbox Code Playgroud) 执行以下脚本(这是实际脚本的一部分)时,我在 powershell 中收到以下错误消息:
The term 'xsd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:10 char:3
$xsds = ls *.xsd | %{ $_.Name }
if ($xsds.Count -eq 0) { exit }
# Add '.\' to last file name, see http://stackoverflow.com/questions/906093/xsd-exe-output-filename
$last = $xsds | select -last 1
$last = '.\' + …Run Code Online (Sandbox Code Playgroud) 我指的是下面的代码,用于从 XSD 进行 XML 验证,从功能的角度来看,它似乎工作正常。
var schemas = new XmlSchemaSet();
schemas.Add("http://microsoft.com/HealthCare/HL7/2X", xsdFilePath);
Boolean result = true;
xdocXml.Validate(schemas, (sender, e) =>
{
result = false;
});
Run Code Online (Sandbox Code Playgroud)
现在,如您所见,我必须明确指定架构名称,即使此信息本身包含在 XSD 中。有没有办法从 XSD 中提取该信息,这样我就不必特别指定?
我想在我的XML模式定义(XSD)中定义一个类型,它以点十进制表示法表示IPv4地址,因此在我的XML中:
<Example>
<Address>192.168.0.1</Address>
</Example>
Run Code Online (Sandbox Code Playgroud)
将被验证为正确和不正确的值,例如:
<Example>
<Address>192.268.0.1</Address>
</Example>
Run Code Online (Sandbox Code Playgroud)
被拒绝为无效.
我正在尝试针对XML文件验证JAXB模式,并且不断收到上述错误。您能告诉我<{} Quote>是什么意思吗?错误是从哪里来的?报价是从XSD文件创建的。是否因为未定义“引号”元素?这是我的代码。
package khoa;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import khoa.Quote;
import khoa.Quotes;
public class EmployeeUnmarshaller {
public static void main(String[] args) throws JAXBException, SAXException {
new EmployeeUnmarshaller().runEmployeeUnmarshaller();
}
private void runEmployeeUnmarshaller() throws JAXBException, SAXException {
JAXBContext context = JAXBContext.newInstance(Quote.class);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("quotes.xsd"));
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new EmployeeValidationEventHandler());
System.out.println(unmarshaller.unmarshal(new File("quotes.xml")));
}
}
class EmployeeValidationEventHandler implements …Run Code Online (Sandbox Code Playgroud) xsd ×10
xml ×6
c# ×4
.net ×1
cmdlet ×1
datetime ×1
ip-address ×1
java ×1
jaxb ×1
linq-to-xml ×1
moxy ×1
powershell ×1
python ×1
schema ×1
timezone ×1
vb.net ×1
xml-parsing ×1
xpath ×1