XSD限制partent元素的子元素数

时间:2017-07-03 19:33:29

标签: xml xsd

所以我试图为我们将在程序中使用的XML文件定义一个模式。有一个子元素,它定义了源语言和整个文件的任何目标语言。始终只有一种源语言,并且每个translationUnit都必须相同。可以有任意数量的targetLanguages,但每个translationUnit必须具有完全相同的targetLanguages,它们可以不同。

XSD或其他架构格式是否有办法:

a)要求每个translationUnits的sourceSegment的langLocale与jobLanguages元素中的sourceLang相同吗?

b)要求每个translationUnits的targetSegment元素的langLocale属性与jobLanguages元素的targetLang元素的langLocales匹配吗?

基本上,随XML一起发送的每个请求必须在整个文档中保持一致,有些translationUnits不能使用不同的源语言或不同的目标语言集。

示例XML文件:

<?xml version="1.0" encoding="utf-8"?>
<translationBody>
    <translationJob>
        <jobLanguages>
            <sourceLang>en-US</sourceLang>
            <targetLang>ja-JP</targetLang>
            <targetLang>ko-KR</targetLang>
            <targetLang>es-ES</targetLang>
            <targetLang>de-DE</targetLang>
            <targetLang>fr-FR</targetLang>
            <targetLang>ru-RU</targetLang>
            <targetLang>zh-CN</targetLang>
            <targetLang>zh-TW</targetLang>
            <targetLang>ar-AA</targetLang>
        </jobLanguages>
        <translationUnit active="true" key="introText" notes="Notes for translator" lenRestriction="25">
            <sourceSegment langLocale="en-US">Some text for translation.</sourceSegment>
            <targetSegment langLocale="ja-JP">翻訳用テキスト</targetSegment>
            <targetSegment langLocale="ko-KR">번역을위한 일부 텍스트"</targetSegment>
            <targetSegment langLocale="es-ES">Algunos textos para traducción</targetSegment>
            <targetSegment langLocale="de-DE">Irgendein Text für Übersetzung</targetSegment>
            <targetSegment langLocale="fr-FR">Quelques textes pour la traduction</targetSegment>
            <targetSegment langLocale="ru-RU">Некоторый текст для перевода</targetSegment>
            <targetSegment langLocale="zh-CN">一些翻译文字</targetSegment>
            <targetSegment langLocale="zh-TW">一些翻譯文字</targetSegment>
            <targetSegment langLocale="ar-AA">بعض النصوص للترجمة</targetSegment>
        </translationUnit>
        <translationUnit active="true" key="headerText" notes="Notes for translator">
            <sourceSegment langLocale="en-US">New text for translation.</sourceSegment>
            <targetSegment langLocale="ja-JP"></targetSegment>
            <targetSegment langLocale="ko-KR"></targetSegment>
            <targetSegment langLocale="es-ES"></targetSegment>
            <targetSegment langLocale="de-DE"></targetSegment>
            <targetSegment langLocale="fr-FR"></targetSegment>
            <targetSegment langLocale="ru-RU"></targetSegment>
            <targetSegment langLocale="zh-CN"></targetSegment>
            <targetSegment langLocale="zh-TW"></targetSegment>
            <targetSegment langLocale="ar-AA"></targetSegment>
        </translationUnit>
        <translationUnit active="false" key="buttonText" notes="Text that is no longer active">
            <sourceSegment langLocale="en-US">New text for translation.</sourceSegment>
            <targetSegment langLocale="ja-JP"></targetSegment>
            <targetSegment langLocale="ko-KR"></targetSegment>
            <targetSegment langLocale="es-ES"></targetSegment>
            <targetSegment langLocale="de-DE"></targetSegment>
            <targetSegment langLocale="fr-FR"></targetSegment>
            <targetSegment langLocale="ru-RU"></targetSegment>
            <targetSegment langLocale="zh-CN"></targetSegment>
            <targetSegment langLocale="zh-TW"></targetSegment>
            <targetSegment langLocale="ar-AA"></targetSegment>
        </translationUnit>
    </translationJob>
