开发者

Combining HTML and XML from one schema all nested within an element of yet another schema

开发者 https://www.devze.com 2022-12-13 20:15 出处:网络
I have been working on learning XML Schema for the past few years now, off and on. I have a pretty good handle on the basics but one thing still eludes me:

I have been working on learning XML Schema for the past few years now, off and on. I have a pretty good handle on the basics but one thing still eludes me:

I want to be able to create an XML document similar to the following:

  <itemList xmlns="http://mydomain.com/namespaceA">
     <item>
        <html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:nsB="http://mydomain.com/namespaceB">
           <body>
              <p>Any HTML code here but I want to be able to mark up
              <nsB:myBTag><strong>some</strong> of the text</nsB:myBTag>
              with tags from namespaceB even if those tags are nested within
              standard HTML tags and even if there are then more HTML tags 
              nested within my namespaceB tags.</p>
           </body>
        </html>
     </item>
  </itemList>

Note that inside the <html> element the xhtml namespace is the default namespace. This is because I want document authors to be able to use a standard HTML editor and then simply insert the special namespaceB tags where they need them. There will be far more XHTML tags than namespaceB tags in any one instance document.

So, from what I have tentatively learned so far, I think my two schemas will need to look something like this:

namespaceA

  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:nsA="http://mydomain.com/namespaceA"
             targetNamespace="http://mydomain.com/namespaceA">
      <xs:element name="itemList">
          <xs:complexType>
              <xs:sequence>
                  <xs:element name="item" maxOccurs="unbounded" minOccurs="1">
                      <xs:complexType>
                          <xs:any namespace="http://www.w3.org/1999/xhtml"
                                  minOccurs="1" maxOccurs="1"
                                  processContents="strict">
                      </xs:complexType>
                  </xs:element>
              </xs:sequence>
          </xs:complexType>
      </xs:element>
  </xs:schema>

(I think the only namespace I need to declare for the content model within the <item> tag is the XHTML namespace because then the <html> tag in the instance document declares the namespaceB namespace, but I am in no way positive.)

namespaceB

  <?xml v开发者_如何学JAVAersion="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:nsB="http://mydomain.com/namespaceB"
             targetNamespace="http://mydomain.com/namespaceB">
      <xs:element name="myBTag">
          <!--  I am clueless here -->
          <!-- I have no idea how to make sure that additional HTML tags
           can go in here without screwing everything up -->
      </xs:element>
  </xs:schema>

The big question is: Do I need to do anything to make sure that XHTML and namespaceB tags can be freely intermixed or is that just part and partial of the operation of the <xs:any> tag?

Naturally, my schemas and documents will be far more complicated than this. I have simplified them down for easy discussion. Thanks in advance for any help you can provide. If I can get over this one hurdle I can create a really powerful system for educational content that will help educate the whole world for free.


According to the XS spec, the <xs:any> element can have a namespace attribute which can have the value ##any - this means that the corresponding element in the document can have any namespace.

The namespace attribute being absent has the same effect - so you can just omit it. i.e. the answer to the "big question" is:

  • Do I need to do anything to make sure that XHTML and namespaceB tags can be freely intermixed? No, you don't

  • Or is that just part and partial of the operation of the tag? Yes, it is

Disclaimer: I haven't tested this, only read the spec above. If I were you, I'd try validating some example XML documents with your simplified XSDs above, using a popular XSD validator, to determine under what conditions it validates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号