简体   繁体   English

解析XML文件时如何处理默认名称空间

[英]How to handle default namespaces when parsing XML files

My PHP page must parse input XML files (XLIFF, to be precise) but it does't work when a default namespace is present in the root element of the XML file. 我的PHP页面必须解析输入的XML文件(准确地说是XLIFF),但是当XML文件的根元素中存在默认名称空间时,该页面不起作用。

My code assumes that a default namespace is required and that it must be urn:oasis:names:tc:xliff:document:1.2 . 我的代码假定需要默认名称空间,并且必须为urn:oasis:names:tc:xliff:document:1.2 If found in the XLIFF root element, it is fetched from there, otherwise it is added by my PHP code. 如果在XLIFF根元素中找到,则从那里获取,否则由我的PHP代码添加。 I thought this was working but it seems it's not, and at the moment the only way I have to make it work is to remove the default namespace from the input XLIFF file. 我以为这是可行的,但似乎不可行,此刻,我唯一可行的唯一方法是从输入XLIFF文件中删除默认名称空间。 Of course, the PHP script should work regardless of whether the default namespace is present in the XLIFF file or not. 当然,无论XLIFF文件中是否存在默认名称空间,PHP脚本都应该起作用。

Under the understanding that a default namespace is necessary, in my PHP script I have: 在了解必须使用默认名称空间的情况下,在我的PHP脚本中,我具有:

$xml_file = file_get_contents($pathToInputFile);
if($xml_file === FALSE) {
    die("there is a problem to get contents from XLIFF file");
} 

$xliffObj = new DOMDocument();
$xliffObj->preserveWhiteSpace = true;
$xliffObj->loadXML($xml_file);

$context = $xliffObj->documentElement;
$xpath = new DOMXPath($xliffObj);

if (isSet($context->getAttributeNode('xmlns')->nodeValue)) {
    $ns = $context->getAttributeNode('xmlns')->nodeValue; 
    echo "The ns is: " . $ns;                          // line 198
}
else {
    $ns = "urn:oasis:names:tc:xliff:document:1.2";
    // this works when no default namespaces is defined in the XLIFF file
    echo "I have defined the ns as: " . $ns; 
}

$xpath->registerNamespace('ns', $ns);                 // line 208

$tus = $xpath->query('//trans-unit');
var_dump_pre($tus);die;

The parsing works fine if my input XLIFF file has: 如果我的输入XLIFF文件具有以下内容,则解析工作正常:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xliff PUBLIC "-//XLIFF//DTD XLIFF//EN" "http://www.oasis-open.org/committees/xliff/documents/xliff.dtd">
<xliff xmlns:pisa="http://www.ets.org/pisa" version="1.2">

In that case, the output is 在这种情况下,输出为

I have defined the ns as: urn:oasis:names:tc:xliff:document:1.2 我已将ns定义为:urn:oasis:names:tc:xliff:document:1.2

object(DOMNodeList)#12 (1) { ["length"]=> int(2) } object(DOMNodeList)#12(1){[“ length”] => int(2)}

The $tus array contains the two trans-unit nodes in the XLIFF file. $tus数组在XLIFF文件中包含两个trans-unit节点。

However, when the file has 但是,当文件具有

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xliff PUBLIC "-//XLIFF//DTD XLIFF//EN" "http://www.oasis-open.org/committees/xliff/documents/xliff.dtd">
<xliff xmlns:pisa="http://www.ets.org/pisa" version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">

then the nothing is extracted and the array where I save the contents of the file is empty (has NULL value). 然后什么都不会提取,并且我保存文件内容的数组为空(具有NULL值)。 The output is: 输出为:

The ns is: urn:oasis:names:tc:xliff:document:1.2 ns是:urn:oasis:names:tc:xliff:document:1.2

object(DOMNodeList)#10 (1) { ["length"]=> int(0) } object(DOMNodeList)#10(1){[“”“] => int(0)}

As you can see, the $tus array is empty. 如您所见, $tus数组为空。

A potential solution could be to simply remove the namespace declaration before adding it again, but I would like to understand what the problem is. 一个可能的解决方案是在再次添加名称空间声明之前先删除它,但是我想了解问题所在。 Thanks. 谢谢。

It seems it is necessary to add the namespace to the xpath only when it is present in the XML file, thus: 似乎只有在XML文件中存在命名空间时才有必要将命名空间添加到xpath,因此:

$xpath->registerNamespace('ns', $ns);
$tus = $xpath->query('//ns:trans-unit');

However, I'm not sure this could backfire in other situations... 但是,我不确定这在其他情况下是否会适得其反...

When it is not present, it seems it's not necessary to include it in the xpath expression: 当它不存在时,似乎没有必要将其包括在xpath表达式中:

#$xpath->registerNamespace('ns', $ns);
$tus = $xpath->query('//trans-unit');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM