构建双语XML Schema:调整生成器以生成双语XML文档
Got it, let's tackle this bilingual XML requirement. Since you already have the occurrence constraints (min/max) sorted out, the core task is adjusting your schema to support dual-language content for your elements. Here are two practical approaches you can take, depending on how you want your final XML structure to look:
Approach 1: Bilingual Content as Child Elements (Structured Pair)
This is ideal if you want each element to explicitly contain both language versions as nested elements. We'll define a reusable complex type for bilingual text, then apply it to your Elem1.
Modified XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="http://example.com/your-namespace" targetNamespace="http://example.com/your-namespace" elementFormDefault="qualified"> <!-- Reusable complex type for bilingual text --> <xs:complexType name="BilingualTextType"> <xs:sequence> <!-- Mandatory Chinese content; adjust minOccurs if optional --> <xs:element name="zh" type="xs:string" minOccurs="1" maxOccurs="1"/> <!-- Mandatory English content; adjust minOccurs if optional --> <xs:element name="en" type="xs:string" minOccurs="1" maxOccurs="1"/> </xs:sequence> </xs:complexType> <xs:element name="root"> <xs:complexType> <xs:sequence> <!-- Keep your existing occurrence controls here --> <xs:element ref="my:Elem1" minOccurs="1" maxOccurs="1"/> </xs:sequence> </xs:complexType> </xs:element> <!-- Replace xs:string with our bilingual type --> <xs:element name="Elem1" type="my:BilingualTextType"/> </xs:schema>
Example generated XML:
<my:root xmlns:my="http://example.com/your-namespace"> <my:Elem1> <my:zh>这是Elem1的中文内容</my:zh> <my:en>This is the English content for Elem1</my:en> </my:Elem1> </my:root>
Approach 2: Language Attribute (xml:lang)
If you prefer having separate instances of Elem1 for each language (with a language identifier), use the standard xml:lang attribute. This is great for flexible multilingual support beyond just two languages.
Modified XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="http://example.com/your-namespace" xmlns:xml="http://www.w3.org/XML/1998/namespace" targetNamespace="http://example.com/your-namespace" elementFormDefault="qualified"> <xs:element name="root"> <xs:complexType> <xs:sequence> <!-- Set min/max to 2 to enforce both languages --> <xs:element ref="my:Elem1" minOccurs="2" maxOccurs="2"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Elem1"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <!-- Require the xml:lang attribute to specify language --> <xs:attribute ref="xml:lang" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:schema>
Example generated XML:
<my:root xmlns:my="http://example.com/your-namespace"> <my:Elem1 xml:lang="zh">这是Elem1的中文内容</my:Elem1> <my:Elem1 xml:lang="en">This is the English content for Elem1</my:Elem1> </my:root>
Both approaches preserve your existing occurrence control logic—just tweak the minOccurs/maxOccurs values as needed for your specific use case.
内容的提问来源于stack exchange,提问作者Oley

