【发布时间】:2011-11-25 19:03:55
【问题描述】:
我有一个第三方模式 XSD,除了它自己的命名空间中的元素外,它还允许在 XHTML 中包含格式化的文本片段。说(一个精简的样本):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://www.example.org/myns"
xmlns:myns="http://www.example.org/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="body">
<xsd:complexType >
<xsd:sequence>
<xsd:element name="expression" type="xsd:string" />
<xsd:element name="documentation" type="myns:formattedText" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="formattedText">
<xsd:sequence>
<xsd:any namespace="http://www.w3.org/1999/xhtml" processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我使用 JAXB (Sun) (xfc myschema.xsd) 生成了我的 Java 类,除 formattedText 节点之外的解组工作,其 getAny() 方法返回 null。
我对在 Java 中获取 xhtml DOM 一点也不感兴趣,我只想要原始文本 (CDATA)。我想,如果允许我修改架构,请在上面的行中替换:
<xsd:element name="documentation" type="xsd:string" />
就足够了,但如果可能的话,我更喜欢其他方式。我对 JAXB 知之甚少。有什么建议吗?
更新:我更新了架构示例,因此可以逐字进行测试。我希望,对于该架构和以下示例 xml,
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/myns"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="http://www.example.org/myns">
<expression>hi</expression>
<documentation>
Hello <b>world</b>
</documentation>
</body>
要获取在其 body.getDocumentation().getAny() 方法中返回的 Body 类,原始字符串 Hello <b>world</b>
【问题讨论】:
标签: jaxb