以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  指定XSD验证XML时:属性不让出现在某元素中  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=89575)


--  作者:XStream
--  发布时间:3/7/2011 5:32:00 PM

--  指定XSD验证XML时:属性不让出现在某元素中
各位路过的大仙~~

刚学XML,我用XSD定义了一个格式~~~来显示产品~~
test.xsd
[CODE=xml]
<?xml version="1.0" encoding="UTF-8"?>
<pm:schema xmlns:pm="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.example.org/test"
xmlns="http://www.example.org/test"
elementFormDefault="qualified">
    <pm:element name="ProductShow">
        <pm:complexType mixed="true">
            <pm:sequence>
                <pm:element name="pClassId" type="simpleElement" />
                <pm:element name="showName" type="simpleElement" />
                <pm:element name="animation" type="MyAnimate"/>
                <!--start productFamily  #####################-->
                <pm:element name="productFamily">
                    <pm:complexType mixed="true">
                    <pm:sequence>
                    <!--start perProduct *******************-->
                    <pm:element name="perProduct" minOccurs="1" maxOccurs="unbounded">
                        <pm:complexType mixed="true">
                        <pm:sequence>
                            <pm:element name="pId" type="simpleElement" />
                            <pm:element name="pFileName" type="simpleElement" />
                            <pm:element name="pFileSrc" type="simpleElement" />
                            <pm:element name="description" type="simpleElement" />
                        </pm:sequence>
                        <pm:attribute name="perPOrder" type="pm:integer" use="required" />
                        </pm:complexType>
                    </pm:element>
                    <!--over perProduct ********************-->
                    </pm:sequence>
                    </pm:complexType>
                </pm:element>
                <!--over productFamily #####################-->
                <pm:element name="downloadSrc" type="simpleElement" />
                <pm:element name="remarks" type="simpleElement" />
                <!--Support for custom extensions,format"name=value" -->
                <pm:element name="otherGroups">
                <pm:complexType>
                <pm:sequence>
                <pm:element name="otherElement" minOccurs="0" maxOccurs="unbounded">
                    <pm:complexType>
                    <pm:sequence>
                        <pm:element name="key" type="pm:string" />
                        <pm:element name="value" type="pm:string" />
                    </pm:sequence>
                    </pm:complexType>
                </pm:element>
                </pm:sequence>
                </pm:complexType>
                </pm:element>
                <!-- over custom extensions -->
            </pm:sequence>
        </pm:complexType>
    </pm:element>
    <!-- ++++++++start define attribute and others+++++++ -->
    <pm:simpleType name="MyAnimate">
        <pm:restriction base="pm:string">
            <pm:enumeration value="是" />
            <pm:enumeration value="否" />
        </pm:restriction>
    </pm:simpleType>
    <pm:complexType name="simpleElement">
        <pm:sequence>
            <pm:element name="value" type="pm:string" />
            <pm:element name="desc" type="pm:string" default="" />
        </pm:sequence>
    </pm:complexType>    
</pm:schema>


[/CODE]

然后根据这个手敲了一个XML——product1.xml
[CODE=xml]
<?xml version="1.0" encoding="UTF-8"?>
<ProductShow xmlns="http://www.example.org/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/test test.xsd">
<pClassId>
    <value>1234</value>
    <desc>the product id</desc>
</pClassId>
<showName >
    <value>XXX产品图</value>
    <desc>the name displayed on the web of product</desc>
</showName>
<animation>是</animation>
<productFamily>
    <perProduct perPOrder="1">
        <pid></pid>
        <pFileName>1.png</pFileName>    
        <pFileSrc>./test</pFileSrc>
        <description></description>
    </perProduct>
    <perProduct perPOrder="2">
        <pid></pid>
        <pFileName>2.png</pFileName>    
        <pFileSrc>./test</pFileSrc>
        <description></description>
    </perProduct>
    <perProduct perPOrder="3">
        <pid></pid>
        <pFileName>3.png</pFileName>    
        <pFileSrc>./test</pFileSrc>
        <description></description>
    </perProduct>
    <perProduct perPOrder="4">
        <pid></pid>
        <pFileName>4.png</pFileName>    
        <pFileSrc>./test</pFileSrc>
        <description></description>
    </perProduct>
    <perProduct perPOrder="5">
        <pid></pid>
        <pFileName>5.png</pFileName>    
        <pFileSrc>./test</pFileSrc>
        <description></description>
    </perProduct>
</productFamily>
<downloadSrc >
    <value></value>
    <desc>the full URL of data to product for download</desc>
</downloadSrc>
<remarks>
    <value></value>
    <desc>the remarks of product</desc>
</remarks>
<others>
<!-- "the extension for others,can contain other elements" -->
</others>
</ProductShow>
[/CODE]

结果,我在验证我的XML是否符合XSD的时候~~会报错·~如下
[CODE=java]
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'pid'. One of '{"http://www.example.org/CIPAS_Products_XML_SCH":pId}' is expected.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1777)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:144)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:107)
 at javax.xml.validation.Validator.validate(Validator.java:127)
 at iproduct.ValidateXML.validateXML(ValidateXML.java:38)
 at iproduct.ValidateXML.main(ValidateXML.java:53)
[/CODE]

说不能将order属性加入到该元素·~~囧~~这事为什么啊? 求解决办法·~~
另,附上我用来验证的java方法:
[CODE=java]
import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidateXML {
     
     public boolean validateXML(String xsdpath,String xmlpath) throws SAXException,IOException{
        //建立schema工厂
        SchemaFactory schemaFactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        //建立验证文档文件对象,利用此文件对象所封装的文件进行schema验证
        File schemaFile=new File(xsdpath);
        //利用schema工厂,接收验证文档文件对象生成Schema对象
        Schema schema=schemaFactory.newSchema(schemaFile);
        //通过Schema产生针对于此Schema的验证器,利用schenaFile进行验证
        Validator validator=schema.newValidator();
        //得到验证的数据源
        Source source=new StreamSource(xmlpath);
        //开始验证,成功输出success!!!,失败输出fail
        try{
         
         validator.validate(source);
         
        }catch(Exception ex){
         
         ex.printStackTrace();
         
        }
              return true;
      }
      
     public static void main(String[] args) {
        String xmlDir="product1.xml";
        String xsdDir="test.xsd";
        boolean out;
        try {
            out=new ValidateXML().validateXML(xsdDir, xmlDir);
            if (out) {
                System.out.println("the 'product1.xml' is format to 'test.xsd'!");
            }else {
                System.err.println("it's so pity! something was wrong!");
            }
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
     
    }

[/CODE]


[此贴子已经被作者于2011-3-7 18:58:08编辑过]

--  作者:song13243546
--  发布时间:11/29/2011 2:26:00 PM

--  
高人在哪里
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
95.703ms