新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> DTD, XML Schema(XMLS), RELAX NG
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DTD/XML Schema 』 → 指定XSD验证XML时:属性不让出现在某元素中 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 13025 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 指定XSD验证XML时:属性不让出现在某元素中 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     XStream 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:2
      积分:56
      门派:XML.ORG.CN
      注册:2011/3/7

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给XStream发送一个短消息 把XStream加入好友 查看XStream的个人资料 搜索XStream在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看XStream的博客楼主
    发贴心情 指定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编辑过]

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2011/3/7 17:32:00
     
     song13243546 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:8
      积分:84
      门派:XML.ORG.CN
      注册:2011/11/29

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给song13243546发送一个短消息 把song13243546加入好友 查看song13243546的个人资料 搜索song13243546在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看song13243546的博客2
    发贴心情 
    高人在哪里

    ----------------------------------------------
    www.532baby.com青岛静康不孕不育医院

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2011/11/29 14:26:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DTD/XML Schema 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/21 14:07:14

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    5,398.438ms