使用PHP正则表达式从XML Schema中提取值


Using PHP regular expressions to extract a value from an XML Schema

使用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">

对于所有标签

和text ' accounting ' from

<xs:documentation>Accountancy</xs:documentation>

使用php greex匹配

我该怎么做?

第一次试试。

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

第二个

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

,但XML解析器将是一个更好的解决方案。