JAXB解组不能容忍令牌枚举周围的空格 [英] JAXB unmarshalling not tolerating whitespace around token enumerations

查看:95
本文介绍了JAXB解组不能容忍令牌枚举周围的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JAXB 2 (Oracle / Metro版本2.2.7,我怀疑其他人也没有)似乎容忍枚举元素中值周围的空格。

JAXB 2 (Oracle / Metro version 2.2.7 and I suspect others as well) doesn't seem to tolerate whitespace around values in enumeration elements.

下面是最小的例子。 xmllint Xerces 根据模式验证实例。令人费解的是,JAXB验证不会抱怨,但在尝试访问该值时会返回 null 。如何配置它以正确返回值?

Minimal example follows. Both xmllint and Xerces validate the instance against the schema. The puzzling think is that JAXB validation doesn't complain but returns null when trying to access the value. How can I configure it to return the value properly?

更新:我尝试关联 XmlAdapter 修剪字符串,如所示这里,但结果是一样的。

update: I 've tried associating an XmlAdapter to trim the strings, as suggested here, but the result is the same.

更新II:这里是 Metro JAXB Jira的门票

<xs:schema targetNamespace="foo://a" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="foo://a">

   <xs:element name="type" type="Type"/>

   <xs:simpleType name="Type">
     <xs:restriction base="xs:token">
         <xs:enumeration value="Archive"/>
         <xs:enumeration value="Organisation"/>
       </xs:restriction>
   </xs:simpleType>

</xs:schema>



a.xml



a.xml

<a:type xmlns:a="foo://a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="foo://a A.xsd"
>Organisation </a:type>

(注意组织之后的空格)

(notice the whitespace after 'Organisation')

public static void main(String args[]) throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    JAXBContext payloadContext = JAXBContext.newInstance("a");
    Unmarshaller unmarshaller = payloadContext.createUnmarshaller();
    unmarshaller.setSchema(schemaFactory.newSchema(new Source[]{new StreamSource(new FileInputStream(new File("A.xsd")))}));
    JAXBElement<?> oUnmarshalled = (JAXBElement<?>) unmarshaller.unmarshal(new File("a.xml"));
    Object o = oUnmarshalled.getValue(); // returns NULL
}


推荐答案

从(暂时)我想坚持JAXB RI(仅被RI部分吸引),最后我用替换(。,'^ \ s + | \s + $在建议的XSLT 2.0转换中,','','m')

Since (for the time being) I want to stick to JAXB RI (being drawn to the "RI" part alone), in the end I used the replace(., '^\s+|\s+$', '', 'm') in an XSLT 2.0 transformation as suggested here.

相关代码部分为:

    Source input = new StreamSource(new File("a.xml"));
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer wsTrimmer = factory.newTransformer(new StreamSource(new File("transform-trim-all.xslt")));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    wsTrimmer.transform(input, new StreamResult(bos));

    JAXBElement<?> oUnmarshalled = (JAXBElement<?>) unmarshaller.unmarshal(new ByteArrayInputStream(bos.toByteArray()));

代码需要Saxon HE在运行时路径上,因为XSLT使用的是,我使用了以下内容常春藤依赖:

The code requires Saxon HE on the runtime path as the XSLT used is , I've used the following ivy dependency:

<dependency org="net.sf.saxon" name="Saxon-HE" rev="9.4"/>

XSLT 是:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
       <xsl:sequence select="replace(., '^\s+|\s+$', '', 'm')"/>
    </xsl:template>
</xsl:stylesheet>

这篇关于JAXB解组不能容忍令牌枚举周围的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