pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

要針對多個(gè) xsd 模式驗(yàn)證 XML

XML to be validated against multiple xsd schemas(要針對多個(gè) xsd 模式驗(yàn)證 XML)
本文介紹了要針對多個(gè) xsd 模式驗(yàn)證 XML的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在編寫 xsd 和要驗(yàn)證的代碼,所以我在這里可以很好地控制.

I'm writing the xsd and the code to validate, so I have great control here.

我想要一個(gè)上傳工具,可以根據(jù) xml 文件向我的應(yīng)用程序添加內(nèi)容.xml 文件的一部分應(yīng)根據(jù)另一部分中的值之一針對不同的模式進(jìn)行驗(yàn)證.下面舉個(gè)例子來說明:

I would like to have an upload facility that adds stuff to my application based on an xml file. One part of the xml file should be validated against different schemas based on one of the values in the other part of it. Here's an example to illustrate:

<foo>
  <name>Harold</name>
  <bar>Alpha</bar>
  <baz>Mercury</baz>
  <!-- ... more general info that applies to all foos ... -->

  <bar-config>
    <!-- the content here is specific to the bar named "Alpha" -->
  </bar-config>
  <baz-config>
    <!-- the content here is specific to the baz named "Mercury" -->
  </baz>
</foo>

在這種情況下,<bar> 的內(nèi)容有一些受控詞匯,我可以很好地處理這部分.然后,根據(jù) bar 值,應(yīng)使用適當(dāng)?shù)?xml 模式來驗(yàn)證 bar-config 的內(nèi)容.對于 baz 和 baz-config 也是如此.

In this case, there is some controlled vocabulary for the content of <bar>, and I can handle that part just fine. Then, based on the bar value, the appropriate xml schema should be used to validate the content of bar-config. Similarly for baz and baz-config.

進(jìn)行解析/驗(yàn)證的代碼是用 Java 編寫的.不確定解決方案對語言的依賴性.

The code doing the parsing/validation is written in Java. Not sure how language-dependent the solution will be.

理想情況下,該解決方案將允許 xml 作者聲明適當(dāng)?shù)募軜?gòu)位置和其他內(nèi)容,以便他/她可以在足夠智能的編輯器中即時(shí)驗(yàn)證 xml.

Ideally, the solution would permit the xml author to declare the appropriate schema locations and what-not so that s/he could get the xml validated on the fly in a sufficiently smart editor.

另外,<bar><baz> 的可能值是正交的,所以我不想通過擴(kuò)展來對每個(gè)可能的條進(jìn)行此操作/baz 組合.我的意思是,如果有 24 個(gè)可能的 bar 值/模式和 8 個(gè)可能的 baz 值/模式,我希望能夠編寫 1 + 24 + 8 = 33 個(gè)總模式,而不是 1 * 24 * 8 = 192 個(gè)總模式.

Also, the possible values for <bar> and <baz> are orthogonal, so I don't want to do this by extension for every possible bar/baz combo. What I mean is, if there are 24 possible bar values/schemas and 8 possible baz values/schemas, I want to be able to write 1 + 24 + 8 = 33 total schemas, instead of 1 * 24 * 8 = 192 total schemas.

另外,如果可能的話,我不希望將 bar-config 和 baz-config 拆分為單獨(dú)的 xml 文件.我意識(shí)到這可能會(huì)使所有問題變得更容易,因?yàn)槊總€(gè) xml 文件都有一個(gè)單一的架構(gòu),但我正在嘗試看看是否有一個(gè)好的單 xml 文件解決方案.

Also, I'd prefer to NOT break out the bar-config and baz-config into separate xml files if possible. I realize that might make all the problems much easier, as each xml file would have a single schema, but I'm trying to see if there is a good single-xml-file solution.

推薦答案

我終于想通了.

首先,在 foo 模式中,bar-config 和 baz-config 元素的類型包含 any 元素,如下所示:

First of all, in the foo schema, the bar-config and baz-config elements have a type which includes an any element, like this:

<sequence>
    <any minOccurs="0" maxOccurs="1"
        processContents="lax" namespace="##any" />
</sequence>

那么,在 xml 中,您必須使用 bar-config 或 baz-config 的子元素上的 xmlns 屬性指定正確的命名空間,如下所示:

In the xml, then, you must specify the proper namespace using the xmlns attribute on the child element of bar-config or baz-config, like this:

<bar-config>
    <config xmlns="http://www.example.org/bar/Alpha">
        ... config xml here ...
    </config>
</bar-config>

然后,bar Alpha 的 XML 模式文件將具有 http://www.example 的目標(biāo)命名空間.org/bar/Alpha 并將定義根元素 config.

Then, your XML schema file for bar Alpha will have a target namespace of http://www.example.org/bar/Alpha and will define the root element config.

