如何使用 idref 获取 xml 元素的内容
我有一个 XML 模式:-
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author">
<xsd:complexType>
<xsd:attribute name="idref" type="xsd:IDREF"
use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Person" />
<xsd:element ref="Book" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
与上面的 XML 模式相对应,我有以下传入的 XML:-
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Person id="P234">
<Name>0002</Name>
</Person>
<Book>
<Title>0001</Title>
<Author idref="P234"/>
</Book>
</root>
我知道使用 XML 解析器验证,我可以验证上面的 XML 是否符合我的 XML 模式。例如 id 和 idref 应该存在。现在我想知道哪个解析器(SAX/DOM/STAX)可以根据 idref 获取完整的 XML 元素。所以基本上在上面的例子中,一旦解析器到达 idref="P234",它应该返回完整的
I have a XML schema:-
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author">
<xsd:complexType>
<xsd:attribute name="idref" type="xsd:IDREF"
use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Person" />
<xsd:element ref="Book" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
and corresponding to above XML schema, I have following incoming XML:-
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Person id="P234">
<Name>0002</Name>
</Person>
<Book>
<Title>0001</Title>
<Author idref="P234"/>
</Book>
</root>
I know using XML parser validation, I can validate if above XML conforms to my XML schema.for e.g. id and idref should be present. Now what I want to know is which parser(SAX/DOM/STAX) can fetch me complete XML element based on idref. So basically in above example, once parser reaches idref="P234", it should return me complete <Person>...</Person>
. Another query is does any parser support id and idref merging, which can replace content of idref with actual element and return me merged XML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,解析器不会这样做。使用 XSLT 发挥魔法。此外,idrefs 可以是自引用的,具有循环依赖关系,因此不可能只是“用实际元素替换内容”。
例如,假设您有 xml:
您对解析器有何期望?
XSLT(不过我自己没有测试过):
Parsers don't do it, as I know. Use XSLT to do the magic. Moreover, idrefs could be self-referenced, have a cyclic dependency, so it's impossible just "replace content with actual element".
E.g. say you have the xml:
What would you expect from a parser?
An XSLT (not tested myself however):