Don't understand this piece of XML
I'm trying to make an XML schema, and not having too much success. there's a construct I don't understand ... here's what I have so far for the schema, followed by the XML file I'm trying to make it for .. any suggestions?
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="CorbelFile">
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="Project" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="Project">
<xsd:sequence>
<xsd:element name="ADProductID"
type="xsd:string" />
<xsd:element name="ADProductID"
type="xsd:string" />
<xsd:element name="ADProductName"
type="xsd:string" />
<xsd:element name="ADProductVersion"
type="xsd:string" />
<xsd:element name="ADProjectId"
type="xsd:string" />
<xsd:element name="ADProjectName"
type="xsd:string" />
<xsd:element name="ADFileLoc"
type="xsd:string" />
<xsd:group ref="Answers"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:group>
<xsd:group name="Answers">
<xsd:sequence>
<xsd:element name="var"/>
</xsd:sequence>
</xsd:group>
</xsd:schema>
XML
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<CorbelFile>
<Project>
<ADProductId>28359</ADProductId>
<ADProductName>IDP 403(b)</ADProductName>
<ADProductVersion>7.0</ADProductVersion>
<ADProjectId>00010001</ADProjectId>
<ADProjectName>Independence Dental Center PS</ADProjectName>
<ADFileLoc>C:\Program Files\Relius\Docs\dp_data\1\0\00010001.ck2</ADFileLoc>
<answers>
<Var Ld="PKGA" />
<Var Ld="BKTHead2" />
<Var Ld="SingleSpace" />
<Var Ld="MarginRagged" />
<Var Ld="SPDTimes" />
<Var Ld="BKTTimes" />
<Var Ld="PlanNameB" />
<Var Ld="ERPYEnds" />
<Var Ld="InitialEffDate" />
<Var Ld="2000SafHarEnhancMatch" />
<Var Ld="StateCollege" />
<Var Ld="CharityCorp" />
<Var Ld="ExceptEEPartTime">Independence</Var>
<Var Ld="ExceptEEStudent">Missouri</Var>
<Var Ld="ExceptEENonResAliens">11500 East 23rd Street</Var>
<Var Ld="ExceptEECollBargAgree">64052</Var>
<Var Ld="EEExclNA">(816) 254-7200</Var>
<Var Ld="NoExclElig">43-1118440</Var>
<Var Ld="ACP2000CurrentYR">001</Var>
<Var Ld="ExceptEEHR" />
<Var Ld="1YRAndHalfServReq">Profit Sharing Plan and Trust</Var>
<Var Ld="YRServReq">Independence Dental Center, Inc.</Var>
<Var Ld="Age21">INDEPENDENCE DENTAL CENTER, INC.</Var>
<Var Ld="AgeOther">PROFIT SHARING PLAN AND TRUST</Var>
<Var Ld="1stDayMonth">September 30</Var>
<Var Ld="1stDayPYNextFollowing">October 1</Var>
<Var Ld="CompExclOT">September 30</Var>
<Var Ld="CompExcl414s">October 1</Var>
<Var Ld="CompSalRed">December 12, 1963</Var>
<Var Ld="NASRA" />
<Var Ld="ACPPreYR" />
<Var Ld="QNECPY" />
<Var Ld="QNEC500HRs" />
<Var Ld="QNECYOS" />
<Var Ld="ShareDiscrYOSEndPY" />
<Var Ld="ForfeitRedERContr" />
<Var Ld="7YRGrade" />
<Var Ld="MinDistCYAftr2000">Brad Babcock</Var>
<Var Ld="MinDistCYAftr2001">Lauren Carr</Var>
<Var Ld="NServPriorEffDate" />
<Var Ld="1999SafeHarbContr">John Smith</Var>
<Var Ld="NRD1stDayMonth" />
<Var Ld="NRAPartAnnivBDay1" />
<Var Ld="NERD" />
<Var Ld="NDistrJSA" />
<Var Ld="Multi2ndERCityStateZip" />
<Var Ld="2000SafHarNonElectOthrPln" />
<Var Ld="TermDistrImmed" />
<Var Ld="YTransRoll403b" />
<Var Ld="LoansNA" />
<Var Ld="Loans10000DeMin" />
<Var Ld="LoansSRALimit" />
<Var Ld="YDirInv" />
<Var Ld="DirInvRollTrans" />
<Var Ld="HrdshipSafeHarb401k" />
<Var Ld="EmployerPhone" />
<Var Ld="NMulti2ndER" />
<Var Ld="Multi2ndERName" />
<Var Ld="YMulti3rdER" />
<Var Ld="Multi3rdERStreet" />
<Var Ld="Multi4thEIN" />
<Var Ld="Multi5thERStreet" />
<Var Ld="SumPlanOnly" />
<Var Ld="YPSBKT" />
<Var Ld="YSBJPA" />
<Var Ld="NAPriorCurrentYRTestMethP" />
<Var Ld="NHCPContrPost973Perc" />
<Var Ld="NComplyGATTUSERRASBJPATRA" />
<Var Ld="NSafeHarbMatch" />
<Var Ld="DeferBenOption" />
<Var Ld="PayOldAnnuitDate">59 1/2</Var>
<Var Ld="NAHrdshipSafeHarb401k">60th</Var>
<Var Ld="VServ3" />
<Var Ld="VServ6" />
<Var Ld="VPerc1" />
<Var Ld="VPerc6" />
<Var Ld="ACPCurrentPre97" />
<Var Ld="ACPCurrentPre98" />
<Var Ld="YDiscrMC4PercCompPost99PY" />
<Var Ld="EG_Hrdshp_Suspensn_Red_N" />
<Var Ld="EG_Suspensn_2001_NA" />
</answers>
</Project>
<Project>
<ADProductId>83</ADProductId>
<ADProductName>IDP Money Purchase/Target</ADProductName>
<ADProductVersion>2.0</ADProductVersion>
<ADProjectId>00010002</ADProjectId>
<ADProjectName>Independence Dental Center MP</ADProjectName>
<ADFileLoc>C:\Program Files\Relius\Docs\dp_data\2\0\00010002.ck2</ADFileLoc>
<answers />
</Project>
<Project>
<ADProductId>24581</ADProductId>
<ADProductName>IDP Revised SA</ADProductName>
<ADProductVersion>10.0</ADProductVersion>
<ADProjectId>000102C9</ADProjectId>
<ADProjectName>2010 - Hanover - Trustee</ADProjectName>
<ADFileLoc>C:\Program Files\Relius\Docs\dp_data\9\C\000102C9.ck2</ADFileLoc>
<answers>
<Var Ld="NShortAmendment" />
<Var Ld="PASMM" />
<Var Ld="PACorpResol" />
<Var Ld="PTimes" />
<Var Ld="SPDTimes" />
<Var Ld="CorbelClientNameA">AMERICAN FIDUCIARY CORPORATION</Var>
<Var Ld="SA401kProfitSharing" />
<Var Ld="EmployerNameA">Hanover Obstetrics & Gynecology, Inc.</Var>
<Var Ld="Corp" />
<Var Ld="EmployerStreet">135 Webster Street</Var>
<Var Ld="EmployerCity">Hanover</Var>
<Var Ld="EmployerState">Massachusetts</Var>
<Var Ld="EmployerZip">02339</Var>
<Var Ld="EmployerPhone">(781) 878-7020</Var>
<Var Ld="EIN">04-2477545</Var>
<Var Ld="PlanNumOther">001</Var>
<Var Ld="PlanNameA">Hanover Obstetrics & Gynecology, Inc.</Var>
<Var Ld="PlanNameB">401(k) Profit Sharing Plan</Var>
<Var Ld="AmendmentOne" />
<Var Ld="AmendDate">August 1, 2010</Var>
<Var Ld="NWitnessLine" />
<Var Ld="PlanAdminERAddress" />
<Var Ld="IndivTrustee" />
<Var Ld="DesignatedAmendments" />
<Var Ld="401KfeaturesNoChange" />
<Var Ld="DiscrContrNoChange" />
<Var Ld="DistributionsAmendNo" />
<Var Ld="MiscAmendYes" />
<Var Ld="NoChangeCompDefinition" />
<Var Ld="NLoans" />
<Var Ld="HCENoChange" />
<Var Ld="NElimOfFamAggreg" />
<Var Ld="AddTransRollNo" />
<Var Ld="PYFYNoChange" />
<Var Ld="ChangeTrustNameYes" />
<Var Ld="ChangedTrustee1Name">Albert Marcus, M.D.</Var>
<Var Ld="ChangedTrustee1EffDate">August 1, 2010</Var>
<Var Ld="ChangedTrustee1Removed" />
<Var Ld="MergFreezeTermNA" />
<Var Ld="NERAdopt" />
<Var Ld="RelDate" />
</answers开发者_StackOverflow社区>
</Project>
In Visual Studio open your xml file, go for the "XML" menu, select "Create Schema".
Instead of using a group, define "Var" as an element with an "Ld" attribute:
<complexType name="Var">
<annotation>
<documentation>
Description of what a Var is
</documentation>
</annotation>
<attribute name="Ld" type="string">
<annotation>
<documentation>
Description of what this attribute means
</documentation>
</annotation>
</attribute>
</complexType>
The define the list of answers:
<complexType name="Answers">
<annotation>
<documentation>
A list of Var elements
</documentation>
</annotation>
<sequence>
<element name="Var" type="Var" minOccurs="0" maxOccurs="unbounded">
<annotation>
<documentation>
The list of Var elements
</documentation>
</annotation>
</element>
</sequence>
</complexType>
Then instead of this:
<xsd:group ref="Answers"
minOccurs="0"
maxOccurs="unbounded"/>
You'd use this:
<element name="answers" type="Answers"/>
Also, a good rule of thumb is to make the default namespace match the type of file you're writing. So if you're writing a schema, put this in the root element instead and you won't have to write "xsd" prefixes everywhere:
xmlns="http://www.w3.org/2001/XMLSchema"
And it's also a good idea to create a target namespace for your schema so that your elements have an associated namespace:
targetNamespace="http://my.example.com/schema"
xmlns:mec="http://my.example.com/schema"
Which would mean your elements would be referred to as type="mec:Project" instead of just "Project" and therefore prevent any future problems when using this schema in conjunction with another one.
How to define an element to contain only text but also an attribute
Generally type of elements with only text content has to be xs:simpleType
. In this case this causes a problem, because type definition of elements that can contain attributes (or other elements) must be xs:complexType
. The solution is to use xs:simpleContent
to allow text only and xs:extension
to extend this type with the desired attributes.
Creating complex text-only elements
xs:simpleContent
is used to derive types from other built-in or previously defined types. It requires either xs:extension
or xs:restriction
child element with attribute base
that defines the base type (simple) that you will extend or restrict. In this case the base type can be xs:string
. xs:extension
or xs:restriction
contains the desired new features.
<xs:element name="var">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Ld" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Several attribute definitions can added inside xs:extension
- in this case we only needed one. xs:simpleContent
cannot contain any element definitions, only new attributes. If the base type is not the same as any of the built-in schema datatypes, for example if the text has some length restrictions, you must define the base type as a global simple type definition and use its name in the base
attribute.
Allowing text inside elements that can contain other elements is simpler: just use <xs:complexType mixed="true">
when defining the element contents.
精彩评论