XML/XPath: Difference between revisions
Content deleted Content added
mNo edit summary |
revert spam |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://svike.info/view/new/gli-unni.htm gli unni] [http://soler.net.cn/data/alessandria-chieti.htm alessandria chieti] [http://veggis.org.cn/resources/articles/radio-control.htm radio control] [http://joperan.org/data/sharp/ sharp 15] [http://wedner.info/content/view/domenico-modugno.htm domenico modugno nel blu dipinto di blu] [http://xionny.cn/topic/tratado-de/ tratado de peru con chile] [http://polex.com.cn/resources/articles/cellulari-gps.htm cellulari gps] [http://sneck.info/data/volvo-s/ volvo s60 d5 kinetic] [http://primonona.info/content/view/il-bagatto/ il bagatto] [http://funkall.us/images/small/knock-on/ knock on wood] [http://veggis.org.cn/resources/articles/ricetta-torta.htm ricetta torta di nutella] [http://budget7i.info/lib/ragnarok/ ragnarok] [http://veggis.org.cn/resources/articles/tv-schermo.htm tv schermo lcd 10] [http://soler.net.cn/data/i-casi.htm i casi del] [http://klohy.info/img/styles/piattoni-franca.htm piattoni franca] [http://soler.net.cn/data/alberghi-di.htm alberghi di una stella milano] [http://primonona.info/content/view/jvc-kdg/ jvc kd-g411] [http://joperan.org/data/la-classica/ la classica] [http://budget7i.info/lib/lavasciuga-biancheria/ lavasciuga biancheria] [http://veggis.org.cn/resources/articles/vivaldi-la.htm vivaldi la notte] [http://svike.info/view/new/sex-shop.htm sex shop ragusa] [http://manoke.cn/topic/zalman-cu/ zalman 700cu] [http://joperan.org/data/giovanni-papini/ giovanni papini] [http://sneck.info/data/tivutibi-com/ tivutibi com] [http://manoke.cn/topic/nec-e/ nec e313 sbloccare] [http://primonona.info/content/view/obrecht-jacob/ obrecht, jacob] [http://klohy.info/img/styles/tiscali-giochi.htm tiscali giochi trucchi] [http://funkall.us/images/small/ipod-custodie/ ipod custodie] [http://svike.info/view/new/profumi-cartier.htm profumi cartier] [http://polex.com.cn/resources/articles/herson.htm herson] [http://sneck.info/data/go-west/ go west] [http://klohy.info/img/styles/puma-ilo.htm puma ilo] [http://wedner.info/content/view/sogni-di.htm sogni di gloria] [http://manoke.cn/topic/numeri-privati/ numeri privati] [http://manoke.cn/topic/ggggggggggggggggggggggggggggggggg/ ggggggggggggggggggggggggggggggggg] [http://primonona.info/content/view/hp-diapositive/ hp diapositive scanner] [http://primonona.info/content/view/la-trasformazione/ la trasformazione eterogenea] [http://veggis.org.cn/resources/articles/wireless-g.htm wireless 11g access point] [http://xionny.cn/topic/voli-per/ voli per edimburgo] [http://svike.info/view/new/staffa-a.htm staffa a muro sony] [http://soler.net.cn/data/tejido-vegetal.htm tejido vegetal] [http://xionny.cn/topic/genesis-songbook/ genesis songbook] [http://sneck.info/data/minijuegoscom/ minijuegos.com] [http://sneck.info/data/www-sleazydream/ www sleazydream com] [http://veggis.org.cn/resources/articles/geforce.htm geforce 5500 256] [http://manoke.cn/topic/nvidia-/ nvidia 6200 256mb agp] [http://sneck.info/data/pagina-erotica/ pagina erotica] [http://svike.info/view/new/router-cisco.htm router cisco per connessione adsl] [http://wedner.info/content/view/gioco-taipei.htm gioco taipei] [http://sneck.info/data/gammarus/ gammarus] [http://xionny.cn/topic/agenzia-delle/ agenzia delle dogana] [http://polex.com.cn/resources/articles/master-prisma.htm master prisma] [http://primonona.info/content/view/river-below/ river below] [http://veggis.org.cn/resources/articles/sophie-kinsella.htm sophie kinsella] [http://xionny.cn/topic/giochi-di/ giochi di polizia] [http://veggis.org.cn/resources/articles/marlowe-indaga.htm marlowe indaga] [http://joperan.org/data/dissipatore-socket/ dissipatore socket 462] [http://veggis.org.cn/resources/articles/disegni-da.htm disegni da colorare babbo natale] [http://veggis.org.cn/resources/articles/monastero-s.htm monastero s rita] [http://svike.info/view/new/nuovi-angeli.htm nuovi angeli] [http://budget7i.info/lib/moglie-nuda/ moglie nuda e siciliana] [http://wedner.info/content/view/cabrio-mini.htm cabrio mini cooper s] [http://funkall.us/images/small/www-telemarket/ www telemarket it] [http://budget7i.info/lib/biotherm-line/ biotherm line peel] [http://soler.net.cn/data/duello-con.htm duello con la morte] [http://funkall.us/images/small/paglialunga/ paglialunga] [http://polex.com.cn/resources/articles/fiamme-a.htm fiamme a calcutta] [http://sneck.info/data/an/ an-] [http://wedner.info/content/view/traghetto-elba.htm traghetto elba] [http://polex.com.cn/resources/articles/mutuo-agevolato.htm mutuo agevolato giovane coppia] [http://xionny.cn/topic/asus-pnd/ asus p5nd2 deluxe] [http://sneck.info/data/chat-al/ chat al buio ha palermo] [http://sneck.info/data/bagnati/ bagnati] [http://soler.net.cn/data/rendita-catastale.htm rendita catastale] [http://soler.net.cn/data/optio-pentax.htm optio pentax] [http://manoke.cn/topic/friends-/ friends - stagione 1] [http://budget7i.info/lib/nikon-dx/ nikon dx] [http://joperan.org/data/trinity-college/ trinity college] [http://manoke.cn/topic/scara/ scara] [http://soler.net.cn/data/emirates.htm emirates] [http://polex.com.cn/resources/articles/compressori-frigorifero.htm compressori frigorifero] [http://budget7i.info/lib/torna-con/ torna con me] [http://joperan.org/data/epson-aculaser/ epson aculaser al-cx11n] [http://polex.com.cn/resources/articles/cipolline-in.htm cipolline in agro dolce] [http://veggis.org.cn/resources/articles/condizionatore-portatile.htm condizionatore portatile pompa di calore] [http://svike.info/view/new/verbatim-dvd.htm verbatim dvd] [http://budget7i.info/lib/www-emendamento/ www emendamento legge 194 it] [http://primonona.info/content/view/remember-me/ remember me james horner] [http://budget7i.info/lib/voglie-pazze/ voglie pazze, desideri... notti di piacere] [http://funkall.us/images/small/calciatori-nudi/ calciatori nudi] [http://soler.net.cn/data/acer-lettore.htm acer lettore mp3 512] [http://wedner.info/content/view/futurama-magazines.htm futurama magazines] [http://joperan.org/data/sposati/ sposati] [http://budget7i.info/lib/www-caesartour/ www caesartour it] [http://joperan.org/data/brenda-exporto/ brenda exporto] [http://sneck.info/data/forone/ forone] [http://sneck.info/data/red-zone/ red zone] [http://polex.com.cn/resources/articles/manic-street.htm manic street teachers] [http://soler.net.cn/data/sedile-doccia.htm sedile doccia] [http://veggis.org.cn/resources/articles/sdio-card.htm sdio card] [http://polex.com.cn/resources/articles/free-free.htm free free] [http://klohy.info/img/styles/foto-pene.htm foto pene] [http://funkall.us/images/small/ahmet-hikmet/ ahmet hikmet] [http://veggis.org.cn/resources/articles/isola-di.htm isola di white] [http://klohy.info/img/styles/s.htm s3] [http://klohy.info/img/styles/lofra-mx.htm lofra mx 66] [http://sneck.info/data/foto-ss/ foto ss lazio] [http://veggis.org.cn/resources/articles/hbh-sony.htm hbh-35 sony ericsson] [http://sneck.info/data/fiat-coupe/ fiat coupe 1 8 turbo] [http://klohy.info/img/styles/kit-scaffale.htm kit scaffale] [http://manoke.cn/topic/mp-player/ mp3 player sitecom] [http://wedner.info/content/view/il-marito.htm il marito ricco] [http://svike.info/view/new/masters.htm masters] [http://joperan.org/data/killing-berg/ killing berg] [http://soler.net.cn/data/kim-beheading.htm kim beheading] [http://xionny.cn/topic/hp-dlt/ hp dlt vs80] [http://funkall.us/images/small/kenzo-donna/ kenzo donna profumo] [http://svike.info/view/new/cristallo-boemia.htm cristallo boemia] [http://svike.info/view/new/trans-matura.htm trans matura] [http://joperan.org/data/settore-secondario/ settore secondario] [http://klohy.info/img/styles/yu-gi.htm yu gi oh ps2] [http://polex.com.cn/resources/articles/tema-guerra.htm tema guerra in iraq] [http://primonona.info/content/view/gibsy-king/ gibsy king] [http://soler.net.cn/data/scanner-per.htm scanner per pellicole canon] [http://manoke.cn/topic/rimorchio-usati/ rimorchio usati] [http://primonona.info/content/view/ranno/ ranno] [http://budget7i.info/lib/sex-dog/ sex dog] [http://polex.com.cn/resources/articles/il-triangolo.htm il triangolo del delitto] [http://sneck.info/data/www-inpdap/ www inpdap i t] [http://polex.com.cn/resources/articles/immagini-piedi.htm immagini piedi] [http://svike.info/view/new/manifesto-del.htm manifesto del futurismo] [http://svike.info/view/new/terratec.htm terratec 5 1] [http://polex.com.cn/resources/articles/obiettivi-nikon.htm obiettivi nikon af-s] [http://wedner.info/content/view/dediche.htm dediche] [http://svike.info/view/new/nike-triax.htm nike triax] [http://soler.net.cn/data/epson-stylus.htm epson stylus cx3600 cartucce] [http://polex.com.cn/resources/articles/hotel-economico.htm hotel economico brescia] [http://manoke.cn/topic/philips-hz/ philips 100hz 32] [http://klohy.info/img/styles/giochi-.htm giochi per adulti] [http://sneck.info/data/parma-albergo/ parma albergo] [http://funkall.us/images/small/hotel-passo/ hotel passo del tonale] [http://budget7i.info/lib/plasma-/ plasma 42 1600x1200] [http://manoke.cn/topic/galleggiante-nuoto/ galleggiante nuoto] [http://budget7i.info/lib/mahra/ mahra] [http://sneck.info/data/music-rai/ music rai maroc] [http://veggis.org.cn/resources/articles/testo-della.htm testo della canzone forca di nely furtado] [http://wedner.info/content/view/www-sexview.htm www sexview plus tv] [http://soler.net.cn/data/instituto-centrale.htm instituto centrale delle banche popolari italiane] [http://polex.com.cn/resources/articles/video-militari.htm video militari porno iraq] [http://sneck.info/data/tesina-appunto/ tesina appunto] [http://wedner.info/content/view/musica-cantata.htm musica cantata anima libera] [http://xionny.cn/topic/belinea/ belinea 10 19 15] [http://klohy.info/img/styles/concessionarie-ford.htm concessionarie ford] [http://svike.info/view/new/ultimo-samura.htm ultimo samura i copertina] [http://veggis.org.cn/resources/articles/lavatrice-.htm lavatrice 7 5 kg] [http://primonona.info/content/view/audi-a/ audi a3 3.2 quattro] [http://wedner.info/content/view/hard-dvd.htm hard dvd recorder divx] [http://svike.info/view/new/buy-fotomodella.htm buy fotomodella] [http://budget7i.info/lib/film-erotici/ film erotici] [http://wedner.info/content/view/hard-disk.htm hard disk ibm travelstar] [http://veggis.org.cn/resources/articles/redstone.htm redstone] [http://joperan.org/data/holux-sirf/ holux sirf gps] [http://wedner.info/content/view/il-mistero.htm il mistero di black angel] [http://xionny.cn/topic/traduzione-testo/ traduzione testo aisha] [http://polex.com.cn/resources/articles/lotto-svizzero.htm lotto svizzero] [http://veggis.org.cn/resources/articles/w-w.htm w w w vigili del fuoco it bando 200 pos] [http://funkall.us/images/small/samsung-dvi/ samsung dvi 19] [http://sneck.info/data/placebo/ placebo] [http://xionny.cn/topic/xbox-rally/ xbox rally] [http://manoke.cn/topic/astraeus/ astraeus] [http://primonona.info/content/view/eminem-da/ eminem da record] [http://manoke.cn/topic/once-upon/ once upon a time in mexico] [http://funkall.us/images/small/john-huston/ john huston] [http://klohy.info/img/styles/sonny-rollins.htm sonny rollins] [http://manoke.cn/topic/tecniche-del/ tecniche del bacio] [http://klohy.info/img/styles/angelica-levi.htm angelica levi upskirt] [http://budget7i.info/lib/nvidia-gt/ nvidia gt6800] [http://manoke.cn/topic/navigatore-satellitare/ navigatore satellitare palmare tomtom] [http://manoke.cn/topic/club-punto/ club punto] [http://svike.info/view/new/riposo-spostato.htm riposo spostato] [http://svike.info/view/new/cuffia-behringer.htm cuffia behringer] [http://svike.info/view/new/front-.htm front 242. speed tribe] [http://svike.info/view/new/riassunto-la.htm riassunto la lupa] [http://manoke.cn/topic/email-virus/ email virus] [http://funkall.us/images/small/glem-cucina/ glem cucina] [http://wedner.info/content/view/cuffie-microfono.htm cuffie microfono speaker] [http://soler.net.cn/data/on-your.htm on your knees] [http://sneck.info/data/f-c/ f c barcelona] [http://svike.info/view/new/frankee-lyrics.htm frankee lyrics] [http://polex.com.cn/resources/articles/profili.htm profili] [http://polex.com.cn/resources/articles/un-emozione.htm un emozione per sempre di eros ramazzotti] [http://xionny.cn/topic/pubblicita-adidas/ pubblicita adidas] [http://veggis.org.cn/resources/articles/el-alem.htm el alem alah] [http://sneck.info/data/cartoline-di/ cartoline di napoli] [http://primonona.info/content/view/giradischi-sony/ giradischi sony] [http://xionny.cn/topic/schermo-per/ schermo per videoproiettori accessori] [http://xionny.cn/topic/bmw/ bmw 520] [http://veggis.org.cn/resources/articles/mobili-giardino.htm mobili giardino erre] [http://klohy.info/img/styles/vgn-fsm.htm vgn fs315m] [http://xionny.cn/topic/vagina/ vagina] {{task}} |
|||
Perform the following three XPath queries on the XML Document below: |
Perform the following three XPath queries on the XML Document below: |
||
Line 87: | Line 87: | ||
</cfsavecontent> |
</cfsavecontent> |
||
<cfset xml = xmlParse(xmlString)> |
<cfset xml = xmlParse(xmlString)> |
||
<!--- First Task ---> |
|||
<cfset itemSearch = xmlSearch(xml, "//item")> |
|||
<!--- item = the first Item (xml element object) ---> |
|||
<cfset item = itemSearch[1]> |
|||
<!--- Second Task ---> |
|||
<cfset priceSearch = xmlSearch(xml, "//price")> |
|||
<!--- loop and print each price ---> |
|||
<cfloop from="1" to="#arrayLen(priceSearch)#" index="i"> |
|||
#priceSearch[i].xmlText#<br/> |
|||
</cfloop> |
|||
<!--- Third Task ---> |
|||
<!--- array of all the name elements ---> |
|||
<cfset names = xmlSearch(xml, "//name")> |
|||
<!--- visualize the results ---> |
|||
<cfdump var="#variables#"> |
|||
==[[JavaScript]]== |
|||
[[Category:JavaScript]] |
|||
'''Interpreter:''' Firefox 2.0 |
|||
//create XMLDocument object from file |
|||
var xhr = new XMLHttpRequest(); |
|||
xhr.open('GET', 'file.xml', false); |
|||
xhr.send(null); |
|||
var doc = xhr.responseXML; |
|||
//get first <item> element |
|||
var firstItem = doc.evaluate( '//item[1]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue; |
|||
alert( firstItem.textContent ); |
|||
//output contents of <price> elements |
|||
var prices = doc.evaluate( '//price', doc, null, XPathResult.ANY_TYPE, null ); |
|||
for( var price = prices.iterateNext(); price != null; price = prices.iterateNext() ) { |
|||
alert( price.textContent ); |
|||
} |
|||
//add <name> elements to array |
|||
var names = doc.evaluate( '//name', doc, null, XPathResult.ANY_TYPE, null); |
|||
var namesArray = []; |
|||
for( var name = names.iterateNext(); name != null; name = names.iterateNext() ) { |
|||
namesArray.push( name ); |
|||
} |
|||
alert( namesArray ); |
|||
Although some browsers support XPath, working with XML is much easier with E4X. |
|||
//create XML object from file |
|||
var xhr = new XMLHttpRequest(); |
|||
xhr.open('GET', 'file.xml', false); |
|||
xhr.send(null); |
|||
var doc = new XML(xhr.responseText); |
|||
//get first <item> element |
|||
var firstItem = doc..item[0]; |
|||
alert( firstItem ); |
|||
//output contents of <price> elements |
|||
for each( var price in doc..price ) { |
|||
alert( price ); |
|||
} |
|||
//add <name> elements to array |
|||
var names = []; |
|||
for each( var name in doc..name ) { |
|||
names.push( name ); |
|||
} |
|||
alert( names ); |
|||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
use XML::XPath qw(); |
|||
my $x = XML::XPath->new('<inventory ... </inventory>'); |
|||
[$x->findnodes('//item[1]')->get_nodelist]->[0]; |
|||
print $x->findnodes_as_string('//price'); |
|||
$x->findnodes('//name')->get_nodelist; |
|||
==[[PHP]]== |
|||
[[Category:PHP]] |
|||
<?php |
|||
//PHP5 only example due to changes in XML extensions between version 4 and 5 (Tested on PHP5.2.0) |
|||
$doc = DOMDocument::loadXML('<inventory title="OmniCorp Store #45x10^3">...</inventory>'); |
|||
//Load from file instead with $doc = DOMDocument::load('filename'); |
|||
$xpath = new DOMXPath($doc); |
|||
/* |
|||
1st Task: Retrieve the first "item" element |
|||
*/ |
|||
$nodelist = $xpath->query('//item'); |
|||
$result = $nodelist->item(0); |
|||
/* |
|||
2nd task: Perform an action on each "price" element (print it out) |
|||
*/ |
|||
$nodelist = $xpath->query('//price'); |
|||
for($i = 0; $i < $nodelist->length; $i++) |
|||
{ |
|||
//print each price element in the DOMNodeList instance, $nodelist, as text/xml followed by a newline |
|||
print $doc->saveXML($nodelist->item($i))."\n"; |
|||
} |
|||
/* |
|||
3rd Task: Get an array of all the "name" elements |
|||
*/ |
|||
$nodelist = $xpath->query('//name'); |
|||
//our array to hold all the name elements, though in practice you'd probably not need to do this and simply use the DOMNodeList |
|||
$result = array(); |
|||
//a different way of iterating through the DOMNodeList |
|||
foreach($nodelist as $node) |
|||
{ |
|||
$result[] = $node; |
|||
} |
|||
==[[Ruby]]== |
|||
[[Category:Ruby]] |
|||
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html) |
|||
require "rexml/document" |
|||
include REXML |
|||
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string) |
|||
doc = Document.new( |
|||
%q@<inventory title="OmniCorp Store #45x10^3"> |
|||
... |
|||
</inventory> |
|||
@ |
|||
) |
|||
# The invisibility cream is the first <item> |
|||
invisibility = XPath.first( doc, "//item" ) |
|||
# Prints out all of the prices |
|||
XPath.each( doc, "//price") { |element| puts element.text } |
|||
# Gets an array of all of the "name" elements in the document. |
|||
names = XPath.match( doc, "//name" ) |