我的XSD生成器的XSD出了什么问题?

时间:2015-11-16 15:01:01

标签: java xml xsd

我正在为我的Java代码创建一个XSD生成器,以便在调用时动态运行。它只在需要时生成XSD。它使用sax解析器并生成以下XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.google.com/types/evt/SendCCToAlternateAddr/v1" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:NS1="SendCCToAlternateAddrEvtType">
<xsd:element name="SendCCToAlternateAddrEvtType">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CommonAttributes" minOccurs="0" maxOccurs="999">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EventName" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="EventVer" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="BusTransEffDt" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="BusTransRecTS" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PubSystemNm" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="EventType" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="EventSubType" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="OrgUnitNm" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="Channel" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="PmtCardBasic" minOccurs="0" maxOccurs="999">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="PartyPDId" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PartyIdTypeCd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="ArrangementId" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="MajBusUnitCd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PrdSvcLinBusCd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PaymentCardDetail" minOccurs="0" maxOccurs="999">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MaskedCardNr" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CardTypeDc" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="AlternateAddressInd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CreatedByPartyPDId" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CreatedByPartyIdTypeCd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PaymentCardEncryptedNr" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="PaymentCardDetail" minOccurs="0" maxOccurs="999">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MaskedCardNr" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CardTypeDc" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="AlternateAddressInd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CreatedByPartyPDId" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
<xsd:element name="CreatedByPartyIdTypeCd" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ServiceTypeDc" minOccurs="0" maxOccurs="999" type="xsd:normalizedString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<NS1:SendCCToAlternateAddrEvtType xmlns:NS1="http://www.google.com/types/evt/SendCCToAlternateAddr/v1">
<CommonAttributes>
    <EventName>sendCCToAlternateAddr</EventName>
    <EventVer>1.0</EventVer>
    <BusTransEffDt>2015-10-24</BusTransEffDt>
    <BusTransRecTS>2015-10-24T13:12:02.276-06:00</BusTransRecTS>
    <PubSystemNm>UpdateServices</PubSystemNm>
    <EventType>*</EventType>
    <EventSubType>*</EventSubType>
    <OrgUnitNm>yioyiu7y</OrgUnitNm>
    <Channel>employee</Channel>
</CommonAttributes>
<PmtCardBasic>
    <PartyPDId>34221717</PartyPDId>
    <PartyIdTypeCd>*</PartyIdTypeCd>
    <ArrangementId>XXXX-XXXX-XXXX-</ArrangementId>
    <MajBusUnitCd>BNK</MajBusUnitCd>
    <PrdSvcLinBusCd>DMD</PrdSvcLinBusCd>
    <PaymentCardDetail>
        <MaskedCardNr>XXXX-XXXX-XXXX-</MaskedCardNr>
        <CardTypeDc>MasterCard</CardTypeDc>
        <AlternateAddressInd>Y</AlternateAddressInd>
        <CreatedByPartyPDId>2341223</CreatedByPartyPDId>
        <CreatedByPartyIdTypeCd>EMPE</CreatedByPartyIdTypeCd>
        <PaymentCardEncryptedNr>999493076694449</PaymentCardEncryptedNr>
            <PaymentCardDetail>
                <MaskedCardNr>XXXX-XXXX-XXXX-</MaskedCardNr>
                <CardTypeDc>MasterCard</CardTypeDc>
                <AlternateAddressInd>Y</AlternateAddressInd>
                <CreatedByPartyPDId>2341223</CreatedByPartyPDId>
                <CreatedByPartyIdTypeCd>EMPE</CreatedByPartyIdTypeCd>
            </PaymentCardDetail>
    </PaymentCardDetail>
</PmtCardBasic>
<ServiceTypeDc>sendCCToAlternateAddr</ServiceTypeDc>
</NS1:SendCCToAlternateAddrEvtType>

我已经查看了在线生成器以验证我的XSD,但我的XSD似乎在使用JAVA进行验证方面比其他第三方生成的XSD更进一步。

我收到错误:

ERROR : cvc-complex-type.2.4.a: Invalid content was found starting with element 'CommonAttributes'. One of '{"http://www.google.com/types/evt/SendCCToAlternateAddr/v1":CommonAttributes, "http://www.google.com/types/evt/SendCCToAlternateAddr/v1":PmtCardBasic, "http://www.google.com/types/evt/SendCCToAlternateAddr/v1":ServiceTypeDc}' is expected.

有关如何使此XSD有效的任何帮助都将是一个很大的帮助。

2 个答案:

答案 0 :(得分:0)

