我刚开始使用xml,很难搞清楚这一点。我在网上为RSS规范找到了一个模式,我可以使用xjc在没有问题的情况下从这个自包含的模式中生成java类。
我想从simpledc.xsd中添加字段,因为在RSS中,我看到了dc:creator在<items>上的标记,我希望设置它,以便为它生成代码。在我的尝试中,我为dc模式添加了dc,并在item定义中添加了一个字段。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- My addition -->
<xsd:include schemaLocation="simpledc.xsd" />
<xsd:element name="rss" type="rss" />
<xsd:complexType name="rss">
<xsd:sequence>
<xsd:element name="channel" type="channel" maxOccurs="1"
minOccurs="1" />
...
<xsd:complexType name="item">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" maxOccurs="1" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The title of the item.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
...
<!-- My addition -->
<xsd:element name="creator" type="dc:creator" maxOccurs="1" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>Intellij提供了错误:
Cannot resolve symbol 'dc:creator'xjc还提供了一个类似的错误:
$ xjc -p com.test.generated -d src/main/java/ src/main/resources/schemas/rss.xsd
parsing a schema...
[ERROR] s4s-att-invalid-value: Invalid attribute value for 'type' in element 'element'. Recorded reason: UndeclaredPrefix: Cannot resolve 'dc:creator' as a QName: the prefix 'dc' is not declared.
line 391 of file:/<project>/src/main/resources/schemas/rss.xsd如何正确地设置它,以便可以在xml中添加带有dc命名空间的元素,并使codegen正常工作?
发布于 2022-07-23 17:22:30
我想出了怎么做。我使用了一个include,在这里我应该使用import,因为我引用的是不同的名称空间。然后,对于来自不同名称空间的元素,我需要使用ref链接到它们:
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<xsd:import schemaLocation="dc.xsd" namespace="http://purl.org/dc/elements/1.1/"/>
...
<xsd:complexType name="item">
<xsd:sequence>
...
<!-- My addition -->
<xsd:element minOccurs="0" ref="dc:creator" />
</xsd:sequence>
</xsd:complexType>我不知道都柏林核心网站提供的simpledc.xsd的意图是什么。使用dc.xsd似乎可以生成模式。
不幸的是,当您试图根据此模式解锁xmlns时,rss作者似乎需要在顶级<rss>元素上添加rss字段,而它们通常不需要。
https://stackoverflow.com/questions/73031680
复制相似问题