如何在XSD中使用xs:any关键字?

huangapple go评论53阅读模式
英文:

How to use xs:any keyword in XSD?

问题

Here is the translated part of your text:

我想要将游戏数据文件使用XML和XSD。 它必须是可扩展的。

xml:

```xml
<?xml version="1.0" encoding="utf-8"?>
<enemy xmlns="rubtub2314@gmail.com/Enemy_Data.xml"
      xmlns:schema="rubtub2314@gmail.com/Enemy_Data.xml">
  <info id="0" name="warrior"> 
    <atk>1</atk>
    <def>1</def>
    <hp>1</hp>
    <spd>1</spd>
    <anti_magic>1</anti_magic>
  </info>
  <info id="1" name="archer">
    <atk>1</atk>
    <def>1</def>
    <hp>1</hp>
    <spd>1</spd>
  </info>
</enemy>

xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="rubtub2314@gmail.com/Enemy_Data.xsd"
            targetNamespace="rubtub2314@gmail.com/Enemy_Data.xml"
            elementFormDefault="qualified"> 
  <xs:element name="enemy">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="info">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="atk" type="xs:positiveInteger"/>
              <xs:element name="def" type="xs:positiveInteger"/>
              <xs:element name="hp" type="xs:positiveInteger"/>
              <xs:element name="spd" type="xs:positiveInteger"/>
              <xs:any minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="id" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

For the issue you're facing, the error message you mentioned, "<'rubtub2314@gmail.com/Enemy_Data.xml:anti_magic'> element is not declared," is likely due to the use of the <xs:any> element in your XSD, which allows for elements from any namespace to be included under the <info> element. To make it work as expected, you may need to specify the expected namespace or namespace wildcard in your XSD.

If you want to create an extensible XML, consider defining additional elements within the XSD and referencing them in your XML document to ensure they are valid and well-formed.

英文:

I want to make the game data file use XML and XSD.
It must be extensible.

xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;enemy xmlns=&quot;rubtub2314@gmail.com/Enemy_Data.xml&quot;
      xmlns:schema=&quot;rubtub2314@gmail.com/Enemy_Data.xml&quot;&gt;
  &lt;info id=&quot;0&quot; name=&quot;warrior&quot;&gt; 
    &lt;atk&gt;1&lt;/atk&gt;
    &lt;def&gt;1&lt;/def&gt;
    &lt;hp&gt;1&lt;/hp&gt;
    &lt;spd&gt;1&lt;/spd&gt;
    **&lt;anti_magic&gt;1&lt;/anti_magic&gt;**
  &lt;/info&gt;
  &lt;info id=&quot;1&quot; name=&quot;archer&quot;&gt;
    &lt;atk&gt;1&lt;/atk&gt;
    &lt;def&gt;1&lt;/def&gt;
    &lt;hp&gt;1&lt;/hp&gt;
    &lt;spd&gt;1&lt;/spd&gt;
  &lt;/info&gt;
&lt;/enemy&gt;

xsd :

&lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
            xmlns=&quot;rubtub2314@gmail.com/Enemy_Data.xsd&quot;
            targetNamespace=&quot;rubtub2314@gmail.com/Enemy_Data.xml&quot;
            elementFormDefault=&quot;qualified&quot;&gt; 
  &lt;xs:element name=&quot;enemy&quot;&gt;
    &lt;xs:complexType&gt;
      &lt;xs:sequence maxOccurs=&quot;unbounded&quot;&gt;
        &lt;xs:element name=&quot;info&quot;&gt;
          &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
              &lt;xs:element name=&quot;atk&quot; type=&quot;xs:positiveInteger&quot;/&gt;
              &lt;xs:element name=&quot;def&quot; type=&quot;xs:positiveInteger&quot;/&gt;
              &lt;xs:element name=&quot;hp&quot; type=&quot;xs:positiveInteger&quot;/&gt;
              &lt;xs:element name=&quot;spd&quot; type=&quot;xs:positiveInteger&quot;/&gt;
              **&lt;xs:any minOccurs=&quot;0&quot;/&gt;**
            &lt;/xs:sequence&gt;
            &lt;xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/&gt;
            &lt;xs:attribute name=&quot;id&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
&lt;/xs:schema&gt;

But my any keyword does not work. It make Just one line error message

> 'rubtub2314@gmail.com/Enemy_Data.xml:anti_magic' element does not declared. (<- it maybe not correct translation)

Why do I get this error?

I want to create an extensible XML that can add other parameters under the info element.

答案1

得分: 0

请查看 xs:any 的定义

{process contents} 控制通配符所允许的信息项对评估的影响,如下所示:

  • strict
    必须有该项的顶层声明可用,或该项必须具有 xsi:type,并且该项必须适当地有效。
  • skip
    没有任何约束:该项必须只是良好格式的 XML。
  • lax
    如果该项具有唯一确定的声明可用,它必须相对于该定义有效,即,如果可以,请验证,如果不能,请不要担心。

因此,在以下行中使用 "skip" 或 "lax" 作为参数:

<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
英文:

Have a look at the definition of xs:any:

> {process contents} controls the impact on ·assessment· of the information items allowed by wildcards, as follows:
>
> - strict
There must be a top-level declaration for the item available, or the item must have an xsi:type, and the item must be ·valid· as appropriate.
> - skip
No constraints at all: the item must simply be well-formed XML.
> - lax
If the item has a uniquely determined declaration available, it must be ·valid· with respect to that definition, that is, ·validate· if you can, don't worry if you can't.

So use "skip" or "lax" as a parameter in the following line:

&lt;xs:any processContents=&quot;skip&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/&gt;

huangapple
  • 本文由 发表于 2023年6月8日 01:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定