</translationBody>

当前XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="sourceString">
    <xs:annotation id="SourceStringDesc"><xs:documentation>This is a type setup to ensure that the length of the sourcString is greater than 0. Essentially, it is not ok to have a blank sourceSegment.</xs:documentation></xs:annotation>
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="langString">
    <xs:annotation id="langStringDesc"><xs:documentation>This is a type setup to ensure that the length of the langString is greater than 0. Essentially, it is not ok to have a blank langString.</xs:documentation></xs:annotation>
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:pattern value="da-DK|ms-MY|it-IT|pt-BR|zh-CN|es-US|ko-KR|fr-CA|pt-PT|fr-FR|ja-JP|es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A|tw|ug-CN|uk-UA|ur-IN|vi-VN|vo|xh-ZA|za|gu-IN|km-KH|zh-MO|or-IN|co-FR|de-LI|it|no|nl-BE|si-LK|de|pt|sq-AL|tz|nai|ti|ky|CHR|fil|nso-ZA|wo|fr-SN|ig|en-SG|ps-IR|to-TO|en-IN|ls|bs-BS-Cyrl|om-ET|hy-AM|dv-IN|fi-FI|hu-HU|es-DO|bs-BS-Latn|aa-ET|af-ZA|ar-BH|ar-EG|bn_IN|bn_BD|bi-VU|ar-AE|ar-YE"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="translationBody">
    <xs:annotation id="translationBodyDesc"><xs:documentation>This is the main body of the translation text. There are no allowed attributes or restrictions. There must be at least one translationUnit element.</xs:documentation></xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="translationJob" maxOccurs="1" minOccurs="1">
          <xs:complexType>
           <xs:sequence>
             <xs:element name="jobLanguages" maxOccurs="1" minOccurs="1">
               <xs:annotation>
                 <xs:documentation>This element encapsulates the languages for the project. All the languages listed here should match the languages indicated in each translationUnit.</xs:documentation>
               </xs:annotation>
               <xs:complexType>
                 <xs:sequence>
             <xs:element name="sourceLang" maxOccurs="1" minOccurs="1" nillable="false">
               <xs:annotation>
                 <xs:documentation>This element encapsulates the source language of the project, and should match the source language of each translationUnit. There can and must be only 1 of these elements</xs:documentation>
               </xs:annotation>
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="langString">
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="targetLang" maxOccurs="unbounded" minOccurs="1" nillable="false">
               <xs:annotation>
                 <xs:documentation>This element encapsulates the target languages of the project. Each translationUnit should have each of these target languages, no exceptions. There must be at least 1 targetLanguage specified, but there is no limit as to how many targetLanguages there can be. Each target language must be unique, and the targetLanguage should not equal the sourceLanguage.</xs:documentation>
               </xs:annotation>
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="langString">
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
                 </xs:sequence>
               </xs:complexType>
               <xs:unique name="uniqueJobLangs">
                 <xs:selector xpath="*"/>
                 <xs:field xpath="@langString"/>
               </xs:unique>
             </xs:element>
        <xs:element name="translationUnit" maxOccurs="unbounded" minOccurs="1">
          <xs:annotation id="translationUnitDesc"><xs:documentation>This is the main unit for translation and comprises language combination(s). There should be only one sourceSegment element, and at least one targetSegment element. The langLocale attribute on each targetSegment element must be unique. In other words, there can only be one targetSegment per language. Also, no targetSegment can have the same langLocale as the sourceSegment. If no translation exists yet, targetSegments should be left blank, but they MUST be included in the file</xs:documentation></xs:annotation>
          <xs:complexType>
            <xs:sequence>
              <xs:element name="sourceSegment" maxOccurs="1" minOccurs="1" nillable="false">
                <xs:annotation id="sourceSegmentDesc"><xs:documentation>Text for translation is placed in this element. Must be 1 per translationUnit element. Cannot be null, also cannot be less than 1 character long.</xs:documentation></xs:annotation>
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="sourceString">
                      <xs:attribute name="langLocale" use="required">
                        <xs:annotation id="langLocaleDesc"><xs:documentation>This is the language-locale combination for the source. All sourceString must have the same langLocale</xs:documentation></xs:annotation>
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:annotation id="langRestrictionDesc"><xs:documentation>The available options for language locale combos are limited certain values based on our API. Please see Glyph Best Practices API Documentation for more details.</xs:documentation></xs:annotation>
                            <xs:pattern value="da-DK|ms-MY|it-IT|pt-BR|zh-CN|es-US|ko-KR|fr-CA|pt-PT|fr-FR|ja-JP|es-ES|de-DE|en-US|ru-RU|es-419|en-GB|zh-TW|tr-TR|nl-NL|th-TH|az-AZ-Cyrl|az-AZ-Latn|ba-RU|eu-ES|hmv|br-FR|bg-BG|my-BU|cal|ca-ES|ceb|ch|ny-MW|am-ET|zh-HK|zh-SG|bn|fr|fu|hr-HR|cs-CZ|ht|prs-AF|en|bal-IR|en-AU|eo|en-BZ|en-CA|en-CB|en-IE|en-JM|en-NZ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|af-ZA|ar-BH|ar-EG|bn_IN|bn_BD|bi-VU|ar-AE|ar-YE"/>
                          </xs:restriction>
                        </xs:simpleType>
                      </xs:attribute>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="targetSegment" maxOccurs="unbounded" minOccurs="1" nillable="true">
                <xs:annotation id="targetSegmentDesc"><xs:documentation>This is the element that holds any translated text or is the place holder for translated text. It can be empty, but there must be at least one in each translationUnit. In other words, there must be at least one language combination in the file.</xs:documentation></xs:annotation>
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="langLocale" use="required">
                        <xs:annotation id="langLocaleDesc2"><xs:documentation>This is the language-locale combination for the source. All sourceString must have the same langLocale</xs:documentation></xs:annotation>
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:annotation id="langRestrictionDesc2"><xs:documentation>The available options for language locale combos are limited certain values based on our API. Please see Glyph Best Practices API Documentation for more details.</xs:documentation></xs:annotation>
                            <xs:pattern value="da-DK|ms-MY|it-IT|pt-BR|zh-CN|es-US|ko-KR|fr-CA|pt-PT|fr-FR|ja-JP|es-ES|de-DE|en-US|ru-RU|es-419|en-GB|zh-TW|tr-TR|nl-NL|th-TH|az-AZ-Cyrl|az-AZ-Latn|ba-RU|eu-ES|hmv|br-FR|bg-BG|my-BU|cal|ca-ES|ceb|ch|ny-MW|am-ET|zh-HK|zh-SG|bn|fr|fu|hr-HR|cs-CZ|ht|prs-AF|en|bal-IR|en-AU|eo|en-BZ|en-CA|en-CB|en-IE|en-JM|en-NZ|en-PH|en-ZA|en-CH|en-TT|en-ZW|et-EE|fo-FO|fa-IR|fj-FJ|sm-WS|fr-BE|nl|ar-DZ|mh|ar-AA|as-IN|ab|fy|fr-LU|ik|fr-MC|tet-ID|fr-CH|gd-GB|gl-ES|ka-GE|ar-IQ|ar-LY|ar-LB|ar-JO|ga-IE|lv-LV|ar-KW|he-IL|ar-MA|de-AT|sla-ME|de-LU|pau|de-CH|el-GR|kl-GL|grn|is-IS|es|ha-NG|hi-IN|id-ID|ia|ie|iu|zu-ZA|kk-KZ|jv-ID|kn-IN|ks|lo-LA|rw-RW|kg-CG|kok-IN|ku-TR|ln-CG|lt-LT|lb|mk-MK|mg-MG|mt-MT|mi-NZ|mn-MN|na-NR|nd-ZW|nb-NO|nn-NO|oc-FR|ps|pl-PL|qu-PE|ro-RO|rn|sg|sa-IN|sr-CY|sr-LA|sr-YU|st|sn|it-CH|ne-NP|pa-PA|ml-IN|yi|la|mo-MD|mr-IN|sk-SK|sl-SI|ar-OM|ar-QA|es-CU|ar-SA|apd-SD|ar-SY|sd|syr-TR|so-DJ|so-ET|tk-TM|so-KE|so-SO|dsb-DE|hsb-DE|es-AR|es-BO|es-CL|es-CO|es-CR|es-EC|es-SV|es-GT|es-HN|es-MX|es-NI|es-PA|es-PY|es-PE|es-PR|rm-CH|be-BY|so|sv|tt-RU|cy-GB|yi-IL|yo-NG|ta-IN|te-IN|ar-TN|bo|es-UY|es-VE|su|sw-KE|ss|sv-FI|sv-SE|tl-PH|tg-TJ|ts-ZA|tn-BW|uz-UZ-Latn|tn-ZA|tw|ug-CN|uk-UA|ur-IN|vi-VN|vo|xh-ZA|za|gu-IN|km-KH|zh-MO|or-IN|co-FR|de-LI|it|no|nl-BE|si-LK|de|pt|sq-AL|tz|nai|ti|ky|CHR|fil|nso-ZA|wo|fr-SN|ig|en-SG|ps-IR|to-TO|en-IN|ls|bs-BS-Cyrl|om-ET|hy-AM|dv-IN|fi-FI|hu-HU|es-DO|bs-BS-Latn|aa-ET|af-ZA|ar-BH|ar-EG|bn_IN|bn_BD|bi-VU|ar-AE|ar-YE"/>
                          </xs:restriction>
                        </xs:simpleType>
                      </xs:attribute>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="active" use="required">
              <xs:annotation id="activeDesc"><xs:documentation>This indicates whether this translationUnit is active. If you do not want this to be translated or processed, set to false. Default should be true</xs:documentation></xs:annotation>
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:enumeration value="true"/>
                  <xs:enumeration value="false"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
            <xs:attribute type="xs:string" name="key" use="required">
            <xs:annotation id="keyDesc"><xs:documentation>key is a required element. It should be a unique identifier for your string.</xs:documentation></xs:annotation>
            </xs:attribute>
              <xs:attribute type="xs:string" name="notes" use="required">
                <xs:annotation id="notesDesc"><xs:documentation>The notes attribute MUST be included, even if it is left blank (=""). Put any notes about context or information the translators should know. The notes will be presented to all langauges. At this time, we don't support langauge specific notes.</xs:documentation></xs:annotation>
              </xs:attribute>
            <xs:attribute name="lenRestriction" use="required">
              <xs:annotation id="lenRestrictionDesc"><xs:documentation>The notes attribute MUST be included, and cannot be left blank. The value should be 0 if no length restriction is desired. This attribute is used to tells the translator the maximum number of characters allowed for this string. If used, it must be an integer of 1 or greater. If used, the translator will be forced to validate the length or change their translation prior to delivery.</xs:documentation></xs:annotation>
              <xs:simpleType>
                <xs:restriction>
                  <xs:minInclusive value="0" />
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:complexType>
          <xs:unique name="uniqueLang">
            <xs:selector xpath="*"/>
            <xs:field xpath="@langLocale"/>
          </xs:unique>
          <xs:unique name="uniqueKey">
            <xs:selector xpath="*"></xs:selector>
            <xs:field xpath="@key"></xs:field>
          </xs:unique>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:0)

乍一看,它看起来好像可以用key / keyref完成。

在translationJob级别,使用selection = jobLanguages / sourceLang,field =“。”定义一个键,并使用选择translationUnit / sourceSegment,field = @ langLocale定义相应的keyref。

然后使用selection = jobLanguages / targetLang(field =“。”)定义第二个键,并使用selection = translationUnit / targetSegment,field = @ langLocale定义相应的keyRef。