Find element name via case-insensitive search - xpath

Given the following three pieces of XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<body>
<customerSearchRequest>
<id>1234</id>
</customerSearchRequest>
</body>
</root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<BODY>
<userSearchRequest>
<id>5678</id>
</userSearchRequest>
</BODY>
</root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<Body>
<orderSearchRequest>
<id>9101</id>
</orderSearchRequest>
</Body>
</root>
I need to extract the name of the first-child of body (i.e. customerSearchRequest, userSearchRequest and orderSearchRequest), which I am currently doing as follows:
name(//SOAP-ENV:body/*[1])
The problem is, this only works for the first request as body is case-sensitive. How do I make the path case-insensitive?
Thanks for any pointers.

I would go with this :
name(//SOAP-ENV:*[translate(local-name(),'BODY','body')='body']/*[1])
The following part first finds elements in namespace of SOAP-ENV, and then filter to those with local-name equals 'body', case-insensitive :
//SOAP-ENV:*[translate(local-name(),'BODY','body')='body']
If you have XPath 2.0 available, it can be done in a cleaner way using lower-case(), upper-case(), or regex based functions like matches() : case-insensitive matching in xpath?

Related

What's the difference between <filled> tag inside the <field> tag and out of it?

The <filled> tag can be written under <field> tag and also outside of the tag, under the a <form> tag.
So what's the difference between these two way of writing code?
And In which scenario this will be beneficial?
Example can be found in : http://help.voxeo.com/go/help/xml.vxml.tutorials.audio
If you write tag inside then it will be a particular handling for that tag.
If you write outside then it can be used for like multiple inputs manipulation that is caught under .
eg.
<?xml version="1.0" encoding="UTF-8"?>
<vxml version="2.1" xmlns="http://www.w3.org/2001/vxml">
<form id="get_starting_and_ending_cities">
<field name="start_city">
<grammar src="city.grxml"
type="application/srgs+xml"/>
<prompt>What is the starting city?</prompt>
</field>
<field name="end_city">
<grammar src="city.grxml"
type="application/srgs+xml"/>
<prompt>What is the ending city?</prompt>
</field>
<filled mode="all" namelist="start_city end_city">
<log><value expr="start_city"/></log>
<log><value expr="end_city"/></log>
<if cond="start_city == end_city">
<prompt>
You can't fly from and to the same city.
</prompt>
</if>
</filled>
</form>
</vxml>

Simplexml Insert into sub element

i have an xml file same bellow:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<prod id="1">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
</prod>
<prod id="2">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
</prod>
</data>
how to use simplexml to insert an item into layer id="layer_3" like bellow example:
<prod id="1">
<layer id="layer_0" imgSRC="data/361_layer_0.png"/>
<layer id="layer_1" imgSRC="data/362_layer_0.png"/>
<layer id="layer_2" imgSRC="data/363_layer_0.png"/>
<layer id="layer_3" imgSRC="data/364_layer_0.png"/>
</prod>
what do i have to do?
Thanks!!
(1) select the node that will be the parent of the new child, I'd go with xpath:
$xml = simplexml_load_string($x); // assuming XML in $x
$node = $xml->xpath("//prod[#id='2']/layer[#id='layer_2']")[0];
xpath will select all <layer> nodes with id=layer_2 that have <prod> with id=2 as a parent. In case there are several results, the [0] will select just the first one. This works with PHP >= 5.4.
see this working: http://3v4l.org/5Q46F
(2) then add the child and its attributes, see example #10 in the simplexml manual:
http://www.php.net/manual/en/simplexml.examples-basic.php

How do I search a node with namespaces defined in the same node

If ancestor nodes defines namespaces, I can use them:
> Nokogiri::XML(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:creator opf:role="aut">John Doe</dc:creator>
</metadata>
</package>
XML
> xml.at_xpath("//dc:creator[#opf:role='aut']", xml.at_xpath("//xmlns:metadata").namespaces).text
=> "John Doe"
However, what shall I do with following XML?
> Nokogiri::XML(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
<metadata>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf" opf:role="aut">John Doe</dc:creator>
</metadata>
</package>
XML
> xml.at_xpath("//dc:creator[#opf:role='aut']", xml.at_xpath("//xmlns:metadata").namespaces).text
Nokogiri::XML::XPath::SyntaxError: Undefined namespace prefix: //dc:creator[#opf:role='aut']
I think xml.remove_namespaces! or literal namespace arguments for at_xpath is last resort.
To programmatically collect all the namespaces, use Document#collect_namespaces.
xml = Nokogiri::XML(xmldata)
ns = xml.collect_namespaces
puts xml.at('//dc:creator[#opf:role="aut"]', ns).text
Output:
John Doe

Xpath: Select node but not specific child elements

I have a structure similar to the following:
<page id='1'>
<title>Page 1</title>
<page id='2'>
<title>Sub Page 1</title>
</page>
<page id='3'>
<title>Sub Page 2</title>
</page>
</page>
<page id='4'>
<title>Page 2</title>
</page>
I need to select a page by Id but if that page has descendant pages I don't want to return those elements, but I do want the other elements of that page. If I select Page 1 I want to return title but not the child pages...
//page[#id=1]
The above gets me page 1, but how do I exclude the sub pages? Also, There could be any arbitrary number of elements in a page.
//page[#id=1]/*[not(self::page)]
I have found that this gets me the data I want. However, that data comes back as an array of objects with one object per element and apparently excludes the element names???. I am using PHP SimpleXML for what it is worth.
Use:
//page[#id=$yourId]/node()[not(self::page)]
This selects all nodes that are not page and that are children of any page in the document, the string value of whose id attribute is equal to the string contained in $yourId (most probably you would substitute $yourId above with a specific, desired string, such as '1').
Here is a simple XSLT-based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pId" select="3"/>
<xsl:template match="/">
<xsl:copy-of select="//page[#id=$pId]/node()[not(self::page)]"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document (wrapped in a single top node to make it well-formed):
<pages>
<page id='1'>
<title>Page 1</title>
<page id='2'>
<title>Sub Page 1</title>
</page>
<page id='3'>
<title>Sub Page 2</title>
</page>
</page>
<page id='4'>
<title>Page 2</title>
</page>
</pages>
the wanted, correct result is produced:
<title>Sub Page 2</title>
Do note: One assumption made is that an id value uniquely identifies a page. If this is not so, the proposed XPath expression will select all page elements whose id attribute has a string valu of $yourId.
If this is the case and only one page element must be selected, the OP must specify which one of the many page elements with this id should be selected.
For example, it may be the first:
(//page[#id=$yourId]/node()[not(self::page)])[1]
or the last:
(//page[#id=$yourId]/node()[not(self::page)])[last()]
or ...
If you're only interested in the title element, this would work:
//page[#id=1]/title
If however you need other sub elements of page, I'm not sure XPath is the right tool for you.
Sounds more like something that an XSLT would be suited for, since what you are really doing is transforming your data.
If the page always has a title:
//page[#id='1']/*[not(boolean(./title))]

XPath query for empty namespace xmlns=""

What XPath do I use to query the info node in the xml below? I've tried different expressions in XMLSpy but nothing works.
<root xmlns="tempuri.org" xmlns:p="http://nonamespace.org/std/Name/2006-10-18/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item xmlns="">
<info>blah blah</info>
<date>2009-07-27 00:00:00</date>
</item>
you can do it like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="tempuri.org">
<xsl:template match="/">
<xsl:value-of select="a:root/item/info"/>
</xsl:template>

Resources