Select parent nodes in XML

Grzesiek23
Grzesiek23 used Ask the Experts™
on
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:
"~/forum-c-17"

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="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <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>
      <siteMapNode url="~/forum-c-12" title="Polityka">
        <siteMapNode url="~/forum-c-14" title="Kandydatury"/>
      </siteMapNode>
      <siteMapNode url="~/forum-c-13" title="Moderatorzy"/>
    </siteMapNode>
</siteMap>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
Commented:
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%>
<html>
 
<body>
<%
	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>"
		wend
	end if
 
	set oXMLDom = nothing
%>
 
</body>
 
</html>

Open in new window

Tony McCreathTechnical SEO Consultant
Commented:
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

Commented:
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