Jump to content

xPath


eskaflone

Recommended Posts

<?xml version="1.0" encoding="windows-1251" ?> - <форма uid="RPT02" name="основная форма отправления электронной отчетности непонятно по какому предмету отчетности">- <учреждение uid="1" name="контора номер 1">+ <таблица uid="1">  <строка uid="010" u1="17" u2="2795929" u3="618767" u4=".../>   <строка uid="020" u1="17" u2="3241285" u3="6444042" u4=".../> ............./>   </таблица>- <субтаблица uid="6">  <строка uid="010" u1="2631732" u2="117000000554317" u3="3165361" .... />   <строка uid="020" u1="5858274" u2="217000000554317" u3="482759" .../>

1)if in first table(таблица uid=1) in first row (строка Uid=010) second attribute(u1) equals to 17

number(/форма/учреждение/таблица[@uid=1]/строка[@uid=010]/attribute::u1)

returns 172)if in table (субтаблица uid=6) in row (строка uid=010) attribute(u2) starts with 117

/форма/учреждение/субтаблица[@uid=6]/строка[@uid=010 and start-with(@u2, "117")]

returns row: <строка uid="010" u1="2631732" u2="117000000554317" u3="3165361" .... /> How must I write xPath expression to extract row(строка uid=020) from (субтаблица Uid=6) if 1) and 2) are true?Can this be done using xPath?Code is written in c#Please, ever if you don't know c# write xPath expression in any lang.

Link to comment
Share on other sites

Something like this maybe?

/форма/учреждение/субтаблица[@uid=6]/строка[@uid=020][number(/форма/учреждение/таблица[@uid=1]/строка[@uid=010]/attribute::u1) = 17]

to select the 020 строка if the u1 attribute of the 010 строка equals 17.For that other one, I'm not sure I follow. You want to select this строка if the extracted node from the second expression is that exact one, if it contains all same values, or if a certain value is the same (as with the first)? If it's the last, it's the easiest - just wrap up the whole query in the "string()" function, then "= 'the value you want to match'". Practically what I've done above, but use string() instead of number() if the value is not numeric.Btw, I must say, I've never really saw a russian use the windows-1251 charset. I thought russians use the KOIR-8 charset more often and that we bulgarians use the Windows-1251 more often.Either way, I'd advise you to migrate to UTF-8 as a charset before it's too late. Don't forget to save the XML file as a UTF-8 too, not just change the XML prolog. UTF-8 is one of the most widely supported charsets and has great internationalization capabilities. I've used it to hold latin and cyrilic characters for ages and since it's always supported, I've never had problems with it.

Link to comment
Share on other sites

of course it works, sorry for that easy question.now I have another question,in this case if I have 2 or 3 or more tables (учреждение uid=somenumber) how would it work?

/форма/учреждение/субтаблица[@uid=6]/строка[@uid=020]

number(/форма/учреждение/таблица[@uid=1]/строка[@uid=010]/attribute::u1) = 17

in first and second expressions are independent from each other.

Link to comment
Share on other sites

can somebody point me into documentation about using xPath2.0 queries in c#(or other languages)

			xIter  = (XPathNodeIterator)xNav.Evaluate("for $x in /root return $x/a");

  <?xml version="1.0" ?> - <root>  <a u1="7" u2="2">1</a>   <a>3</a>   <a>2</a>   </root>

for example how to do this query

Link to comment
Share on other sites

Again, I don't follow what you're really asking (about the expression I mean). Are you asking how to do something or are you asking how the last one worked?If the later, it worked because the first predicate is first evaluated and returns a node-set (in this case, that's just one element really). The second predicate is then evaluated and if that node matches the test in it, it's returned. Needless to say the argument of the number() function is another XPath expression and that it's value is then compared to the right one, which in this case is 17.The only way you can do XSLT 2 processing and XPath 2.0/XQuery 1.0 in C# is by getting an XSLT 2.0 processors. They come with XPath 2.0 and XQuery 1.0 processors too.The two most popular XSLT 2 processors are SAXON and AltovaXML.

Link to comment
Share on other sites

for example I have file:

<?xml version="1.0" encoding="windows-1251" ?> - <форма uid="RPT02" name="основная форма отправления электронной отчетности непонятно по какому предмету отчетности">- <учреждение uid="1" name="контора номер 1">+ <таблица uid="1">  <строка uid="010" u1="17" u2="2795929" u3="618767" u4=".../> ............./>   </таблица>- <субтаблица uid="6">  <строка uid="010" u1="2631732" u2="117000000554317" u3="3165361" .... />   </субтаблица>- <учреждение uid="2" name="контора номер 2">+ <таблица uid="1">  <строка uid="010" u1="17" u2="2795929" u3="618767" u4=".../> ............./>   </таблица>- <субтаблица uid="6">  <строка uid="010" u1="2631732" u2="117000000554317" u3="3165361" .... />

the expression:

number(/форма/учреждение/таблица[@uid=1]/строка[@uid=010]/attribute::u1) = 17

would not work correctly, because it returns two nodes.So in xPath2.0, as I understand I should use {for} to solve this problem, and in xPath1.0 how could this be done?

Link to comment
Share on other sites

the expression:
number(/форма/учреждение/таблица[@uid=1]/строка[@uid=010]/attribute::u1) = 17

would not work correctly, because it returns two nodes.

No. What this always returns is a boolean value. Either true or false depending on whether the matched node's value is "17".And if you mean the expression in number() returns two nodes... again, I don't think that's possible, unless you have two строка elements with the same UID or two таблица elements with the same UID containing a single строка with the same UID each, or if you have two учреждение elements containing a таблица and строка elements with the same UIDs. If it's the last case, you can always add yet another predicate, this time on учреждение to ensure you always go on that one level only.
So in xPath2.0, as I understand I should use {for} to solve this problem, and in xPath1.0 how could this be done?
I couldn't really tell what you're trying to achieve. It possibly couldn't be done with XPath 1.0, so why don't you just get one of those XSLT 2.0 processors and be over with it?
Link to comment
Share on other sites

And if you mean the expression in number() returns two nodes... again, I don't think that's possible, unless you have two строка elements with the same UID or two таблица elements with the same UID containing a single строка with the same UID each,
UID's of строка element and таблица element are unique only whith in the scope of учреждение element, sory again I forgot to tell this.Anyway, thanks a lot for your help.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...