Connecting Tech Pros Worldwide Forums | Help | Site Map

How Can I retrieve parent tag while I have a child tag in XML?

Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#1: Sep 10 '09
Hi!!!!!!!!
All,

I am facing a problem to retrieving parent node. I have Child node already
I am using Xpath.
Here is my XML::::::

Expand|Select|Wrap|Line Numbers
  1. <book>
  2.          <chapters>
  3.                  <chapter>
  4.                         <title>The City</title>
  5.                         <position>1</position>
  6.                         <page>1</page>
  7.                  </chapter>
  8.  
  9.                  <chapter>
  10.                         <title>The village</title>
  11.                         <position>2</position>
  12.                         <page>15</page>
  13.                  </chapter>
  14. </chapters>
  15.  
  16. </book>
  17.  
In this xml first I retrive the Title of the both Chapter tag. using Xpath

Expand|Select|Wrap|Line Numbers
  1. XmlDocument xdoc=new XmlDocument; 
  2. xdoc.Load("\\Storage Card\\book.xml");
  3.             XmlNodeList xnl;
  4.             xnl = xdoc.SelectNodes("book/chapters/chapter/title ");
  5.  
Now I need that, When I click the first Title the other childs of the same chapter of the title must display.

for that purpose I m thinking that I get first the parent of the clicked title and then retrive the other childs (position, page).

May be that I m going in wrong way. Plz help me and give the solution of this problem
Ignore my may be silly code.
I m trying this in by this xpath. but its showing me error
Expand|Select|Wrap|Line Numbers
  1.  xl = xdoc.SelectNodes("book/chapters/chapter/title ");
  2.             for (int j = 0; j <= xl.Count - 1; j++)
  3.             {
  4.                 if (xl.Item(j).InnerText == title)
  5.                 {
  6.                     xpath1 = "book/chapters/.." + x.Item(j) + "pargraphs/pargraph/title";
  7.                 }
  8.             }
  9.  
If there is any other way to do this then also tell me.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#2: Sep 10 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


if <title> is always the first child you can use
Expand|Select|Wrap|Line Numbers
  1. xpath = "following-sibling::*";
if that’s not the case
Expand|Select|Wrap|Line Numbers
  1. xpath = "../*[local-name() != 'title']";
provided you start from the title node
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#3: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Thank you dear but its not working.
Its not giving me the sublings.
Is there any other way?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#4: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Quote:

Originally Posted by Kapil Choubisa View Post

Thank you dear but its not working.
Its not giving me the sublings.

I don’t know, what you did, but I could confirm the correctness of the XPath (using XSLT)
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="text"/>
  4. <xsl:variable name="test" select="/book/chapters/chapter/title"/>
  5.  
  6. <xsl:template match="/">
  7.     <xsl:for-each select="$test">
  8.         <xsl:for-each select="following-sibling::*">
  9.             <xsl:value-of select="./text()"/>
  10.             <xsl:text>&#10;</xsl:text>
  11.         </xsl:for-each>
  12.     </xsl:for-each>
  13. </xsl:template>
  14.  
  15. </xsl:stylesheet>
gives
Expand|Select|Wrap|Line Numbers
  1. 1
  2. 1
  3. 2
  4. 15
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#5: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


I am Sorry. May be possible that I was unable to explain. But I require that while I click on the first title means "The City" then I need the siblings of the same chapter. Means I need the position 1and page no 1
like:

The City
//on Click
1
1

When I click on Village is will be

The Village
2
15

for that I tried some of your suggestion
and someting other but all is failed
These all are different syntax for Xpath I used.
Expand|Select|Wrap|Line Numbers
  1.  
  2. xpath1 = "following-sibling::chapters/" + xl.Item(j) + "*";
  3.  
  4.  
  5. xpath1 = "//chapter/following-sibling::position";
  6.  
  7.  
  8. xpath1 = "following-sibling::*";                   
  9.  //display all siblings doesn't matter that are in Chapter tag or not 
  10.  
  11.  
  12. xpath1="following-sibling";                          //display none
  13.  
  14.  
  15. xpath1 = "book/chapters/.." + xl.Item(j) + "position";
  16.                 //error occurs
  17.  
  18.  
  19. xpath1 = "book/chapters/" + xl.Item(j)+ "../position";
  20.                //display none
  21.  
  22.  
  23.  xpath1 = "book/chapters/chapter/position";
  24.               //Display all siblings from all the chapter tags
  25.  
  26.  
here the xl.Item is the item property of my XmlNodeList object xl.
j is a loop variable. So don't get confused.


Please help me to find the position and page no while I have the title.
Remind that I don't have the ID of chapter tag. Plz help me.

Now I think that I had explained that what I needed.

Thank you.........
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#6: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


simply add a condition
Expand|Select|Wrap|Line Numbers
  1. self::title[text() == $title_name]/following-sibling::*
  2. // or going from root
  3. /book/chapters/chapter/title[text() == $title_name]/following-sibling::*
