Ошибка десятичной цифры проверки php схемы XML - PullRequest
0 голосов
/ 13 сентября 2011

У меня есть эта схема:

<?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>

, если я проверяю его с помощью функции schemaValidate php DOMDocument, я получаю ошибку на десятичных числах больше 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 Ответ

2 голосов
/ 13 сентября 2011

а) измените этот ярлык на любое значение, которое вам нравится

<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>
...