minOccurs 设置为 0 的 XSD 日期类型现在的值为“
发布于 2024-12-06 22:10:01 字数 3739 浏览 0 评论 0 原文

如果我没有提供足够的信息,请提前原谅我,这是我看到的一个问题并负责修复,但我最初并没有亲自编写这段代码(编写的人已经离开了)。

我们使用 Apache XMLBeans 从一组 XSD 生成一些 Java 类。然后,我们的 Web 应用程序生成一个 Web HTML 表单来创建这些类的实例(或修改现有类的字段)。将其置于某种背景下;我们的 XSD 之一代表一个人。然后,我们可以使用通用表单生成代码为用户生成 HTML 表单,以提供有关特定人员的信息。有许多与人相关的日期属性,例如出生日期。

我最近对项目的结构和依赖项进行了一些实质性更改,但与生成这些 Java 类和 HTML 表单的代码没有直接关系。在进行这些更改之前,一切正常,现在,如果尚未设置任何值,则所有日期字段的值均为 。在 XSD 中,这些日期属性/元素的 minOccurs 属性设置为 0(因此应该是可选的)。如果我将每个属性的 nillable 设置为 true,则该属性可能不再存在。

花了一些时间调试后,我发现空日期值不是有效值,除非 nillable 为 true。在 XMLBeans 类 XmlObjectBase 中,validate 方法对于空日期字段返回 false。我不明白的是发生了什么变化以及为什么现在需要 nillable 属性。依赖关系已发生变化,但比较 XMLBean 版本后,它们似乎都相同:

我们的 Web 应用程序的 lib 目录中有三个包含 XmlObjectBase 类的 jar; tika-app-0.7、xbeans-2.2.0 和 xmlBeans-2.3.0。所有版本都与大规模重组之前的 lib 目录相同。 XSD 文件没有改变。有什么?

到目前为止,我发现以下链接对我的调查有所帮助:

我已经接受这可能是无需花费大量时间即可解决此问题的唯一现实方法就是添加 nillable 属性。但是,我并不热衷于为 XSD 中的所有日期元素添加它。幸运的是,我定义了 3 个日期基本类型,所有日期元素都是如此。我尝试将基本类型的默认 nillable 属性修改为 true,但没有成功:

Original NonFutureDateBaseType:

<xs:complexType name="nonFutureDateType">
    <xs:simpleContent>
        <xs:extension base="cr:nonFutureDateBaseType">
            <xs:attribute name="uid" type="xs:int" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

(其中 cr:nonFutureDateBaseType is a simple type with a limit of the standard XSD date type)

New with attempts nillable default为真:

<xs:complexType name="nonFutureDateType">
    <xs:simpleContent>
        <xs:extension base="cr:nonFutureDateBaseType">
            <xs:attribute name="uid" type="xs:int" />
    <xs:attribute name="nillable" type="xs:boolean" default="true" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

有人对为什么上述方法不起作用有任何建议吗?或者我还应该研究什么?

对于冗长的问题(以及缺乏格式 - 由于某种原因我没有 SO 图像/图标)表示歉意,并提前致谢。

更多详细信息编辑

我刚刚在数据库中查找代表一个人的 XML。我有这样的事情:

<PERSON>
    ...
    <BIRTH_DATE uid="12" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:nil="true" />
    ...
</PERSON>

然后 BIRTH_DATE 的值计算为 。卧槽?从我读到的内容来看,这应该意味着它的计算结果为“”。嗯...

另一个编辑:

我已经厌倦了这个问题,因此开始调查最新的网络应用程序和运行早期代码库的应用程序之间的差异。我在调试时看到的主要区别是:

新代码库:调用 java.beans.PropertyEditorSupport.getValue() 返回

旧代码库:调用 java.beans.PropertyEditorSupport.getValue() 返回

在我的之间生成表单和值的setter的JSP,有很多Spring类。新代码库和旧(工作)代码库之间的主要区别在于 Spring 版本。

新代码库依赖于 spring-context-3.0.5.RELEASE,而旧代码库依赖于 spring-context-2.5.6.SEC02。

任何建议将不胜感激!

Forgive me in advance if I don't provide enough information, this is an issue I am seeing and tasked with fixing but I did not personally write this code originally (the person who did has moved on).

We are using Apache XMLBeans to generate some Java classes from a set of XSDs. Our web application then generates a web HTML form to create instances of these classes (or modify fields of existing classes). To put this into some context; one of our XSDs represents a person. We can then use our generic form generation code to generate an HTML form for a user to supply information about a particular person. There a number of date attributes associated with a person, e.g. date of birth.

I have made some substantial changes recently to the project's structure and dependencies, but nothing directly related to the code the generates these Java classes and HTML form from them. Before these changes were made everything worked, now, all of the date fields have a value of <xml-fragment uid="theAttributesId"/> if they have not had any value set. In the XSDs, these date attributes/elements have the minOccurs attribute set to 0 (and hence should be optional). If I set the attribute nillable to true for each one the probably no longer exists.

Having spent some time debugging I can see that an empty date value is not a valid value unless nillable is true. In the XMLBeans class XmlObjectBase the validate method returns false for null date fields. What I don't understand is what has changed and why is there now a need for the nillable attribute. Dependencies have changed but having compared XMLBean versions they all seem the same:

We have three jars in our web app's lib directory that contain the class XmlObjectBase; tika-app-0.7, xbeans-2.2.0 and xmlBeans-2.3.0. All versions are identical to the lib directory before the massive restructure. The XSD files haven't changed. What has?

I have found the following links which helped in my investigations so far:

