DLR-SC/tixi

tixiGetNumberOfChilds gives 1 for an empty list

Closed this issue · 6 comments

For the xml file:

<?xml version="1.0" encoding="utf-8"?>
<project>
  <elements>
  </elements>
</project>

And the corresponding function call:

int count;
tixiGetNumberOfChilds(handle,"project/elements",&count);

we get 1 and not as I would expect to 0. Is this intentional, and if so, how would one get the actual list size?

Confirmed for Python:

> set PATH=C:\Users\bach_ar\Downloads\TIXI-3.3.0-win64\bin;%PATH%
> python
import tixi3wrapper
cpacs = tixi3wrapper.Tixi3()
cpacs.openString("""?xml version="1.0" encoding="utf-8"?>
<project>
  <elements>
  </elements>
</project>""")
cpacs.getNumberOfChilds("/project/elements")
1

I can fix it by removing the newline and spaces between <element> and </element>.
It seems that tixi counts the space as an element somehow.

Spaces and new Lines constitute technically to (invisible) XML text elements, so that's why you'll get "1" as the number of childs. So technically (by xml definition), the result 1 is correct. I understand though, that in this case, you want probably iterate over the "element" nodes, so you expect a zero count.

I think a better API would be something

tixiGetNumberOfChildElements(handle, "/project/elements", "element", &count)

where you explicitly need to defined the element name you are searching for.

You can though use the function tixiXPathEvaluateNodeNumber instead, to query only for "element" nodes:

tixiXPathEvaluateNodeNumber(handle, "/project/elements//element", &count)

Here's an example

from tixi3 import tixi3wrapper

cpacs = tixi3wrapper.Tixi3()
cpacs.openString("""<?xml version="1.0" encoding="utf-8"?>
<project>
  <elements>
  </elements>
</project>""")

# prints 0
print(cpacs.xPathEvaluateNodeNumber("/project/elements//element"))

cpacs = tixi3wrapper.Tixi3()
cpacs.openString("""<?xml version="1.0" encoding="utf-8"?>
<project>
  <elements>
    <element/>
  </elements>
</project>""")

# prints 1
print(cpacs.xPathEvaluateNodeNumber("/project/elements//element"))

I guess, this would be equivalent to the
ReturnCode tixiGetNamedChildrenCount (const TixiDocumentHandle handle, const char *elementPath, const char *childName, int *count)
method, right?

@CLiersch You are absolutely right 🤦 . It seems, that it is too long ago, since I was using tixi last time.

I am closing the issue, since the function is doing what it should do. Use instead tixiGetNamedChildrenCount as @CLiersch suggested.