Hello,

I need to return the areas connected with the town placeid attribute of an XML document. The town placeid is a variable equal to a current page variable $x.

e.g.
xpath("/country/city/town[@placeid=$x]");

How can I return the areas for each using simpleXML/XPATH?


<country>

<city>
<town placeid="" />

<areas>
<area>A</area>
<area>B</area>
<area>C</area>
<areas>


</city>

</country>

Thanks!

I'm not sure if I understood your requirement correctly, but have posted the below with an assumption:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="xml"/>

<xsl:variable name="x" select="'Plano'"/>

<xsl:template match="/">
	
			<xsl:for-each select="//town[@placeid = $x]">
				<xsl:copy-of select="following-sibling::areas[1]"/>
			</xsl:for-each>
			
    </xsl:template>

</xsl:stylesheet>

If thats not what you want, please post a minimal input XML and the required output XML.

Thanks Mrame, your answer has greatly helped.

I'm using simpleXML/ XPATH/ PHP to pull the contents of the XML (I'm not using XSL). I've got as far as printing the array - The following code prints this:

$area = $xml->xpath("/country/city/town[@placeid=$x]/following-sibling::areas[1]");
print_r($area);

and outputs:
Array ( [0] => SimpleXMLElement Object ( [area] => Array ( [0] => A [1] => B [2] => C ) ) )

but how do I only print the area content (with a line break or <br/> if possible) e.g.
A
B
C

Any help would be great.
Thanks

I really don't have any idea about PHP. From an assumption I think $area has

<areas>
<area>A</area>
<area>B</area>
<area>C</area>
</areas>

in its memory. So use for-each to work on each <area> (for-each areas/area) and get the value in separate line.

I think this will work.

Thanks Mrame! foreach worked, really appreciate your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.