Nic*_*las 6 java xml xpath jaxb xml-serialization
我有一个xsd文件(yahoo.xsd),我导入另一个xsd文件,如下所示:
<xs:import schemaLocation="stock.xsd"/>
<xs:attribute name="lang" type="xs:NCName"/>
Run Code Online (Sandbox Code Playgroud)
stock.xsd看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">
<xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>
<xs:element name="quote">
<xs:complexType>
<xs:sequence>
<xs:element ref="Symbol"/>
</xs:sequence>
<xs:attribute name="symbol" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="Symbol" type="xs:NCName"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
当我使用xjc进行编译时,我收到以下错误消息:
[错误]属性"符号"已定义.使用<jaxb:property>解决此冲突.
我基本上在SO上找到了这个解决方案(JAXB编译问题 - [错误]属性"任何"已经定义)但是我无法让它工作.我猜我的XPath是错的.
这是我正在使用的绑定文件:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="yahoo.xsd" version="1.0" >
<!-- rename the value element -->
<bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
<property name="SymbolAttribute"/>
</bindings>
</bindings>
Run Code Online (Sandbox Code Playgroud)
如果我现在正在使用xjc -b进行编译,则表示XPath评估会导致空目标节点.
我可能要重命名Symbol定义,然后重命名?怎么自动这样做?
让我问一下这句话:
<xs:element ref="Symbol"/>
Run Code Online (Sandbox Code Playgroud)
是在yahoo.xsd中定义的符号还是在同一个xsd文件中本地?
我会尝试推断出一些事实.
我假设你有两个的XSD:yahoo.xsd
和some.xsd
(在您的文章第一个).我有强烈的信心"符号"类型定义在some.xsd
和不在yahoo.xsd
.如果是这样,我会期待一些名称空间前缀("雅虎:符号"?).
现在,你的some.xsd看起来是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" >
<!-- It's not important right now: -->
<!--<xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>-->
<!-- declaration you omitted in your post, it's only example -->
<xs:element name="Symbol">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="quote">
<xs:complexType>
<xs:sequence>
<xs:element ref="Symbol"/>
</xs:sequence>
<xs:attribute name="symbol" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
如果我说的是真的,那么你的jaxb绑定应该如下所示:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="some.xsd"> <!-- not yahoo.xsd -->
<bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
<property name="SymbolAttribute" />
</bindings>
</bindings>
</bindings>
Run Code Online (Sandbox Code Playgroud)
而生成的java类将是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"symbolAttribute"
})
@XmlRootElement(name = "quote")
public class Quote {
@XmlElement(name = "Symbol")
protected int symbolAttribute;
@XmlAttribute(name = "symbol", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String symbol;
....
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11851 次 |
最近记录: |