当前位置:网站首页>Generate XML schema from class
Generate XML schema from class
2022-07-05 18:00:00 【User 7741497】
This chapter describes how to use %XML.Schema Enabled from XML Class generation for XML framework .
summary
To generate the same XML Multiple classes in the namespace define the complete schema of the type , Please use %XML.Schema Build architecture , And then use %XML.Writer Generate output for it .
Build an architecture from multiple classes
To build XML framework , Do the following :
- establish
%XML.Schemaexample . - You can choose to set the properties of the instance :
- To specify a namespace for any other unassigned type , Please specify
DefaultNamespaceattribute . The default value isNULL. - By default , Class documents for classes and their attributes are contained in the
<annotation>In the elements . To disable this feature , Please putIncludeDocumentationProperty specified as 0.
Be careful : Must be called on AddSchemaType() Method to set these properties .
- Of the calling instance
AddSchemaType()Method .
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 It's supporting xml The complete package name and class name of the class .
- top It's optional ; If specified , It will override the type name of this class .
- format Specify the format of this type . It has to be
"literal"( Text format , Default ),"encoded"( be used for SOAP code ),"encoded12"( be used for SOAP 1.2 code ), or"element". value“element”The same format as the text with the element at the top . - summary, If true, Will lead to InterSystems IRIS Enable xml Of
XMLSUMMARYParameters . If this parameter is specified , Then the schema will only contain the attributes listed by this parameter . - input, If true, Will lead to InterSystems IRIS Get input mode , Instead of output mode . in the majority of cases , The input mode and output mode are the same ; If you specify
XMLIOProperty parameters , Then they are different . - refOnly If true, Will lead to InterSystems IRIS Generate schemas only for referenced types , Instead of generating schemas for a given class and all referenced types .
This method returns a state that should be checked .
- Repeat the previous steps as needed .
- If you want to define the location of the import mode , You can call
DefineLocation()Method .
method DefineLocation(namespace As %String, location As %String)namespace Is the namespace used by one or more reference classes , The position is the corresponding pattern (XSD file ) Of URL Or path and file name .
You can call this method repeatedly to add locations for multiple imported schemas .
If you don't use this method , The pattern will contain a <import> Instructions , But it will not give the position of the mode .
- Define additional
<import>Instructions , You can callDefineExtraImports()Method .
method DefineExtraImports(namespace As %String, ByRef imports)namespace yes <import> The namespace to which the directive should be added ,imports It's a multidimensional array , Form the following :
Node | Value |
|---|---|
arrayname("namespace URI") | character string , Give the schema of this namespace (XSD file ) The location of . |
Generate output for schema
Create as described in the previous section %XML.Schema After an instance of the , Please do the following to generate output :
- Of the calling instance
GetSchema()Method takes the schema as the document object model (DOM) Node return of .
This method has only one parameter : The target namespace of the schema URI. This method returns %XML.Node An example of , The example is in “ take XML The document is represented as DOM” Chapter one introduces .
If the schema has no namespace , Please use “” As GetSchema() Parameters of .
- You can choose to modify this DOM.
- To generate a schema , Do the following :
a. establish %XML.Write Example , And you can choose to set properties ( Like indenting ).
b. You can choose to call the AddNamespace() Methods and other methods , Add namespace declarations to <schema> Elements .
Because the architecture may refer to simple XSD type , So call AddSchemaNamespace() To add XML Schema namespaces are useful .
c. Use schema as a parameter , Call the writer's DocumentNode() or Tree() Method .
Example
A simple example
The first example shows the basic steps :
Set schemawriter=##class(%XML.Schema).%New()
// Add classes and packages ( for example )
Set status=schemawriter.AddSchemaType("Facets.Test")
// Through its URI( In this case NULL) Retrieval architecture
Set schema=schemawriter.GetSchema("")
//create writer
Set writer=##class(%XML.Writer).%New()
Set writer.Indent=1
//use writer
Do writer.DocumentNode(schema)More complex architectural examples
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 Classes are defined in the same XML The name space (“http://www.myapp.com”) in , and OtherAddress Classes are defined in different XML The name space (“http://www.other.com”) in .
Company Classes are also defined in XML The name space “http://www.myapp.com” in . Its definition is as follows :
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;
} Be careful , No connection exists Person and Company Attribute relationship of class .
Namespace "http://www.myapp.com" Generation patterns , We can use the following methods :
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 }
}Output is as follows :
<?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>Please pay attention to the following points :
- Patterns include
PersonAnd the types of all the referenced classes , as well asCompanyAnd the types of all the referenced classes . <import>The instruction importsOtherAddressNamespace used by class ; Because we usedDefineLocation(), So this instruction also indicates the position of the corresponding mode .- Because we're calling
DocumentNode()I usedAddSchemaNamespace()andAddNamespace(), therefore<schema>The element contains a namespace declaration , It defines prefixes for these namespaces . - If we don't use
AddSchemaNamespace()andAddNamespace(),<schema>These namespace declarations will not be included , The pattern will look like this :
<?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">边栏推荐
- Size_t 是无符号的
- 7 pratiques devops pour améliorer la performance des applications
- Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
- Sentinel-流量防卫兵
- To solve the problem of "double click PDF file, pop up", please install Evernote program
- ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
- 数据访问 - EntityFramework集成
- Neural network self cognition model
- 图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
- Leetcode daily question: the first unique character in the string
猜你喜欢
![最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]

ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V

神经网络自我认知模型

Tencent music launched its new product "quyimai", which provides music commercial copyright authorization

Zabbix

Thesis reading_ Chinese NLP_ LTP

Thesis reading_ Medical NLP model_ EMBERT

RSE2020/云检测:基于弱监督深度学习的高分辨率遥感图像精确云检测

论文阅读_中文NLP_LTP

What are the changes in the 2022 PMP Exam?
随机推荐
[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
Teamcenter 消息注册前操作或后操作
[BeanShell] there are many ways to write data locally
登录连接 CDB 和 PDB
华夏基金:基金行业数字化转型实践成果分享
To solve the problem of "double click PDF file, pop up", please install Evernote program
Customize the theme of matrix (I) night mode
Size_t 是无符号的
“12306” 的架构到底有多牛逼?
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
Oracle recovery tools -- Oracle database recovery tool
JVM第三话 -- JVM性能调优实战和高频面试题记录
寻找第k小元素 前k小元素 select_k
What are the requirements for PMP certification? How much is it?
访问数据库使用redis作为mysql的缓存(redis和mysql结合)
Leetcode exercise - 206 Reverse linked list
7 pratiques devops pour améliorer la performance des applications
集群部署如何解决海量视频接入与大并发需求?
Disabling and enabling inspections pycharm