2012-04-14 2 views
1

xsd에서 특정 유형의 요소 중 일부 요소 만 나타나야하지만 그 요소 이름은 지정할 수 있습니까?요소가 XML 스키마의 특정 유형이지만 임의의 이름이어야합니다.

<xs:element type="myType"/> 

이 할 수있는 방법이 있나요 : 비주얼 스튜디오하지 날과 같이 '이름'속성을 생략 할 수 있습니다? 그렇지 않은 경우 RELAX NG와 같은 다른 XML 스키마 언어에서도 가능합니까? 감사합니다.

답변

2

XSD 1.0의 한 가지 방법은 대체 그룹을 사용하는 것입니다.

BaseAny.xsd

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="anyOfCType" type="myCType" abstract="true"/> 
    <xsd:complexType name="myCType"> 
     <xsd:sequence> 
      <xsd:element name="something" type="xsd:string"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:element name="anyOfSType" type="mySType" abstract="true"/> 
    <xsd:simpleType name="mySType"> 
     <xsd:restriction base="xsd:string"> 
      <xsd:minLength value="2"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:element name="root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="anyOfCType"/> 
       <xsd:element ref="anyOfSType"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

RefBaseAny.xsd :

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/1/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/1/XMLSchema.xsd" xmlns:b="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="BaseAny.xsd"/> 

    <xsd:element name="c.one" type="b:myCType" substitutionGroup="b:anyOfCType"/> 
    <xsd:element name="s.one" type="b:mySType" substitutionGroup="b:anyOfSType"/> 

</xsd:schema> 

샘플 XML RefBaseAny.xsd에 대해 유효성을 검사 :

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:r="http://tempuri.org/1/XMLSchema.xsd"> 
    <r:c.one> 
     <something></something> 
    </r:c.one> 
    <r:s.one>12</r:s.one> 
</root> 

을 당신은 정의해야합니다 NG 긴장의 (xsd : complexType을 모방합니다).

<element> 
    <anyName/> 
    <ref name="myType"/> 
</element> 

Test.rng :

<?xml version="1.0" encoding="UTF-8"?> 
<grammar xmlns="http://relaxng.org/ns/structure/1.0"> 
    <start> 
     <element> 
      <anyName/> 
      <ref name="myType"/> 
     </element> 
    </start> 
    <define name="myType"> 
     <element name="c.one"> 
      <interleave> 
       <attribute name="id"> 
        <value>AA</value> 
       </attribute> 
       <attribute name="ref"> 
        <text/> 
       </attribute> 
      </interleave> 
      <element name="s.two"> 
       <text/> 
      </element> 
     </element> 
    </define> 
</grammar> 

유효한 XML1 :

<?xml version="1.0" encoding="UTF-8"?> 
<Data> 
    <c.one id="AA" ref="123"> 
     <s.two>Hello</s.two> 
    </c.one> 
</Data> 

유효한 XML2 :

다음

<define name="myType"> 
    ... 
</define> 

이 구조를 가진 요소를 정의

<?xml version="1.0" encoding="UTF-8"?> 
<Data1> 
    <c.one id="AA" ref="123"> 
     <s.two>Hello</s.two> 
    </c.one> 
</Data1> 
+0

RELAX NG 솔루션은 내가 필요한 제품입니다. 그러나 XSD에서 어떤 이름을 부여 할 수 없습니까? XSD가 매우 제한적이라는 것을 알았습니다. 그들은 XSD의 구현자를 먼저 생각하고 사용자는 생각하지 않았습니다 ... – RoadBump

+0

@RoadBump, XSD 1.0에서는 임의의 이름이 와일드 카드 형식으로 지원됩니다 (xsd : any 요소, xsd : anyAttribute 특성). 그러나 자격이 부여 된 네임 스페이스와 관련된 제한 사항과 유효성 확인을위한 처리 모델은 사용자가 요청한 유형과 관련이 없습니다. 최근에 승인 된 XSD 1.1의 새로운 사양이 있습니다. 모든 사람들이 사용할 수있는 싼 파서가 없기 때문에 이번에는 언급하지 않겠습니다. –

+0

@PetruGardea 당신이 이것을 확인할 수 있습니까? http://stackoverflow.com/questions/37851057/xsd-for-no -attributes-and-no-self-closing 제발 – user960567

관련 문제