2012-04-03 86 views
0

对不起,我的英语。如何指定没有特定名称的元素

我需要为我的XML文件编写XML模式。我的XML文件样本:

<?xml version="1.0" encoding="utf-8"?> 
<styles> 

    <style name="p"> 
    <text-indent>25px</text-indent> 
    </style> 

    <style name="td"> 
    <border>solid 2px</border> 
    <border-color>Black</border-color> 
    <padding-left>5px</padding-left> 
    </style> 

    <style name="p.withoutRedLine"> 
    <text-indent>0px</text-indent> 
    </style> 

</styles> 

每个“风格”元素可以有任何名的项目。

我写的下一个XML模式(问题在代码中的注释中定义):

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema 
    xmlns="http://www.mia-orbis.com/2012/XMLSchema/styles" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

    <xsd:complexType name="style"> 
    <xsd:sequence minOccurs="1" maxOccurs="1"> 
     <!--Error in it place (I don't need to specify value 
     of attribute 'name', but from me it demand):--> 
     <xsd:element type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>  
    </xsd:sequence> 
    <xsd:attribute name="name" type="xsd:ID" use="required"/> 
    </xsd:complexType> 

    <xsd:element name="styles"> 
    <xsd:complexType> 
     <xsd:sequence minOccurs="1" maxOccurs="1"> 
     <xsd:element name="style" minOccurs="0" maxOccurs="unbounded"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 

</xsd:schema> 

问候

回答

0

您需要使用的xsd:any:对

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:complexType name="style"> 
     <xsd:sequence> 
      <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="skip"/> 
     </xsd:sequence> 
     <xsd:attribute name="name" type="xsd:ID" use="required"/> 
    </xsd:complexType> 
    <xsd:element name="styles"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="style" minOccurs="0" maxOccurs="unbounded" type="style"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

我做了一些修正,以你的模式:删除默认名称空间,将类型设置为样式元素等。

如果你同意使用xsd:any,这可能是一种很好的习惯来定义你所知道的,并将xsd:any留给其他任何东西。

+0

这也不行。错误行: 2012-04-05 12:56:49

+0

我无法理解您的代码感 – 2012-04-05 13:05:06

+0

@Bush,我发布的XSD是完全有效;你用什么工具来验证它? – 2012-04-05 14:04:18

相关问题