lunes, 10 de agosto de 2009

XPATH Cookbook

I have been working for 2 years with several XML related tecnology, (Web services, XML firewalls, etc) and during this time i have been collecting recipes to get information from XML documents with xpath. I finally put them here because every time I had to come back working with xml, I had to refresh my knowledge with the rules and syntax, again and again...

Basic Selections


Select a node in a tree
/person/name

Select the first element of a nodeset
/person/names[1]


Select an attribute of an predifined element

xpath
/root/saludo/@idioma

Select an attribute with namespace
/root/*[namespace-uri()='http://geronimo' and local-name()='saludo']/@idioma

Find an element in any part of the document named "entidad"
//*[local-name()='entidad']

Find an element named Body no matter what namespace you are using
/*[local-name()='Envelope']/*[local-name()='Body']/*

Find an element with the namespace named test.com and an element named element1
//*[namespace-uri()='http://test.com1' and local-name()='element1']

Find an all document an element whose namespace is "http://test.com"
//*[namespace-uri()='http://test.com1']

Function to emulate indexOf (java)
<pre><xsl:function name="eg:index-of" as="xs:integer">
<xsl:param name="a1" as="xs:string?">
<xsl:param name="a2" as="xs:string?">
<xsl:sequence select="string-length(substring-before($a1, $a2)">
</xsl:sequence>
</xsl:param></xsl:param></xsl:function></pre>

No hay comentarios:

Publicar un comentario