本章介绍如何使用%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
"literal"
(文字格式,默认),"encoded"
(用于SOAP编码),"encoded12"
(用于SOAP 1.2编码),或"element"
。
值“element”
与元素位于顶层的文字格式相同。XMLSUMMARY
参数。
如果指定了此参数,则模式将只包含该参数列出的属性。XMLIO
属性参数,则它们是不同的。这个方法返回一个应该被检查的状态。
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()
的参数。
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">
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。