gpt4 book ai didi

php - 使用 PHP 解析 XSD 多个模式位置

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:10 25 4
gpt4 key购买 nike

我已经讨论了几个关于如何阅读 XSD 的话题。但是我无法得到正确的答案。

所以这是我的问题。我有 XSD 文件,其中包含 include 元素。所以它使用多模式 位置。我想在 php 中读取该文件以获取从中创建 xml 所需的所有元素。所以我的第一步是如何读取 XSD 文件以获取所有元素以创建 xml 文件。

简而言之,我想做什么。

  • 阅读 XSD 以获取形成 xml 所需的所有元素
  • 用我的数据填充 xml
  • 保存xml

我想知道的

  • Core PHP 可以做到这一点吗?
  • 是否有任何现成的伙伴脚本来执行此操作?
  • 我应该创建一个自定义方法来执行此操作吗?

我尝试过的

我试过 DOMDOCUMENT 加载 xsd 和读取文件,但它不包含多个模式位置文件。

这是我的示例 xsd 文件

主文件Test.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://w3school.com/"
targetNamespace="http://w3school.com/"
elementFormDefault="qualified"
version="2.1">

<xsd:include schemaLocation="Headers.xsd"/>

<xsd:element name="head">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Headers" type="Headers">
<xsd:annotation>
<xsd:documentation>
header
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

包含文件 Header.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://w3school.com/"
targetNamespace="http://w3school.com/"
elementFormDefault="qualified"
version="2.1">


<xsd:element name="Headers" type="Headers"/>

<xsd:complexType name="Headers">
<xsd:sequence>
<xsd:element name="version" minOccurs="1" default="1.0">
<xsd:annotation>
<xsd:documentation>
This indicates schema version associated with the XML
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="1.0" />
<xsd:enumeration value="0.10.8" />
<xsd:enumeration value="0.6" />
<xsd:enumeration value="0.1" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="requestId" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
request id
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

预期输出

<?xml version="1.0" encoding="UTF-8"?>

<head>
<Headers>
<version>1.0</version>
<requestId></requestId>
</Headers>
</head>

最佳答案

我扩展了一点 DOMDocument 类,以便递归地处理包含。看看它是否有帮助(需要进一步测试!)

class MyDomDoc extends DOMDocument
{
public function load($filename, $resolveIncludes = true, $options = null)
{
chdir(dirname($filename));

parent::load($filename, $options);

if ($resolveIncludes) {
$this->resolveIncludes();
}
}

public function resolveIncludes()
{
$this->resolveNodeIncludes($this);
}

private function resolveNodeIncludes(DOMNode $node)
{
if ($this->isIncludeNode($node)) {
$included = new static();
$included->load($node->attributes->getNamedItem('schemaLocation')->textContent);
$this->replaceIncludedElements($included, $node);
}
elseif ($node->childNodes) {
foreach ($node->childNodes as $node) {
$this->resolveNodeIncludes($node);
}
}
}

private function replaceIncludedElements(DOMDocument $included, DOMNode $replace)
{
foreach ($included->firstChild->childNodes as $childNode) {
$replace->parentNode->insertBefore($this->importNode($childNode, true), $replace);
}

$replace->parentNode->removeChild($replace);
}

private function isIncludeNode(DOMNode $node)
{
return $node->localName == 'include' && $node->namespaceURI == 'http://www.w3.org/2001/XMLSchema';
}
}

用法:

$x = new MyDomDoc();
$x->load('path/to/Test.xsd');
echo $x->saveXML();

更新:

好的,从您的评论中可以清楚地看出您不想“解析”XSD 文件(多个模式位置或其他),而是生成一个示例 XML 以适应给定模式。我认为您可以使用我的答案中的代码从多个位置获取单个 XSD,然后在生成示例 XML 的某些工具上使用它。也许thisthis PHP library (我没有使用过这些工具。)

关于php - 使用 PHP 解析 XSD 多个模式位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com