How can I define an XML schema element that allows either base64 content or an xop:Include element?

StackOverflow https://stackoverflow.com/questions/20909580

  •  24-09-2022
  •  | 
  •  

Question

I have a XML schema that defines an element that may be either base64 text or an xop:Include element. Currently, this is defined as a base64Binary type:

<xs:element name="PackageBinary" type="xs:base64Binary" minOccurs="1" maxOccurs="1"/>

When I insert the xop:Include element instead, it looks like this:

<PackageBinary>
    <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="http://google.com/data.bin" />
</PackageBinary>

But this gives an XML validation error (I'm using .NET validator):

The element 'mds:xml-schema:soap11:PackageBinary' cannot contain child element 'http://www.w3.org/2004/08/xop/include:Include' because the parent element's content model is text only.

This makes sense because it's not base64 content, but I thought this was common practice...? Is there any way to support this in the schema? (We have existing product that supports this syntax but we are adding validation now.)

No correct solution

OTHER TIPS

The best I could come up with was to create a complex type that allowed any tags but was also tagged as "mixed" so it allowed text. This doesn't explicitly declare the content as base64, but it does let it pass validation.

<xs:complexType name="PackageBinaryInner" mixed="true">
  <xs:sequence>
    <xs:any minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="PackageBinary" type="PackageBinaryInner" minOccurs="1" maxOccurs="1"/>

The solution I've found is like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.org"
           elementFormDefault="qualified"
           xmlns="http://example.org" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:xop="http://www.w3.org/2004/08/xop/include">

<xs:import namespace="http://www.w3.org/2004/08/xop/include"
           schemaLocation="http://www.w3.org/2004/08/xop/include"/>

<xs:complexType name="PackageBinary" mixed="true">
  <xs:all>
    <xs:element ref="xop:Include"/>
  </xs:all>
</xs:complexType>

I saw this in an xml document that appeared to allow validation - basically the attribute xmlns:xop="..." did the trick:

<SomeElement xmlns:xop="http://www.w3.org/2004/08/xop/include/" id="465390" type="html">
<SomeElementSummaryURL>https://file.someurl.com/SomeImage.html</SomeElementSummaryURL>
<xop:Include href="cid:1111111@someurl.com"/>
</SomeElement >
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top