当前位置:网站首页>从类生成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">边栏推荐
- Sentinel flow guard
- Which platform of outer disk gold is regular and safe, and how to distinguish it?
- Cartoon: looking for the k-th element of an unordered array (Revised)
- 职场进阶指南:大厂人必看书籍推荐
- [BeanShell] there are many ways to write data locally
- LeetCode 练习——206. 反转链表
- Customize the theme of matrix (I) night mode
- 每日一练:关于日期的一系列
- 基于YOLOv3的口罩佩戴检测
- Data access - entityframework integration
猜你喜欢

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

Neural network self cognition model

Thesis reading_ Medical NLP model_ EMBERT

Zabbix

2022新版PMP考试有哪些变化?

提高应用程序性能的7个DevOps实践

GFS distributed file system

统计php程序运行时间及设置PHP最长运行时间

leetcode每日一练:旋转数组

Cmake tutorial Step2 (add Library)
随机推荐
多线程(一) 进程与线程
Teamcenter 消息注册前操作或后操作
LeetCode 练习——206. 反转链表
Leetcode exercise - 206 Reverse linked list
如何保存训练好的神经网络模型(pytorch版本)
Cartoon: looking for the best time to buy and sell stocks
Oracle recovery tools -- Oracle database recovery tool
Elk log analysis system
VBA drives SAP GUI to realize office automation (II): judge whether elements exist
Knowledge points of MySQL (6)
每日一练:关于日期的一系列
c#图文混合,以二进制方式写入数据库
钉钉开放平台小程序API的缓存接口都有哪些内容?
QT控制台打印输出
How to modify MySQL fields as self growing fields
网络威胁分析师应该具备的十种能力
Data access - entityframework integration
Check the WiFi password connected to your computer
Cartoon: interesting [pirate] question
Delete some elements in the array