I have accepted that maybe the only realistic way of solving this issue without vasts amount of time is to add the nillable attribute. However, I am not keen to add it for all date elements in the XSDs. Fortunately I have 3 date base types defined that all date elements are. I have tried modifying the base types' default nillable attribute to true without success:

Original NonFutureDateBaseType:

<xs:complexType name="nonFutureDateType">
    <xs:simpleContent>
        <xs:extension base="cr:nonFutureDateBaseType">
            <xs:attribute name="uid" type="xs:int" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

(where cr:nonFutureDateBaseType is a simple type with a restriction of the standard XSD date type)

New with attempted nillable default as true:

<xs:complexType name="nonFutureDateType">
    <xs:simpleContent>
        <xs:extension base="cr:nonFutureDateBaseType">
            <xs:attribute name="uid" type="xs:int" />
    <xs:attribute name="nillable" type="xs:boolean" default="true" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

Anyone have any suggestions as to why the above doesn't work? Or what else I should look into?

Apologies for the lengthy question (and lack of formatting - I have no SO images/icons for some reason) and thanks in advance.

More Detail Edit

I've just been looking in the database at the XML that represents a person. I have something like this:

<PERSON>
    ...
    <BIRTH_DATE uid="12" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:nil="true" />
    ...
</PERSON>

And then the value of BIRTH_DATE evaluates to <xml-fragment uid="theAttributesId"/>. Wtf? From what I have read that should mean it evaluates to ''. Hmmm...

Another Edit:

I'm getting seriously fed up with this issue so have started investigating the differences between the latest web app and the one running the earlier codebase. The major difference I see when debugging is this:

New codebase: invoking java.beans.PropertyEditorSupport.getValue() returns
<xml-fragment uid="12"/>

Old codebase: invoking java.beans.PropertyEditorSupport.getValue() returns
<xml-fragment uid="12" xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

Between my JSP that generates the form and the setter for the value, there are a lot of Spring classes. The key difference between the new codebase and the old (working) codebase is the Spring version.

The new codebase depends on spring-context-3.0.5.RELEASE, whereas the old depends on spring-context-2.5.6.SEC02.

Any suggestions would greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-12-13 22:10:01

Nillable 不是您自己实现的东西。它是 XSD 定义的一部分。

xsi:nil="true" 用于指示字段的实例没有值。

nillable="true" 用于指示字段的模式定义不允许有任何值。

还是我错过了重点?

Nillable is not something you implement yourself. It's part of the XSD definition.

xsi:nil="true" is used to indicate that the an instance of a field has no value.

nillable="true" is used to indicate that the schema definition of the field allows no value.

Or have I missed the point?

等数载,海棠开 2024-12-13 22:10:01

经过大量的调试,我有了一个答案 - 我现在知道两个版本之间的区别是什么:

Spring 类 org.springframework.web.servlet.tags.form.ValueFormatter 有一个版本 2.5.6.SEC02 和 3.0.5.RELEASE 之间略有差异。

两个版本之间 getDisplayString 方法的实现有所不同:

2.5.6.SEC02(在我的应用程序中正常工作)

if (propertyEditor != null && !(value instanceof String)) {
    try {
        propertyEditor.setValue(value);
        return getDisplayString(propertyEditor.getAsText(), htmlEscape);
    }
    catch (Throwable ex) {
        // The PropertyEditor might not support this value... pass through.
        return getDisplayString(value, htmlEscape);
    }
}
else {
    return getDisplayString(value, htmlEscape);
}

3.0。 5.RELEASE(如果未设置日期值,则显示 XML 片段):

if (propertyEditor != null && !(value instanceof String)) {
    try {
        propertyEditor.setValue(value);
        String text = propertyEditor.getAsText();
        if (text != null) {
            return getDisplayString(text, htmlEscape);
        }
    }
    catch (Throwable ex) {
        // The PropertyEditor might not support this value... pass through.
    }
}
return getDisplayString(value, htmlEscape);

当日期没有值时,从 propertyEditor.getAsText() 返回的文本始终返回 null,因此使用“value”属性。我相信问题实际上出在我的应用程序中的某些代码中(自定义 XmlBaseTypePropertyEditor 似乎是错误),并且我认为 Spring 更改没有问题。

很抱歉浪费了任何人的时间。不管怎样,谢谢。

Having done a significant amount of debugging, I have an answer - I now know what the difference between the two versions is:

The Spring class org.springframework.web.servlet.tags.form.ValueFormatter has a slight difference between version 2.5.6.SEC02 and 3.0.5.RELEASE.

The implementation of the method getDisplayString differs between the two versions:

2.5.6.SEC02 (Working correctly in my application)

if (propertyEditor != null && !(value instanceof String)) {
    try {
        propertyEditor.setValue(value);
        return getDisplayString(propertyEditor.getAsText(), htmlEscape);
    }
    catch (Throwable ex) {
        // The PropertyEditor might not support this value... pass through.
        return getDisplayString(value, htmlEscape);
    }
}
else {
    return getDisplayString(value, htmlEscape);
}

3.0.5.RELEASE (Display the XML fragment if no date value is set):

if (propertyEditor != null && !(value instanceof String)) {
    try {
        propertyEditor.setValue(value);
        String text = propertyEditor.getAsText();
        if (text != null) {
            return getDisplayString(text, htmlEscape);
        }
    }
    catch (Throwable ex) {
        // The PropertyEditor might not support this value... pass through.
    }
}
return getDisplayString(value, htmlEscape);

The text returned from propertyEditor.getAsText() always returns null for me when the date has no value and hence the 'value' property is used. I believe the issue is actually in some code in my application (the custom XmlBaseTypePropertyEditor is as fault it seems) and I pressume there is no issue with the Spring change.

Sorry for wasting anyone's time. Thanks anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文