1

我正在生成 XML,其中主要数据来自 xsl 转换(但这不是问题,这只是我不使用 PHP DOM 或 SimpleXML 的原因)。

像这样:

$xml =  '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
$xml .= '<rootElement>';

foreach($xslRenderings as $rendering) {
    $xml .= $rendering;
}

$xml .= '</rootElement>';

生成的 XML 在此处针对其 XSD 进行验证http://www.freeformatter.com/xml-validator-xsd.html和此处http://xsdvalidation.utilities-online.info/

但在这里失败:http ://www.xmlforasp.net/schemavalidator.aspx ,

Unexpected XML declaration. The XML declaration must be the first node in the 
document, and no white space characters are allowed to appear before it. 
Line 2, position 3.

如果我确实手动删除了 PHP_EOL 产生的换行符并点击返回,它会验证。

我假设这是最后一个模式验证器中的错误。还是 PHP_EOL(或 PHP 中的手动中断)对某些验证器来说是个问题?如果是,如何解决?

我问是因为生成的 XML 将被发送到 .NET 服务,并且最后一个验证器是用 NET 构建的。

编辑

XML 看起来像这样,Scheme 可以在这里找到http://cb.heimat.de/interface/schema/interfaceformat.xsd

<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://cb.heimat.de/interface/schema/interfaceformat.xsd">
<production foreignId="1327" id="0" cityId="6062" productionType="3" subCategoryId="7013" keywords="" productionStart="" productionEnd="" url=""><title languageId="1">
...
</production>
4

1 回答 1

1

您确实必须将生成的 XML 视为二进制流来了解发生了什么。我会试着解释你应该看什么......

我将向您展示无效 XML 的转储(类似于您的)以帮助说明:

在此处输入图像描述

前三个字节是字节顺序标记,可能会遇到文本文件和流(在本例中为 UTF-8)。这些字节永远不会导致兼容的 XML 解析器出错,因为它们被用作理解编码方案的提示。

接下来的两个字节(0x0D0A)是 Windows 平台上的新行。这些应该会导致任何 XML 解析器无法通过格式良好的规则。根据当前的 XML 1.0 标准,在 XML 声明之前不允许有空格。

在 .NET 上,您会收到如您所描述的错误。Java(基于 xerces)会说一些更神秘的东西:The processing instruction target matching "[xX][mM][lL]" is not allowed. [2]

在第一次删除之前删除任何空白<应该可以修复此错误消息。你所要做的就是了解那个空白是如何到达那里的......

根据您的描述,XML PI 在使用 XML 之前似乎以某种方式被丢弃了。

于 2012-08-14T17:49:17.963 回答