【问题标题】:Two different xmlns:xs in XML SchemaXML Schema 中的两个不同的 xmlns:xs
【发布时间】:2013-09-20 04:50:20
【问题描述】:

这是我的问题: 我有一个包含一些值的 xml 文件,而且这个文件是证书签名的。 我的 xsd 架构文件知道如何仅处理值,而在添加签名行时架构失败。 错误是: 元素“标头”在命名空间“http://www.w3.org/2000/09/xmldsig#”中具有无效的子元素“签名”。预期“签名”的可能元素列表 这是我的代码, 谢谢:)

文件 test.xml:

<Header>
<tank>
    <code>1</code>
    <level>0</level>
</tank>

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
        ...
    <SignedInfo>
</Signature>
</Header>

SchemaTest.xsd:

<?xml version="1.0"?>
<xs:schema id="SchemaTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefalut="qualified"
attributeFormDefault="unqualified">
<xs:element name="Header">
    <xs:complexType>
    <xs:sequence>
        <xs:element name="tank">
        <xs:complexType>
        <xs:sequence>
            <xs:element name="code"/>
            <xs:element name="level"/>
            </xs:sequence>
        </xs:complexType>
        </xs:element>           
        <xs:element name="Signature">
                <xs:complexType>
                <xs:sequence>
                    <xs:element name="SignedInfo">
                    <xs:complexType>
                    <xs:sequence>
                    </xs:sequence>
                    </xs:complexType>
                    </xs:element>
                </xs:sequence>
                </xs:complexType>
        </xs:element>
        </xs:sequence>
        </xs:complexType>
</xs:element>
</xs:schema>

【问题讨论】:

    标签: xml xsd schema xml-namespaces


    【解决方案1】:

    您需要第二个架构来描述签名命名空间。以下代码为基于两个不同命名空间的 xml 文件构建了一个简单示例。

    这里是第二个命名空间模式 ...

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- second namespace schema -->
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://my.second.namespace" 
        xmlns:tns="http://my.second.namespace" 
        elementFormDefault="qualified">
        <element name="SomethingElse" type="string"></element>
    </schema>
    

    主命名空间的架构 ...

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- first namespace schema -->
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://my.first.namespace" xmlns:tns="http://my.first.namespace"
        xmlns:S="http://my.first.namespace"      
        xmlns:T="http://my.second.namespace"
            elementFormDefault="qualified">
        <import namespace="http://my.second.namespace"
            schemaLocation="./SecondNamespaceSchema.xsd"/>
        <element name="Root">
            <complexType>
                <sequence>
                    <element name="Head">
                        <complexType>
                            <sequence>
                                <element name="Something" type="string"></element>
                                <element ref="T:SomethingElse"/>
                            </sequence>
                        </complexType>
                    </element>
                    <element name="Body" type="string"></element>
                </sequence>
            </complexType>
        </element>
    </schema>
    

    ...和一个将所有内容放在一起的示例 xml。

    <?xml version="1.0"?>
    <!-- validatable xml file - proved with eclipse validator -->
    <S:Root xmlns:S="http://my.first.namespace"
        xmlns:T="http://my.second.namespace" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://my.first.namespace ./Sample.xsd">
       <S:Head>
            <S:Something/>
            <T:SomethingElse/>
       </S:Head>
       <S:Body>
       </S:Body>
    </S:Root>
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多