【问题标题】:Using PHP regular expressions to extract a value from an XML Schema使用 PHP 正则表达式从 XML Schema 中提取值
【发布时间】:2011-04-18 09:01:39
【问题描述】:

使用 PHP 正则表达式我想从代码中提取一些值

<?xml version="1.0" encoding="UTF-8"?>
                <xs:restriction base="xs:string">
                <xs:enumeration value="001">
                    <xs:annotation>
                        <xs:documentation>Accountancy</xs:documentation>
                    </xs:annotation>
                </xs:enumeration>
                <xs:enumeration value="002">
                    <xs:annotation>
                        <xs:documentation>Advertising</xs:documentation>
                    </xs:annotation>
                </xs:enumeration>
                <xs:enumeration value="005">
                    <xs:annotation>
                        <xs:documentation>Amusement</xs:documentation>
                    </xs:annotation>
                </xs:enumeration>
            </xs:enumeration>
            </xs:restriction>
        </xs:simpleType>
    </xs:schema>

我想从中提取值 001

<xs:enumeration value="001">

对于所有标签

和来自

的文本'Accountancy'
<xs:documentation>Accountancy</xs:documentation>

使用 php reg ex 匹配

我该怎么做?

【问题讨论】:

标签: php xml regex xsd


【解决方案1】:

先试试这个。

preg_match("/<xs:enumeration value=\"(\d+)\">/", $string, $matches);
print_r($matches);

第二个

preg_match("/<xs:documentation>(.+)</xs:documentation>/", $string, $matches);
print_r($matches);

但 XML 解析器会是更好的解决方案。我认为 DOMDocument 可以处理。

【讨论】:

  • -1 绝对没有意义为此任务使用正则表达式。
  • 那你为什么要提供它?看,OP 接受了它并且现在使用了错误的工具来完成这项工作。从 XML 文件解析信息是一个已解决的问题。 OP 现在没有学习如何正确地做到这一点,而是使用科学怪人解决方案。
  • 我没有提供我实际上提供 DOMDocument 的解决方案,我只是展示了他如何使用 Regex 获得所需的值。
  • 这个解决方案提供了很多空数组...如何删除
  • @Headshota 不,你没有。没有 DOM 解决方案。您提供了一个正则表达式解决方案。您基本上是说:“这里有一些 Crack,但请记住药物对您的健康不利”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多