简体   繁体   中英

How to validate against schema for namespace, not complete document?

I have a document like this:

<d:block xmlns:d="D" xmlns:b="B" xmlns="default" name="popover">
  <d:description>...</d:description>
  <d:sample>
    <b:popover>
      ...some b:stuff...
    </b:popover>
   </d:sample>
</d:block>

Schema for this document looks like:

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
        <element name="block" ns="D">
            <attribute name="name"/>
            <element name="description">
                <text/>
            </element>
            <element name="sample">
                <ref name="anything"/>
            </element>
        </element>
    </start>
    <define name="anything">
        <element>
            <anyName>
                <except>
                    <nsName ns="D"/>
                </except>
            </anyName>
            <zeroOrMore>
                <choice>
                    <attribute>
                        <anyName/>
                    </attribute>
                    <text/>
                    <ref name="anything"/>
                </choice>
            </zeroOrMore>
        </element>
    </define>
</grammar>

"Anything" literally means anything but D-namespaced.

And I want to create another schema for B namespace to use it against any arbitrary XML containing B:namespace.

How to create Schema for the namespace, not for the complete document?

Can't get this.

A kind of schema like that should work. You just have to define the elements for B namespace in the define element named BElements .

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
      <ref name="anythingOrB"/>
    </start>
    <define name="anythingOrB">
        <choice>
            <ref name="BElements"/>
            <ref name="anythingExceptB"/>
        </choice>
    </define>
    <define name="anythingExceptB">
        <element>
            <anyName>
                <except>
                    <nsName ns="B"/>
                </except>
            </anyName>
            <zeroOrMore>
                <choice>
                    <attribute>
                        <anyName/>
                    </attribute>
                    <text/>
                    <ref name="anythingOrB"/>
                </choice>
            </zeroOrMore>
        </element>    
    </define>
    <define name="BElements">
        <choice>
            <element name="elt1" ns="B">
                <empty/>
            </element>
            <element name="elt2" ns="B">
                <empty/>
            </element>
        </choice> 
    </define>
</grammar>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM