Select parent nodes in XML

Grzesiek23 used Ask the Experts™
I wrote the xmld code below.

I want to get all the parent nodes by entering specify URL as parameter.

I mean - I have the category ID = 17, so the url will be:

Now I want to select this element (ID, title) and also parent nodes, so:
- "~/forum" | Strona glowna forum
---- "~/forum-c-11" | Gospodarka
-------- "~/forum-c-17" | PKO BP

How can I do this?

After get it I need to format these elements iinto string with anchors links.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="" >
    <siteMapNode url="~/forum" title="Strona glowna forum">
      <siteMapNode url="~/forum-c-1" title="Ogólne"/>
      <siteMapNode url="~/forum-c-3" title="Spoleczenstwo"/>
      <siteMapNode url="~/forum-c-4" title="Wojsko"/>
      <siteMapNode url="~/forum-c-5" title="Parlament"/>
      <siteMapNode url="~/forum-c-6" title="Rzadowe dyskusje"/>
      <siteMapNode url="~/forum-c-7" title="Uwagi i propozycje do administracji"/>
      <siteMapNode url="~/forum-c-8" title="Wojsko Polskie"/>
      <siteMapNode url="~/forum-c-10" title="Informacje"/>
      <siteMapNode url="~/forum-c-11" title="Gospodarka">
        <siteMapNode url="~/forum-c-17" title="PKO BP"/>
      <siteMapNode url="~/forum-c-12" title="Polityka">
        <siteMapNode url="~/forum-c-14" title="Kandydatury"/>
      <siteMapNode url="~/forum-c-13" title="Moderatorzy"/>

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
You have not specified your preferred language. Here is a version in asp. Even if you use some other language, the same logic would be used.
<%@ Language=VBScript %>
<%option explicit%>
	Dim oXMLDom
	Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument")
	Dim bLoaded
	bLoaded = oXMLDom.Load(Server.MapPath("TheXMLFile.xml"))
	Dim i
	Dim oNodes, oNode
	Dim sXPathQuery
	sXPathQuery = "//siteMapNode[@url='~/forum-c-17']"
	set oNode = oXMLDom.documentElement.selectSingleNode(sXPathQuery)
	if not (oNode is nothing) then
		Response.Write oNode.getAttribute("url") & "|" & oNode.getAttribute("title") & "<br>"
		while not oNode.parentNode is nothing and oNode.parentNode.NodeType = 1
			Set oNode = oNode.parentNode
			Response.Write oNode.getAttribute("url") & "|" & oNode.getAttribute("title") & "<br>"
	end if
	set oXMLDom = nothing

Open in new window

Tony McCreathTechnical SEO Consultant
Here's an XPATH that gets the nodes you want:
//siteMapNode[.//*[@url='~/forum-c-17'] or @url='~/forum-c-17']

Open in new window

Tony McCreathTechnical SEO Consultant

p.s. I removed the namespace from the sample XML to make it work as is. If you want to support namespaces you will have to refer to the particular XPATH implementation on how to do it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial