Author |
Message |
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Hi
I have this XML structure: <Doc> <Values> <value a="100" b="131" c="20" /> <value a="100" b="130" c="20" /> <value a="23" b="400" c="20" /> <value a="2" b="1" c="110" /> <value a="2" b="1" c="100" /> <value a="0" b="30" c="2" /> </Values> </Doc>
As you see the nodes are sorted in descending order by first "a", then "b" and last "c".
I now want to insert <value a="100" b="10" c="11" />. This new node should be inserted at position 3.
Is it possible to select the node from which the new node should be inserted after In this case <value a="100" b="130" c="20" />
/ Kind Regards
.NET Development28
|
|
|
|
|
MarcD
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Hi marcD
I forgot to say that I don't know the already saved values.
The only thing I know are the values of the node that is going to be inserted.
/ Kind Regards
|
|
|
|
|
MarcD
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Well you can replace the values with something like
"//value[a > " + NodeValue.Attributes["a"].InnerText + " and b > " +
That should select the first node it finds that haves values greater - assuming that's the logic. You also have or operators and etc.
|
|
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
How do you mean Only if one attribute is equal, the next attribute should be checked.
If I do
I get no node as a result.
/ Kind Regards
|
|
|
|
|
Dimitre_Novatchev
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
This XPath expression:
selects the wanted node.
Cheers, Dimitre Novatchev
|
|
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Yes, but that expression doesn't return the correct node for _any_ node input.
Assume you want to insert the node <value a="100" b="10" c="25" />. Then the second node should be the answer.
If the new node should be inserted at the first position, then XPath can never return the node which to insert after, because it doesn't exists.
So I think the only solution is to manually iterate through all <value> nodes to retrieve the correct index
/ Kind Regards
|
|
|
|
|
Dimitre_Novatchev
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
> Yes, but that expression doesn't return the correct node for _any_ node input.
> Assume you want to insert the node <value a="100" b="10" c="25" />. > Then the second node should be the answer.
> If the new node should be inserted at the first position, then XPath can never > return the node which to insert after, because it doesn't exists.
> So I think the only solution is to manually iterate through all <value> nodes to > retrieve the correct index
It seems to me that you're replying not to my message. Please, read my answer and indicate whether you really understand it. If necessary, refine the problem.
Cheers, Dimitre Novatchev
|
|
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Hi Dimitre That was a reply to your message.
But assume you want to insert the node <value a="100" b="10" c="25" />. Then your expression does not return the second node as the result.
/ Kind Regards
|
|
|
|
|
Dimitre_Novatchev
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Dear PublicError,
I think I understand now what you want.
One solution is to represent the values of all three attributes in one single number.
If for example, the values are guaranteed to be less than 1000, then this can be done using the following expression:
Then, the first node to insert after (in your case) will be selected by this XPath expression:
Hope this helped.
Cheers, Dimitre Novatchev
|
|
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Hi Dimitre Im glad you understand me, I've some problems expressing myself in English sometimes. :/
Yes, one solution is to represent all three attributes as one single number.
But then let's say that the attributes are sorted like: asc(a), desc(b), asc(c)
How do you do it then :)
/ Kind Regards
|
|
|
|
|
Chris Lovett
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
What if you simply append the node then run the following XSL transform to re-sort the whole thing:
< xml version="1.0" encoding="utf-8" > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <Doc> <Values> <xsl:apply-templates select="Doc/Values/value"> <xsl:sort select="
" order="ascending" data-type="number"/> <xsl:sort select="
" order="descending" data-type="number"/> <xsl:sort select="
" order="ascending" data-type="number"/> </xsl:apply-templates> </Values> </Doc> </xsl:template> <xsl:template match="value"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet>
This produces the following XML output:
< xml version="1.0" encoding="utf-8" > <Doc> <Values> <value a="0" b="30" c="2" /> <value a="2" b="1" c="100" /> <value a="2" b="1" c="110" /> <value a="23" b="400" c="20" /> <value a="100" b="131" c="20" /> <value a="100" b="130" c="20" /> <value a="100" b="10" c="11" /> </Values> </Doc>
|
|
|
|
|
Dimitre_Novatchev
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
> But then let's say that the attributes are sorted like: > asc(a), desc(b), asc(c)
> How do you do it then :)
Cheers, Dimitre Novatchev
P.S. As Chris Lovett has said, this task is trivially done in XSLT -- my understanding is you want a single XPath expression.
|
|
|
|
|
PublicError
|
Posted: XML and the .NET Framework, XPath - Get node |
Top |
Hi Thanks for your answers, Dimitre and Chris!
/ Kind Regards
|
|
|
|
|
|