duandong9195 2013-04-11 12:31
浏览 52
已采纳

使用PHP将XML模式默认注入到文档中

Here we have a simple XML schema:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="item">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="item" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute name="is-foo" type="xs:boolean" default="true" use="optional" />
            <xs:attribute name="is-bar" type="xs:boolean" default="false" use="optional" />
        </xs:complexType>
    </xs:element>
</xs:schema>

Just recursive <item> nodes with optional is-foo/is-bar boolean attributes, defaulting to true and false respectively.

Now, how are these default values useful, in the context of reading XML data in PHP? If I have a valid XML document:

<item is-foo="false" is-bar="false">
    <item is-bar="true">
        <item>
            <item is-foo="true">
                <item />
                <item is-foo="true" />
            </item>
        </item>
        <item is-foo="true" is-bar="true" />
    </item>
</item>

Against which I run:

$document = new DOMDocument();
$document->load('./test.xml');
var_dump($document->schemaValidate('./test.xsd'));

$xpath = new DOMXPath($document);
foreach ($xpath->query('//item') as $item) {
    echo $item->getNodePath() . PHP_EOL
        . '  is-foo : ' . $item->getAttribute('is-foo') . PHP_EOL
        . '  is-bar : ' . $item->getAttribute('is-bar') . PHP_EOL;
}

I get the following output (which, while expected, is unfortunate):

bool(true)
/item
  is-foo : false
  is-bar : false
/item/item
  is-foo :
  is-bar : true
/item/item/item[1]
  is-foo :
  is-bar :
/item/item/item[1]/item
  is-foo : true
  is-bar :
/item/item/item[1]/item/item[1]
  is-foo :
  is-bar :
/item/item/item[1]/item/item[2]
  is-foo : true
  is-bar :
/item/item/item[2]
  is-foo : true
  is-bar : true

The issue here being all of the empty attribute values.

Aside from programatically injecting default values into the documents (which would require interpreting the schema for any reusability) are there any options to transform an XML document to include default values as defined in the corresponding schema?

  • 写回答

1条回答 默认 最新

  • dongying9756 2013-04-11 16:54
    关注

    This is not currently possible with the DOM extension.

    libxml doesn't do this unless the XML_SCHEMA_VAL_VC_I_CREATE is set on the schema validation context in use, and currently it isn't.

    I've created a patch that should hopefully expose this functionality by means of an additional $flags argument to schemaValidate()/schemaValidateSource() and an new LIBXML_SCHEMA_CREATE constant (example target code), but I've yet to test it as I don't have a usable build environment readily available.

    EDIT

    The patch has now been tested and I've created a PR to merge into the 5.5 branch.

    EDIT 2

    The PR has now been merged into master and the feature should be available from PHP 5.5.2.

    EDIT 3

    See it working

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算