您没有正确使用命名空间。我建议你查看下面的例子并以类似的方式实现你的...我试着在各处验证我的,它就像魅力一样。

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
attributeFormDefault="qualified" 
elementFormDefault="qualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://myNamespaceUrl/taxonomie/1"
targetNamespace="http://myNamespaceUrl/taxonomie/1"
xmlns="http://myNamespaceUrl/taxonomie/1">  
    <xs:element name="taxonomie">
    ...
    </xs:element>
</xs:schema>

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taxonomie xmlns="http://bva.sozvers.at/lava/taxonomie/1">
    <taxon>
    ...
    </taxon>
</taxonomie>

答案 1 :(得分:0)

您应该定义您的命名空间并正确地装饰您的XML元素,根据您在那里的内容判断,您已定义了命名空间,但它不是XSD正在寻找的命名空间。以下内容适用于您:

<?xml version="1.0" encoding="UTF-8"?>
<NS1:SendCCToAlternateAddrEvtType xmlns:NS1="http://www.usaa.com/types/evt/SendCCToAlternateAddr/v1">
    <NS1:CommonAttributes>
        <NS1:EventName>sendCCToAlternateAddr</NS1:EventName>
        <NS1:EventVer>1.0</NS1:EventVer>
        <NS1:BusTransEffDt>2015-10-24</NS1:BusTransEffDt>
        <NS1:BusTransRecTS>2015-10-24T13:12:02.276-06:00</NS1:BusTransRecTS>
        <NS1:PubSystemNm>UpdateServices</NS1:PubSystemNm>
        <NS1:EventType>*</NS1:EventType>
        <NS1:EventSubType>*</NS1:EventSubType>
        <NS1:OrgUnitNm>yioyiu7y</NS1:OrgUnitNm>
        <NS1:Channel>employee</NS1:Channel>
    </NS1:CommonAttributes>
    <NS1:PmtCardBasic>
        <NS1:PartyPDId>34221717</NS1:PartyPDId>
        <NS1:PartyIdTypeCd>*</NS1:PartyIdTypeCd>
        <NS1:ArrangementId>XXXX-XXXX-XXXX-</NS1:ArrangementId>
        <NS1:MajBusUnitCd>BNK</NS1:MajBusUnitCd>
        <NS1:PrdSvcLinBusCd>DMD</NS1:PrdSvcLinBusCd>
        <NS1:PaymentCardDetail>
            <NS1:MaskedCardNr>XXXX-XXXX-XXXX-</NS1:MaskedCardNr>
            <NS1:CardTypeDc>MasterCard</NS1:CardTypeDc>
            <NS1:AlternateAddressInd>Y</NS1:AlternateAddressInd>
            <NS1:CreatedByPartyPDId>2341223</NS1:CreatedByPartyPDId>
            <NS1:CreatedByPartyIdTypeCd>EMPE</NS1:CreatedByPartyIdTypeCd>
            <NS1:PaymentCardEncryptedNr>999493076694449</NS1:PaymentCardEncryptedNr>
            <NS1:PaymentCardDetail>
                <NS1:MaskedCardNr>XXXX-XXXX-XXXX-</NS1:MaskedCardNr>
                <NS1:CardTypeDc>MasterCard</NS1:CardTypeDc>
                <NS1:AlternateAddressInd>Y</NS1:AlternateAddressInd>
                <NS1:CreatedByPartyPDId>2341223</NS1:CreatedByPartyPDId>
                <NS1:CreatedByPartyIdTypeCd>EMPE</NS1:CreatedByPartyIdTypeCd>
            </NS1:PaymentCardDetail>
        </NS1:PaymentCardDetail>
    </NS1:PmtCardBasic>
    <NS1:ServiceTypeDc>sendCCToAlternateAddr</NS1:ServiceTypeDc>
</NS1:SendCCToAlternateAddrEvtType>

请注意更改:http://www.google.com/types/evt/SendCCToAlternateAddr/v1http://www.usaa.com/types/evt/SendCCToAlternateAddr/v1。我还装饰了每一个元素。你可以解决这个问题,但我发现更好的做法是明确说明每个元素使用的命名空间,因为可以在单个XML中使用多个命名空间,而且大多数XML都是由代码生成的,所以它不需要任何额外的时间真的。

我注意到的一件事是,您上面提供的XSD中包含http://www.google.com/types/evt/SendCCToAlternateAddr/v1命名空间,但是,您报告的错误表明它正在寻找http://www.usaa.com/types/evt/SendCCToAlternateAddr/v1,某处您有不匹配的地方至于预期的命名空间是什么。在更改代码中的命名空间之前,您应首先查看它。