当前位置:网站首页>从类生成XML架构
从类生成XML架构
2022-07-05 17:43:00 【用户7741497】
本章介绍如何使用%XML.Schema
从启用了XML的类生成XML架构。
概述
要生成为同一XML命名空间中的多个类定义类型的完整架构,请使用%XML.Schema
构建架构,然后使用%XML.Writer
为其生成输出。
从多个类构建架构
要构建XML架构,请执行以下操作:
- 创建
%XML.Schema
实例。 - 可以选择设置实例的属性:
- 若要为任何其他未分配的类型指定命名空间,请指定
DefaultNamespace
属性。默认值为NULL
。 - 默认情况下,类及其属性的类文档包含在模式的
<annotation>
元素中。 要禁用此功能,请将IncludeDocumentation
属性指定为0。
注意:必须在调用AddSchemaType()
方法之前设置这些属性。
- 调用实例的
AddSchemaType()
方法。
method AddSchemaType(class As %String,
top As %String = "",
format As %String,
summary As %Boolean = 0,
input As %Boolean = 0,
refOnly As %Boolean = 0) as %Status
- class是支持xml的类的完整包名和类名。
- top 是可选的; 如果指定,它将覆盖该类的类型名。
- format指定此类型的格式。 它必须是
"literal"
(文字格式,默认),"encoded"
(用于SOAP编码),"encoded12"
(用于SOAP 1.2编码),或"element"
。 值“element”
与元素位于顶层的文字格式相同。 - summary,如果为true,将导致InterSystems IRIS启用xml的类的
XMLSUMMARY
参数。 如果指定了此参数,则模式将只包含该参数列出的属性。 - input,如果为true,将导致InterSystems IRIS获取输入模式,而不是输出模式。 在大多数情况下,输入模式和输出模式是相同的; 如果为类的属性指定
XMLIO
属性参数,则它们是不同的。 - refOnly如果为true,将导致InterSystems IRIS仅为引用的类型生成模式,而不是为给定的类和所有引用的类型生成模式。
这个方法返回一个应该被检查的状态。
- 根据需要重复前面的步骤。
- 如果要定义导入模式的位置,可以调用
DefineLocation()
方法。
method DefineLocation(namespace As %String, location As %String)
namespace 是一个或多个引用类使用的名称空间,位置是对应模式(XSD文件)的URL或路径和文件名。
可以重复调用此方法来为多个导入的模式添加位置。
如果不使用这个方法,模式会包含一个<import>
指令,但是不会给出模式的位置。
- 要定义额外的
<import>
指令,可以调用DefineExtraImports()
方法。
method DefineExtraImports(namespace As %String, ByRef imports)
namespace是<import>
指令应该添加到的命名空间,imports是一个多维数组,形式如下:
Node | Value |
---|---|
arrayname("namespace URI") | 字符串,给出此名称空间的模式(XSD文件)的位置。 |
为架构生成输出
按照上一节所述创建%XML.Schema
的实例后,请执行以下操作以生成输出:
- 调用实例的
GetSchema()
方法将架构作为文档对象模型(DOM)的节点返回。
此方法只有一个参数:模式的目标命名空间的URI。该方法返回%XML.Node
的一个实例,该实例在“将XML文档表示为DOM”一章中介绍。
如果模式没有命名空间,请使用“”
作为GetSchema()
的参数。
- 可以选择修改此DOM。
- 要生成架构,请执行以下操作:
a. 创建%XML.Write
的实例,并可选择设置属性(如缩进)。
b. 可以选择调用编写器的AddNamespace()
方法和其他方法,将名称空间声明添加到<schema>
元素。
因为架构可能引用简单的XSD类型,所以调用AddSchemaNamespace()
来添加XML模式命名空间很有用。
c. 使用架构作为参数,调用编写器的DocumentNode()
或Tree()
方法。
示例
简单的示例
第一个示例显示了基本步骤:
Set schemawriter=##class(%XML.Schema).%New()
//添加类和包(例如)
Set status=schemawriter.AddSchemaType("Facets.Test")
//通过其URI(在本例中为NULL)检索架构
Set schema=schemawriter.GetSchema("")
//create writer
Set writer=##class(%XML.Writer).%New()
Set writer.Indent=1
//use writer
Do writer.DocumentNode(schema)
更复杂的架构示例
Class SchemaWriter.Person Extends (%Persistent, %XML.Adaptor)
{
Parameter NAMESPACE = "http://www.myapp.com";
Property Name As %Name;
Property DOB As %Date(FORMAT = 5);
Property PatientID as %String;
Property HomeAddress as Address;
Property OtherAddress as AddressOtherNS ;
}
Address
类定义在相同的XML名称空间(“http://www.myapp.com”
)中,而OtherAddress
类定义在不同的XML名称空间(“http://www.other.com”
)中。
Company
类也被定义在XML名称空间“http://www.myapp.com”
中。 其定义如下:
Class SchemaWriter.Company Extends (%Persistent, %XML.Adaptor)
{
Parameter NAMESPACE = "http://www.myapp.com";
Property Name As %String;
Property CompanyID As %String;
Property HomeOffice As Address;
}
注意,不存在连接Person
和Company
类的属性关系。
要为命名空间"http://www.myapp.com"
生成模式,我们可以使用以下方法:
ClassMethod Demo()
{
Set schema=##class(%XML.Schema).%New()
Set schema.DefaultNamespace="http://www.myapp.com"
Set status=schema.AddSchemaType("SchemaWriter.Person")
Set status=schema.AddSchemaType("SchemaWriter.Company")
Do schema.DefineLocation("http://www.other.com","c:/other-schema.xsd")
Set schema=schema.GetSchema("http://www.myapp.com")
//create writer
Set writer=##class(%XML.Writer).%New()
Set writer.Indent=1
Do writer.AddSchemaNamespace()
Do writer.AddNamespace("http://www.myapp.com")
Do writer.AddNamespace("http://www.other.com")
Set status=writer.DocumentNode(schema)
If $$$ISERR(status) {Do $system.OBJ.DisplayError() Quit }
}
输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:s01="http://www.myapp.com"
xmlns:s02="http://www.other.com"
elementFormDefault="qualified"
targetNamespace="http://www.myapp.com">
<import namespace="http://www.other.com" schemaLocation="c:/other-schema.xsd"/>
<complexType name="Person">
<sequence>
<element minOccurs="0" name="Name" type="s:string"/>
<element minOccurs="0" name="DOB" type="s:date"/>
<element minOccurs="0" name="PatientID" type="s:string"/>
<element minOccurs="0" name="HomeAddress" type="s01:Address"/>
<element minOccurs="0" name="OtherAddress" type="s02:AddressOtherNS"/>
</sequence>
</complexType>
<complexType name="Address">
<sequence>
<element minOccurs="0" name="State">
<simpleType>
<restriction base="s:string">
<maxLength value="2"/>
</restriction>
</simpleType>
</element>
<element minOccurs="0" name="Zip">
<simpleType>
<restriction base="s:string">
<maxLength value="10"/>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<complexType name="Company">
<sequence>
<element minOccurs="0" name="Name" type="s:string"/>
<element minOccurs="0" name="CompanyID" type="s:string"/>
<element minOccurs="0" name="HomeOffice" type="s01:Address"/>
</sequence>
</complexType>
</schema>
请注意以下几点:
- 模式包括
Person
及其所有引用的类的类型,以及Company
及其所有引用的类的类型。 <import>
指令导入了OtherAddress
类使用的命名空间; 因为我们使用了DefineLocation()
,所以这个指令还指示了相应模式的位置。- 因为我们在调用
DocumentNode()
之前使用了AddSchemaNamespace()
和AddNamespace()
,所以<schema>
元素包含了名称空间声明,它为这些名称空间定义了前缀。 - 如果我们没有使用
AddSchemaNamespace()
和AddNamespace()
,<schema>
将不会包含这些名称空间声明,模式将会如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.myapp.com">
<import namespace="http://www.other.com" schemaLocation="c:/other-schema.xsd"/>
<complexType name="Person">
<sequence>
<element minOccurs="0" name="Name" type="s01:string" xmlns:s01="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="DOB" type="s02:date" xmlns:s02="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="PatientID" type="s03:string" xmlns:s03="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="HomeAddress" type="s04:Address" xmlns:s04="http://www.myapp.com"/>
<element minOccurs="0" name="OtherAddress" type="s05:AddressOtherNS" xmlns:s05="http://www.other.com"/>
</sequence>
</complexType>
<complexType name="Address">
<sequence>
<element minOccurs="0" name="State">
<simpleType>
<restriction base="s06:string" xmlns:s06="http://www.w3.org/2001/XMLSchema">
边栏推荐
- 数据访问 - EntityFramework集成
- 请问下为啥有的表写sql能查到数据,但在数据地图里查不到啊,查表结构也搜不到
- Which platform of outer disk gold is regular and safe, and how to distinguish it?
- Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
- [JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
- Redis基础
- Tkinter window preload
- Career advancement Guide: recommended books for people in big factories
- Leetcode daily practice: rotating arrays
- EPM相关
猜你喜欢
MySQL之知识点(六)
Mongodb (quick start) (I)
C # mixed graphics and text, written to the database in binary mode
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Cmake tutorial step1 (basic starting point)
leetcode每日一题:字符串中的第一个唯一字符
Thesis reading_ Medical NLP model_ EMBERT
Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
「运维有小邓」用于云应用程序的单点登录解决方案
Zabbix
随机推荐
Interpretation: how to deal with the current security problems faced by the Internet of things?
Oracle Recovery Tools ----oracle数据库恢复利器
Webapp development - Google official tutorial
深拷贝与浅拷贝【面试题3】
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
rsync
What are the changes in the 2022 PMP Exam?
Seven Devops practices to improve application performance
论文阅读_中文NLP_LTP
解读:如何应对物联网目前面临的安全问题?
Mask wearing detection based on yolov3
Elk log analysis system
Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
Thesis reading_ Chinese NLP_ LTP
Knowledge points of MySQL (6)
Mongodb (quick start) (I)
ITK Example
leetcode每日一练:旋转数组
职场进阶指南:大厂人必看书籍推荐
mybash