0
我有這樣的模式:XML模式的PHP驗證十進制數錯誤
<?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:如果我驗證使用PHP的DOMDocument schemaValidate功能它,我得到的十進制數錯誤
<?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.123456789</newElement>
<newElement>123456789.500000000000003</newElement>
<newElement>1234567890.500000000000003</newElement>
<newElement>12345678901.500000000000003</newElement>
<newElement>123456789012.50000000000003</newElement>
</TEST>
總數大於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.123456789</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());
錯誤不在模式聲明中,但在處理十進制數字的php中,嘗試使用高位代碼。 – dfilkovi
請仔細閱讀...我寫了b)圍繞數字小於限制 –
使用round()函數獲取所需長度的數字 –