Suppose in the future there will be a foo element. So, I first test for the existence of foo:
{ if (fn:exists(/Document/foo)) then fn:count(/Document/row)+fn:count(/Document/foo)+1 else fn:count(/Document/row)+1 } Daffodil gives the same error message: No element corresponding to step {}foo found. Eek! That seems like a bug in Daffodil. Or a bug in the DFDL specification. Or both. Yes? /Roger From: Roger L Costello <coste...@mitre.org> Sent: Thursday, January 4, 2024 6:49 AM To: users@daffodil.apache.org Subject: Re: How to count the number of child elements of the root element? Mike said: * { fn:count(/Document/row) + 1 } should give the value you want. Question: Suppose I call fn:count on a non-existent foo element: {fn:count(/Document/row)+fn:count(/Document/foo)+1} I would expect fn:count(/Document/foo) to return 0 since there are no foo elements. But it doesn’t. Daffodil generates an error. Why is that? /Roger From: Mike Beckerle <mbecke...@apache.org<mailto:mbecke...@apache.org>> Sent: Tuesday, January 2, 2024 8:32 AM To: users@daffodil.apache.org<mailto:users@daffodil.apache.org> Subject: [EXT] Re: How to count the number of child elements of the root element? DFDL's count function does not operate on any "node set", but on specific named array elements only. In your case, the array is the row element, so { fn: count(/Document/row) + 1 } should give the value you want. On Wed, Dec 27, DFDL's count function does not operate on any "node set", but on specific named array elements only. In your case, the array is the row element, so { fn:count(/Document/row) + 1 } should give the value you want. On Wed, Dec 27, 2023 at 8:44 AM Roger L Costello <coste...@mitre.org<mailto:coste...@mitre.org>> wrote: Hi Folks, My input file has a field containing the number of “words” in the file, where the “words” are the child elements of the root element. Here’s the XML that parsing produces: <Document> <initial_word> <message_length>3</message_length> </initial_word> <row>...</row> <row>...</row> </Document> The message_length is the initial_word + number of <row> elements, i.e., 1 + 2 = 3 I would like to hide message_length because it provides no meaningful information in the XML (after all, if you want to know the message length simply count the child elements of <Document>). To hide message_length, I will need a hidden group in my DFDL schema: <xs:group name="hidden-MESSAGE_LENGTH "> <xs:sequence> <xs:element name="hidden" type="unsignedint3" dfdl:outputValueCalc="{????}"/> </xs:sequence> </xs:group> The challenge that I am having is what XPath to use in dfdl:outputValueCalc. Initially, I used this: dfdl:outputValueCalc="{fn:count(/Document/*)} Daffodil complained: Wildcard * is not supported. Then I tried this: dfdl:outputValueCalc="{fn:count(../following-sibling::row)+1} Daffodil complained: following-sibling axis is not supported. Eek! I don’t know what else to try. Any suggestions? /Roger