我有一个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元素。所以基本上在上面的示例中,once parser reaches idref="P234", it should return me complete <Person>...</Person>
。另一个查询是任何解析器支持id和idref合并,它可以用实际元素替换idref的内容并返回合并的XML。
答案 0 :(得分:2)
E.g。说你有xml:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Person id="P234">
<Name>0002</Name>
<WroteBook idref="B442"/>
</Person>
<Book id="B442">
<Title>0001</Title>
<Author idref="P234"/>
</Book>
</root>
您对解析器的期望是什么?
XSLT(不是自己测试过):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@idref">
<xsl:apply-templates select="id(.)"/>
</xsl:template>
</xsl:stylesheet>