0

Using PHP Regular Expression I want to extract some value from code

<?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>

I want to extract value 001 from

<xs:enumeration value="001">

For all tag

And text 'Accountancy' from

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

Using php reg ex match

how do I do this?

user713085
  • 61
  • 1
  • 4
  • 7
    Use an xml parser – Richard H Apr 18 '11 at 09:02
  • its not working due to semi column here xs:enu... – user713085 Apr 18 '11 at 09:03
  • 1
    It's called namespace and yes, it's working. – Shikiryu Apr 18 '11 at 09:06
  • 1
    possible duplicate of [SimpleXmlElement and XPath, getting empty array()](http://stackoverflow.com/questions/4024197/simplexmlelement-and-xpath-getting-empty-array) – Gordon Apr 18 '11 at 09:09
  • possible duplicate of [php xpath problems](http://stackoverflow.com/questions/5354345/php-xpath-problems/5354633#5354633) – Gordon Apr 18 '11 at 09:11
  • possible duplicate of [Parsing xml problem…](http://stackoverflow.com/questions/3523012/parsing-xml-problem/3523060#3523060) – Gordon Apr 18 '11 at 09:11
  • possible duplicate of [Parse RDF XML file to get all rdf:about values](http://stackoverflow.com/questions/2486722/parse-rdf-xml-file-to-get-all-rdfabout-values/2486820#2486820) – Gordon Apr 18 '11 at 09:14
  • possible duplicate of [How to parse HTML with PHP?](http://stackoverflow.com/questions/3650125/how-to-parse-html-with-php) – PeeHaa Apr 19 '12 at 21:19

1 Answers1

0

Try this for the first.

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

And for the second

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

but XML parser would be a better solution. DOMDocument can handle it I think.

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • 3
    -1 There is absolutely **no point** to use Regular Expressions for this task. – Gordon Apr 18 '11 at 09:12
  • 1
    Then why are you offering it? See, the OP accepted it and uses the wrong tool for the job now. Parsing information from XML files is a a solved problem. Instead of learning how to do it properly, the OP now uses the Frankenstein solution. – Gordon Apr 18 '11 at 09:22
  • I'm not offering that solution I in fact offered DOMDocument, I just showed how he can get a needed value with Regex that's all. – Headshota Apr 18 '11 at 09:26
  • this solution provides alot of empty in array ... how to remove – user713085 Apr 18 '11 at 09:28
  • @Headshota no, you didnt. There is no DOM solution. You offered a Regex solution. You basically said: "Here is some Crack, but remember drugs are not good for your health". – Gordon Apr 18 '11 at 09:32
  • its says Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ':' in D:\Program Files\webserver\www\car.php on line 5 – user713085 Apr 18 '11 at 09:43
  • @user then why did you accept it? How about you finally look at the four linked duplicates below you question all showing how to do it properly. I assume you are smart enough to substitute arguments to a function? – Gordon Apr 18 '11 at 09:53