如果您的 XML 文件具有兩個(gè)模式文件的名稱空間聲明和模式位置,這足以讓編輯器完成所有驗(yàn)證(至少對 Eclipse 來說足夠好).

If your XML file has namespace declarations and schema locations for both of the schema files, this is sufficient for the editor to do all of the validating (at least good enough for Eclipse).

到目前為止,我們已經(jīng)滿足了xml作者可以以在編輯器中驗(yàn)證的方式編寫xml的要求.

So far, we have satisfied the requirement that the xml author may write the xml in such a way that it is validated in the editor.

現(xiàn)在,我們需要消費(fèi)者能夠進(jìn)行驗(yàn)證.就我而言,我使用的是 Java.

Now, we need the consumer to be able to validate. In my case, I'm using Java.

如果您有機(jī)會(huì)提前知道需要用于驗(yàn)證的架構(gòu)文件,那么您只需創(chuàng)建一個(gè) Schema 對象并照常進(jìn)行驗(yàn)證,如下所示:

If by some chance, you know the schema files that you will need to use to validate ahead of time, then you simply create a single Schema object and validate as usual, like this:

Schema schema = factory().newSchema(new Source[] {
    new StreamSource(stream("foo.xsd")),
    new StreamSource(stream("Alpha.xsd")),
    new StreamSource(stream("Mercury.xsd")),
});

然而,在這種情況下,在我們解析主文檔之前,我們不知道要使用哪些 xsd 文件.所以,一般的程序是:

In this case, however, we don't know which xsd files to use until we have parsed the main document. So, the general procedure is to:

  1. 僅使用主 (foo) 架構(gòu)驗(yàn)證 xml
  2. 確定用于驗(yàn)證文檔部分的架構(gòu)
  3. 使用單獨(dú)的架構(gòu)查找作為要驗(yàn)證的部分的根節(jié)點(diǎn)
  4. 將該節(jié)點(diǎn)導(dǎo)入到一個(gè)全新的文檔中
  5. 使用其他架構(gòu)文件驗(yàn)證全新的文檔

注意事項(xiàng):似乎必須構(gòu)建可識(shí)別命名空間的文檔才能使其正常工作.

這是一些代碼(這是從我的代碼的各個(gè)地方撕下來的,所以復(fù)制和粘貼可能會(huì)引入一些錯(cuò)誤):

Here's some code (this was ripped from various places of my code, so there might be some errors introduced by the copy-and-paste):

// Contains the filename of the xml file
String filename;

// Load the xml data using a namespace-aware builder (the method 
// 'stream' simply opens an input stream on a file)
Document document;
DocumentBuilderFactory docBuilderFactory =
    DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
document = docBuilderFactory.newDocumentBuilder().parse(stream(filename));

// Create the schema factory
SchemaFactory sFactory = SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);

// Load the main schema
Schema schema = sFactory.newSchema(
    new StreamSource(stream("foo.xsd")));

// Validate using main schema
schema.newValidator().validate(new DOMSource(document));

// Get the node that is the root for the portion you want to validate
// using another schema
Node node= getSpecialNode(document);

// Build a Document from that node
Document subDocument = docBuilderFactory.newDocumentBuilder().newDocument();
subDocument.appendChild(subDocument.importNode(node, true));

// Determine the schema to use using your own logic
Schema subSchema = parseAndDetermineSchema(document);

// Validate using other schema
subSchema.newValidator().validate(new DOMSource(subDocument));

這篇關(guān)于要針對多個(gè) xsd 模式驗(yàn)證 XML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 快速门厂家批发_PVC快速卷帘门_高速门_高速卷帘门-广州万盛门业 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 福建自考_福建自学考试网| 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 北京成考网-北京成人高考网| POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 顶空进样器-吹扫捕集仪-热脱附仪-二次热解吸仪-北京华盛谱信仪器 | 真空包装机-诸城市坤泰食品机械有限公司 | 纳米二氧化硅,白炭黑,阴离子乳化剂-臻丽拾科技 | 氟塑料磁力泵-不锈钢离心泵-耐腐蚀化工泵厂家「皖金泵阀」 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 雄松华章(广州华章MBA)官网-专注MBA/MPA/MPAcc/MEM辅导培训 | 铝单板_铝窗花_铝单板厂家_氟碳包柱铝单板批发价格-佛山科阳金属 | 模切之家-专注服务模切行业的B2B平台! | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 进口试验机价格-进口生物材料试验机-西安卡夫曼测控技术有限公司 | 幂简集成 - 品种超全的API接口平台, 一站搜索、试用、集成国内外API接口 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 防堵吹扫装置-防堵风压测量装置-电动操作显示器-兴洲仪器 | 不锈钢发酵罐_水果酒发酵罐_谷物发酵罐_山东誉诚不锈钢制品有限公司 |