How Can I retrieve parent tag while I have a child tag in XML? | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| |
Hi!!!!!!!!
All,
I am facing a problem to retrieving parent node. I have Child node already
I am using Xpath.
Here is my XML:::::: -
<book>
-
<chapters>
-
<chapter>
-
<title>The City</title>
-
<position>1</position>
-
<page>1</page>
-
</chapter>
-
-
<chapter>
-
<title>The village</title>
-
<position>2</position>
-
<page>15</page>
-
</chapter>
-
</chapters>
-
-
</book>
-
In this xml first I retrive the Title of the both Chapter tag. using Xpath -
XmlDocument xdoc=new XmlDocument;
-
xdoc.Load("\\Storage Card\\book.xml");
-
XmlNodeList xnl;
-
xnl = xdoc.SelectNodes("book/chapters/chapter/title ");
-
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 -
xl = xdoc.SelectNodes("book/chapters/chapter/title ");
-
for (int j = 0; j <= xl.Count - 1; j++)
-
{
-
if (xl.Item(j).InnerText == title)
-
{
-
xpath1 = "book/chapters/.." + x.Item(j) + "pargraphs/pargraph/title";
-
}
-
}
-
If there is any other way to do this then also tell me.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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 - xpath = "following-sibling::*";
if that’s not the case - xpath = "../*[local-name() != 'title']";
provided you start from the title node
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML? Quote:
Originally Posted by Kapil Choubisa 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) - <?xml version="1.0" encoding="UTF-8" ?>
-
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl:output method="text"/>
-
<xsl:variable name="test" select="/book/chapters/chapter/title"/>
-
-
<xsl:template match="/">
-
<xsl:for-each select="$test">
-
<xsl:for-each select="following-sibling::*">
-
<xsl:value-of select="./text()"/>
-
<xsl:text> </xsl:text>
-
</xsl:for-each>
-
</xsl:for-each>
-
</xsl:template>
-
-
</xsl:stylesheet>
gives | | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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. -
-
xpath1 = "following-sibling::chapters/" + xl.Item(j) + "*";
-
-
-
xpath1 = "//chapter/following-sibling::position";
-
-
-
xpath1 = "following-sibling::*";
-
//display all siblings doesn't matter that are in Chapter tag or not
-
-
-
xpath1="following-sibling"; //display none
-
-
-
xpath1 = "book/chapters/.." + xl.Item(j) + "position";
-
//error occurs
-
-
-
xpath1 = "book/chapters/" + xl.Item(j)+ "../position";
-
//display none
-
-
-
xpath1 = "book/chapters/chapter/position";
-
//Display all siblings from all the chapter tags
-
-
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.........
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML?
simply add a condition - self::title[text() == $title_name]/following-sibling::*
-
// or going from root
-
/book/chapters/chapter/title[text() == $title_name]/following-sibling::*
comments to the XPaths - // elements do not exist
-
xpath1 = "following-sibling::chapters/" + xl.Item(j) + "*";
-
-
// elements do not exist
-
xpath1 = "//chapter/following-sibling::position";
-
-
// depends from where you start
-
xpath1 = "following-sibling::*";
-
-
// element does not exist at all
-
xpath1="following-sibling";
-
-
// position not child of chapters
-
xpath1 = "book/chapters/" + xl.Item(j)+ "../position";
-
-
// gives a node list
-
xpath1 = "book/chapters/chapter/position";
to get one specified element - //position[preceding-sibling::title/text() = $title_name]
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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 -
XmlNodeList obj;
-
obj=XmlDocumentobject.SelectNode("book/chapters/chapter/title");
-
obj.Item().Innertext;
-
//It return the tag Value like <Title>The City</Title> than The City
-
//can I get the Tag Text by
-
obj.Item(); //or by any other way.
-
I have to get Parent Node only Then I will get othe childs easily.
Help me plz
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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 -
//Expression must evaluate to a node-set.
-
xpath1 = "book/chapters/chapter/title[text() == $title]/following-sibling::*";
-
//Expression must evaluate to a node-set.
-
xpath1 = "self::title[text() == $title_name]/following-sibling::*";
-
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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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. -
-
for (int j = 0; j <= xnl1.Count - 1; j++)
-
{
-
if (xl.Item(j).InnerText == title)
-
{
-
xpath1 = "book/chapters/chapter/title[text() == $title]/following-sibling::*";
-
break;
-
-
}
-
}
-
-
xl1 = xdoc.SelectNodes(xpath1);
-
label1.Text = xl1.Item(0).InnerText;
-
Please help me
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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 - xpath1 = "/book/chapters/chapter/title[text() = "+title+"]/following-sibling::*";
-
xl1 = xdoc.SelectNodes(xpath1);
-
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML? Quote:
Originally Posted by Kapil Choubisa 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
| | | re: How Can I retrieve parent tag while I have a child tag in XML? -
xpath1 = "/book/chapters/chapter/title[text() = "+title+"]/following-sibling::*";
-
xl1 = xdoc.SelectNodes(xpath1);
-
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.....
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML?
ok, I forgot to quote the string. - xpath1 = "/book/chapters/chapter/title[text() = '"+title+"']/following-sibling::*";
-
xl1 = xdoc.SelectNodes(xpath1);
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML? Quote:
Originally Posted by Kapil Choubisa 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
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML? Quote:
Originally Posted by Kapil Choubisa 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
| | | 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | re: How Can I retrieve parent tag while I have a child tag in XML?
Yeh!
Dear I am from India
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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.........
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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
| | | 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
| | | 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 -
xpath1 = "book/chapters/chapter/title[text() = " + "'title'" + "]/following-sibling::*";
-
what is error in this syntax fot that this error is comming
while retriving the Xpath -
xl1 = xdoc.SelectNodes(xpath1);
-
-
label1.Text = xl1.Item(0).InnerText;
-
the error is comming on the last line.
Innertext of xl1.Item(0).InnerText; is causing the exception
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | re: How Can I retrieve parent tag while I have a child tag in XML? Quote:
Originally Posted by Kapil Choubisa -
xpath1 = "book/chapters/chapter/title[text() = " + "'title'" + "]/following-sibling::*";
-
not sure whether that is correct. better stick with the tried original.
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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
| | | 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>
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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 - /books/chapters/chapter/*[preceding-sibling::title/text() = '_title_' or following-sibling::title/text() = '_title']
-
-
/books/chapters/chapter[title/text() = '_title_']/*[local-name() != 'title']
-
-
// match a single one
-
/books/chapters/chapter[title/text() = '_title_']/position
| | Member | | Join Date: Sep 2009 Location: Sunvillage
Posts: 42
| | | 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
| | | 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 -
<paragraph>
-
<position>1</position>
-
<title>Title</title>
-
<text>
-
<![CDATA[<p style="text-align: center;"><span style="color: #800000;"><span style="font-size: xx-large;">Wings of Pride </span>
-
</span><br /><br /><span style="color: #800000;">Adnan Kërçagu </span><br /><span style="font-size: small;">
-
Love is stronger than the fear compassion is the true source of the tear
-
<br />
-
care for the one who is yours, for the one who is dear there is nothing that’s impossible to bear </span>
-
<br />
-
<br /><span style="font-size: small;"><em><span style="color: #000080;">Special thanks for great support to my brother: Fatan Kërçagu</span></em></span>
-
</p>
-
<p style="text-align: center;"><span style="font-size: small;"><em>
-
<span style="color: #000080;"><br /></span></em></span>
-
</p>]]>
-
-
-
</text>
-
-
</paragraph>
-
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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,661
| | | 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?
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|