XML模式php验证十进制数错误

时间:2011-09-13 13:55:29

标签: php xsd domdocument

我有这个架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="errorType">
        <xs:restriction base="xs:decimal">
            <xs:totalDigits value="28"/>
            <xs:fractionDigits value="16"/>
            <xs:minExclusive value="0"/>
            <xs:maxExclusive value="1000000000000"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="TEST">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="newElement" type="errorType" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

有这个xml:

<?xml version="1.0" encoding="UTF-8"?>
<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <newElement>123456789012.5000000000000003</newElement>
    <newElement>12345678901.5000000000000003</newElement>
    <newElement>1234567890.5000000000000003</newElement>
    <newElement>123456789.5000000000000003</newElement>
    <newElement>123456789.1234567890123456</newElement>
    <newElement>123456789.500000000000003</newElement>
    <newElement>1234567890.500000000000003</newElement>
    <newElement>12345678901.500000000000003</newElement>
    <newElement>123456789012.50000000000003</newElement>

</TEST>

如果我使用php DOMDocument schemaValidate函数验证它,我会得到一个大于27位的十进制数的错误。有没有办法克服这个问题?

完整代码:

<?php


function libxml_display_error($error)
{
    $return = "<br/>\n";
    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "<b>WARNING:</b> $error->message ";
            break;
        case LIBXML_ERR_ERROR:
            $return .= "<b>ERROR:</b> $error->message ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "<b>FATAL:</b> $error->message ";
            break;
    }
    $return .= " on line <b>$error->line</b>\n";
    return $return;
}

function libxml_display_errors() {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        print libxml_display_error($error);
    }
    libxml_clear_errors();
}

$xsd = '<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="errorType">
        <xs:restriction base="xs:decimal">
            <xs:totalDigits value="28"/>
            <xs:fractionDigits value="16"/>
            <xs:minExclusive value="0"/>
            <xs:maxExclusive value="1000000000000"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="TEST">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="newElement" type="errorType" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>';

$xml ='<?xml version="1.0" encoding="UTF-8"?><TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <newElement>123456789012.5000000000000003</newElement>
    <newElement>12345678901.5000000000000003</newElement>
    <newElement>1234567890.5000000000000003</newElement>
    <newElement>123456789.5000000000000003</newElement>
    <newElement>123456789.1234567890123456</newElement>
    <newElement>123456789.500000000000003</newElement>  <!-- VALID -->
    <newElement>1234567890.500000000000003</newElement>
    <newElement>12345678901.500000000000003</newElement>
    <newElement>123456789012.50000000000003</newElement>
</TEST>
';
    libxml_use_internal_errors(true);
    $xmlObj = new DOMDocument();
    $xmlObj->loadXML($xml);
    $xmlObj->schemaValidateSource($xsd) ;
print_r (libxml_display_errors());

1 个答案:

答案 0 :(得分:2)

a)将此标签更改为您喜欢的任何值

<xs:totalDigits value="28"/>

b)将数字四舍五入小于限制(可能你的数字是29位,包括小数点)

只需执行架构所需的任何限制

<xs:restriction base="xs:decimal">
            <xs:totalDigits value="28"/>
            <xs:fractionDigits value="16"/>
            <xs:minExclusive value="0"/>
            <xs:maxExclusive value="1000000000000"/>
</xs:restriction>