comments to the XPaths
Expand|Select|Wrap|Line Numbers
  1. // elements do not exist
  2. xpath1 = "following-sibling::chapters/" + xl.Item(j) + "*";
  3.  
  4. // elements do not exist
  5. xpath1 = "//chapter/following-sibling::position";
  6.  
  7. // depends from where you start
  8. xpath1 = "following-sibling::*";                   
  9.  
  10. // element does not exist at all
  11. xpath1="following-sibling";
  12.  
  13. // position not child of chapters
  14. xpath1 = "book/chapters/" + xl.Item(j)+ "../position";
  15.  
  16. // gives a node list
  17. xpath1 = "book/chapters/chapter/position";
to get one specified element
Expand|Select|Wrap|Line Numbers
  1. //position[preceding-sibling::title/text() = $title_name]
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#7: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


If you can tell me that how can I get a tag text in a variable then also it makes easy to solve my problem. My mean with the tag text is
<chapter> chapter
<title> title
like this.
while I m using XmlNodeList than can I get the tag name with
Expand|Select|Wrap|Line Numbers
  1. XmlNodeList obj;
  2. obj=XmlDocumentobject.SelectNode("book/chapters/chapter/title");
  3. obj.Item().Innertext;
  4.  //It return the tag Value like <Title>The City</Title> than The City
  5. //can I get the Tag Text by
  6. obj.Item(); //or by any other way.
  7.  
I have to get Parent Node only Then I will get othe childs easily.
Help me plz
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#8: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


check out the XPath functions. esp name() (with NS prefix) and local-name(). ref.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#9: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


i am trying to show iamge from image folder but it can done if i do same thing with storage card then it is done but when i create cab file for instalation i get error file not found what i can do plz
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#10: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


what function to find image folder for view in C# for Window Mobile Application like server.mappath for computer
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#11: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


that’s C# stuff I don’t know.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#12: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


In your last to previous suggestion while I gives it to the xpath it generatis an exception
Expand|Select|Wrap|Line Numbers
  1. //Expression must evaluate to a node-set.
  2. xpath1 = "book/chapters/chapter/title[text() == $title]/following-sibling::*";
  3. //Expression must evaluate to a node-set.
  4. xpath1 = "self::title[text() == $title_name]/following-sibling::*";
  5.  
Should i make any change in it or I can Directly Access it by the same as you suggest.
You said that this are conditions. How can I call them.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#13: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


of course you have to replace $title/$title_name with your own values (otherwise it assumes non-existing variables).
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#14: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


I am calling it in this manner. I don't know that I am doing right aur not.
Can you tell me. how to do it.
xnl1 and xl are the objects of XmlNodeList.
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int j = 0; j <= xnl1.Count - 1; j++)
  3.             {
  4.                 if (xl.Item(j).InnerText == title)
  5.                 {
  6.                     xpath1 = "book/chapters/chapter/title[text() == $title]/following-sibling::*";
  7.                      break;
  8.  
  9.                 }
  10.             }
  11.  
  12.           xl1 = xdoc.SelectNodes(xpath1);
  13.             label1.Text = xl1.Item(0).InnerText;
  14.  
Please help me
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#15: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


you don’t need that loop. you can access the nodes directly
Expand|Select|Wrap|Line Numbers
  1. xpath1 = "/book/chapters/chapter/title[text() = "+title+"]/following-sibling::*";
  2. xl1 = xdoc.SelectNodes(xpath1);
  3.  
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#16: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


I had replaced $title/$title_name with my tag name title but it generates the same exception.
Should i use it as a variable?
If yes then this will be exist outside" "?
and if not like a variable than I have to wirte Tag name. Then Which tag?
The tag which value I has or The tags which I am going to retrive.

And can you suggest me that can write this conditions in the loop which I send you before or I have to use it just outside?
I understand that here $title_name will be replace but which tag will replace it.
The Position tag or the title tag in my xml.
And what will I do for retriving the Two values from the same(pageno,position).

Please help me.

And Thank you a lot for giving your Important time to me.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#17: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Quote:

Originally Posted by Kapil Choubisa View Post

I had replaced $title/$title_name with my tag name title but it generates the same exception.

there is no == in XSL, there is also no assignment operator (it’s not a programming language (though it comes close)).
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#18: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Expand|Select|Wrap|Line Numbers
  1.  xpath1 = "/book/chapters/chapter/title[text() = "+title+"]/following-sibling::*";
  2.  xl1 = xdoc.SelectNodes(xpath1);
  3.  
Its Generating a exception that Null Reference is not set.
here the variable title is used for the value of the title tag.
I used a local variable named title. Which keeps the value of title tag.
it is either
"The City"
or
"The Village"
Am I passing the right Value?
If yes thenWhy its is showing me the exception Can you tell me?
Please.....
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#19: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


ok, I forgot to quote the string.
Expand|Select|Wrap|Line Numbers
  1. xpath1 = "/book/chapters/chapter/title[text() = '"+title+"']/following-sibling::*";
  2.  xl1 = xdoc.SelectNodes(xpath1);
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#20: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Thanx for giving me solutions but its again the same exception
" Object reference not set to an instance of an object."

The " 'title' " keeps what?
yhe title tag or the values "The City"/"The Village".

