Monday, September 8. 2008XSL Hell :-)
While traveling from Cologne to Schwelm I was working on a xsl stylesheet that produces odd/even rows for a list view. Oh, you say: "Not really hard to implement! Why do you blog about such stuff?" Either I am totally blind or the odd/even topic is really hard to solve problem for recursive, tree like structures, so please: "repeat this answer/question for the following xsl fragment"
Tuesday, July 3. 2007xpathingHave you ever worked with external xml documents where you don't know the hole structure or exactly all the used namespaces? It's a pain. Such a situation occured for one of our user group members, when he had to process a complex rdf document with multiple different xml markups. The following listing shows a modified and simplified version of this rdf document <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="resource" xmlns:br="http://binary.resource/"> <br:length />1024</br:length> </rdf:Description> <rdf:Description rdf:about="meta" xmlns:bm="http://binary.meta/"> <bm:author>Manuel Pichler</bm:author> <bm:published>2007-07-03</bm:published> </rdf:Description> <rdf:Description rdf:about="more" xmlns:bmore="http://binary.more/"> <bmore:more>more and more and more namespaces</bmore:more> </rdf:Description> </rdf:RDF> How would you handle such a structure where you know all or almost all possible markups, but each is optional? The design for the Document Object Model (DOM) assumes that you know all used namespaces in a document. Ok you can use <?php
$dom = new DOMDocument(); $dom->load( "xpath.xml" ); $foundNS = {FNAMEL}">array(); $nodes = $dom->getElementsByTagNameNS( "*", "*" ); foreach ( $nodes as $node ) { if ( $node->namespaceURI === "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ) { continue; } if ( {FNAMEL}">isset( $foundNS[$node->namespaceURI ] ) ) { continue; } $foundNS[$node->namespaceURI] = true; {FNAMEL}">print "ns-prefix: {$node->prefix}, ns-uri: {$node->namespaceURI}\n"; } ?> This could be a good solution for small documents, but consider really large documents with thousands of elements where each element is part of a namespace. This solution would load all these nodes into user land code and check there for an unknown/unvisited namespace which will be very expensive. So we have to find another solution for our problem. And here comes XPath into the play. XPath provides a large set of functions and constructs that help solving this problem. <?php
$dom = new DOMDocument(); $dom->load( "xpath.xml" ); $xp = new DOMXPath( $dom ); $xp->registerNamespace( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ); $nodes = $xp->evaluate( "//*[ name(.) = 'rdf:Description' ]/*[ namespace-uri(.) != namespace-uri(..) and namespace-uri(.) != '' and namespace-uri(.) != namespace-uri(preceding-sibling::*)]" ); foreach ( $nodes as $node ) { {FNAMEL}">print "ns-prefix: {$node->prefix}, ns-uri: {$node->namespaceURI}\n"; } ?> What we do here is. We look for the Comments and optimizations are very welcome.
(Page 1 of 1, totaling 2 entries)
|
ProjectsFurther stuffCategories |

