| 1 | /* |
| 2 | www.sourceforge.net/projects/tinyxpath |
| 3 | Copyright (c) 2002-2004 Yves Berquin ([email protected]) |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any |
| 7 | damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any |
| 10 | purpose, including commercial applications, and to alter it and |
| 11 | redistribute it freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must |
| 14 | not claim that you wrote the original software. If you use this |
| 15 | software in a product, an acknowledgment in the product documentation |
| 16 | would be appreciated but is not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and |
| 19 | must not be misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source |
| 22 | distribution. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | \file xml_util.cpp |
| 27 | \author Yves Berquin |
| 28 | */ |
| 29 | |
| 30 | #include "xml_util.h" |
| 31 | |
| 32 | namespace TinyXPath |
| 33 | { |
| 34 | |
| 35 | /// Cardinality in the terms of XPath counts from 1 for the first element |
| 36 | int i_xml_cardinality ( |
| 37 | const TiXmlElement * XEp_elem, ///< Base element. Must not be null |
| 38 | bool o_by_name) ///< true if we ask for the cardinality for our name only |
| 39 | { |
| 40 | const TiXmlNode * XNp_parent; |
| 41 | const TiXmlElement * XEp_child; |
| 42 | TIXML_STRING S_name; |
| 43 | int i_res; |
| 44 | |
| 45 | assert (XEp_elem); |
| 46 | XNp_parent = XEp_elem -> Parent (); |
| 47 | assert (XNp_parent); |
| 48 | if (o_by_name) |
| 49 | { |
| 50 | S_name = XEp_elem -> Value (); |
| 51 | XEp_child = XNp_parent -> FirstChildElement (S_name . c_str ()); |
| 52 | i_res = 1; |
| 53 | while (XEp_child) |
| 54 | { |
| 55 | if (XEp_child == XEp_elem) |
| 56 | return i_res; |
| 57 | else |
| 58 | { |
| 59 | i_res++; |
| 60 | XEp_child = XEp_child -> NextSiblingElement (S_name . c_str ()); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | XEp_child = XNp_parent -> FirstChildElement (); |
| 67 | i_res = 1; |
| 68 | while (XEp_child) |
| 69 | { |
| 70 | if (XEp_child == XEp_elem) |
| 71 | return i_res; |
| 72 | else |
| 73 | { |
| 74 | i_res++; |
| 75 | XEp_child = XEp_child -> NextSiblingElement (); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | assert (false); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | /// Family size : Nb of sibling elements (including ourselves) |
| 84 | int i_xml_family_size ( |
| 85 | const TiXmlElement * XEp_elem) ///< Base element. Must not be null |
| 86 | { |
| 87 | const TiXmlElement * XEp_child; |
| 88 | const TiXmlNode * XNp_parent; |
| 89 | int i_res; |
| 90 | |
| 91 | assert (XEp_elem); |
| 92 | XNp_parent = XEp_elem -> Parent (); |
| 93 | assert (XNp_parent); |
| 94 | XEp_child = XNp_parent -> FirstChildElement (); |
| 95 | i_res = 0; |
| 96 | while (XEp_child) |
| 97 | { |
| 98 | i_res++; |
| 99 | XEp_child = XEp_child -> NextSiblingElement (); |
| 100 | } |
| 101 | return i_res; |
| 102 | } |
| 103 | |
| 104 | } |