I m sorry for asking you may be silly questions but I am Very new that's why?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#21: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Quote:

Originally Posted by Kapil Choubisa View Post

The " 'title' " keeps what?
yhe title tag or the values "The City"/"The Village".

the title value (the latter ones).

the exception stuff seems to be caused from the C# side, for I didn’t have problems with the XPath.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#22: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Ok Dear Thanks A lot for Helping Me.
I will try myself more and then If I get problem I will ask to you.
Can I ask you more if I needed?

Can I know that from where you are?
It doen't matter but it will be good for me to know about my helper
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#23: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Quote:

Originally Posted by Kapil Choubisa View Post

Can I know that from where you are?
It doen't matter but it will be good for me to know about my helper

well, I don’t keep it hidden…
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#24: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


oh, I m really very Sorry. I see it now. Yow are from Germany.
Great............
Thanx budy for helping me.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#25: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


And you are from India.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#26: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Yeh!
Dear I am from India
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#27: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


near Bombay?


–––––
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#28: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


No, Its far from Here. had you ever come India?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#29: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


nope, neither Bombay nor Rajasthan.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#30: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Oh! Come Here Its a good place. I know that Germany is also a very good Country but Come to rajasthan Its realy very nice.
Whenever you decide to come here tell me. I will surely meet you.........
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#31: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


then I should print out your photo and ask the people if they have seen this guy.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#32: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


No, You can tell me on my mail ID and I will Come there and we will meet each other
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#33: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Its Not working, Perfectly.
It still showing me an exception that object reference is not set for an instance of anobject.

while I am usinig the condition
Expand|Select|Wrap|Line Numbers
  1. xpath1 = "book/chapters/chapter/title[text() = " + "'title'" + "]/following-sibling::*";
  2.  
what is error in this syntax fot that this error is comming
while retriving the Xpath
Expand|Select|Wrap|Line Numbers
  1. xl1 = xdoc.SelectNodes(xpath1);
  2.  
  3.             label1.Text = xl1.Item(0).InnerText;
  4.  
the error is comming on the last line.
Innertext of xl1.Item(0).InnerText; is causing the exception
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#34: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Quote:

Originally Posted by Kapil Choubisa View Post

Expand|Select|Wrap|Line Numbers
  1. xpath1 = "book/chapters/chapter/title[text() = " + "'title'" + "]/following-sibling::*";
  2.  

not sure whether that is correct. better stick with the tried original.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#35: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


will It work if my title tag is not the first one..................
In my application the Title tag is the second one
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#36: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Hey Please tell me That will it work in the case of the other tag is the first one then the <title>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#37: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Sorry, but from the XPath point of view, it is correct. what C# does with it, is, bluntly speaking, out of my control or ability.

you may also try the parent child XPath
Expand|Select|Wrap|Line Numbers
  1. /books/chapters/chapter/*[preceding-sibling::title/text() = '_title_' or following-sibling::title/text() = '_title']
  2.  
  3. /books/chapters/chapter[title/text() = '_title_']/*[local-name() != 'title']
  4.  
  5. // match a single one
  6. /books/chapters/chapter[title/text() = '_title_']/position
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#38: Sep 11 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Thank you dear.
I had done It.
Thank you very much for this Great Support and Help.
Member
 
Join Date: Sep 2009
Location: Sunvillage
Posts: 42
#39: Sep 12 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


Hi! Now I think that you understand that what am I going to do for?
I want to create a book reader.
Here I got the Inner most tag
but now in the tag HTML contents are in the CDATA Section. How can I read this CDATA Section by using XmlNodeList or is there any other way to do this?

my XML is
Expand|Select|Wrap|Line Numbers
  1. <paragraph>
  2.           <position>1</position>
  3.           <title>Title</title>
  4.           <text>
  5.             <![CDATA[<p style="text-align: center;"><span style="color:     #800000;"><span style="font-size: xx-large;">Wings of Pride </span>
  6. </span><br /><br /><span style="color: #800000;">Adnan K&euml;r&ccedil;agu </span><br /><span style="font-size: small;">
  7. Love is stronger than the fear compassion is the true source of the tear 
  8. <br />
  9. care for the one who is yours, for the one who is dear there is nothing that&rsquo;s impossible to bear &nbsp;</span>
  10. <br />
  11. <br /><span style="font-size: small;"><em><span style="color: #000080;">Special thanks for great support to my brother: Fatan K&euml;r&ccedil;agu</span></em></span>
  12. </p>
  13. <p style="text-align: center;"><span style="font-size: small;"><em>
  14. <span style="color: #000080;"><br /></span></em></span>
  15. </p>]]>
  16.  
  17.  
  18.            </text>
  19.  
  20. </paragraph>
  21.  
I have the title tag of this XML file.
now I want to read the html information of the CDATA section of <text> tag.
Remind it that it is a XML file.

How can I do it Please tell me.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#40: Sep 13 '09

re: How Can I retrieve parent tag while I have a child tag in XML?


wouldn’t it be easier to use XHTML (or HTML 5) and throw in some XML? or even use DocBook XML?
Reply