无法使用 XJC 仅限定 xsd 属性
unable to qualify only xsd attribute using XJC
我需要像这样生成我的 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<instructions>
<instruction i:type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
请注意 "type" 属性由 "i" 限定。
我将 xsd 定义为:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="advice_request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instructions" type="Instructions" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="type" id="type" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
XJC生成如下包-info.java:
@XmlSchema(namespace = "urn:xyz.com:bf:api:core:v1", elementFormDefault = QUALIFIED)
然而,在编组对象时,我总是得到 XML 作为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1">
<instructions>
<instruction type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
如何获得符合"i"条件的属性?非常感谢任何帮助。
我已经尝试了很多事情,比如导入 XMLSchema-instance 命名空间,在 marshaller 上显式设置 SCHEMA_LOCATION(虽然这对我来说可能不是一个选项)但仍然没有...
提前致谢!
您应该为 complexType
使用层次结构。 i:type="advice_instruction_adjust"
表示有一个complextType
叫advice_instruction_adjust
.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="advice_instruction_adjust" type="advice_instruction_adjust"/>
<xsd:complexType name="advice_instruction_adjust">
<xsd:complexContent>
<xsd:extension base="Instruction">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Instructions" type="Instructions"/>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction" abstract="true">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
这里是XML
的例子
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Instructions xmlns:ns1="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ns1:instruction i:type="advice_instruction_adjust">
<ns1:method />
<ns1:quantity/>
<ns1:site_guid/>
</ns1:instruction>
</ns1:Instructions>
我需要像这样生成我的 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<instructions>
<instruction i:type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
请注意 "type" 属性由 "i" 限定。
我将 xsd 定义为:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="advice_request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instructions" type="Instructions" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="type" id="type" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
XJC生成如下包-info.java:
@XmlSchema(namespace = "urn:xyz.com:bf:api:core:v1", elementFormDefault = QUALIFIED)
然而,在编组对象时,我总是得到 XML 作为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1">
<instructions>
<instruction type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
如何获得符合"i"条件的属性?非常感谢任何帮助。
我已经尝试了很多事情,比如导入 XMLSchema-instance 命名空间,在 marshaller 上显式设置 SCHEMA_LOCATION(虽然这对我来说可能不是一个选项)但仍然没有...
提前致谢!
您应该为 complexType
使用层次结构。 i:type="advice_instruction_adjust"
表示有一个complextType
叫advice_instruction_adjust
.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="advice_instruction_adjust" type="advice_instruction_adjust"/>
<xsd:complexType name="advice_instruction_adjust">
<xsd:complexContent>
<xsd:extension base="Instruction">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Instructions" type="Instructions"/>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction" abstract="true">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
这里是XML
的例子<?xml version="1.0" encoding="UTF-8"?>
<ns1:Instructions xmlns:ns1="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ns1:instruction i:type="advice_instruction_adjust">
<ns1:method />
<ns1:quantity/>
<ns1:site_guid/>
</ns1:instruction>
</ns1:Instructions>