Open wattsjp2 opened 4 years ago
Possibly related to https://github.com/lucidsoftware/xtract/issues/24?
I think the root problem is the way stringReader handles nodes that have a text element and children elements which I think is tied to how NodeSeq.text
works. I think #24 is thwarting my efforts to work around the problem so in that way they're related.
But what should the behaviour be? should it use the concatenation of all text nodes that are direct children (but not descendents), use just the first text node, fail if there are element node children, or use a concatenation of all descendent text nodes (as is currently done)?
Changing the default way from the current behaviour is probably a breaking change, although I'm not sure if any existing usages make use of the current behaviour in the presence of child elements. I'm not necessarily opposed to changing the default way of parsing text. But to be honest, I kind of think that failing if there are child elements makes the most sense to me as the default. I pointed at #24 because it is a more general solution, though perhaps not as convenient in your case.
I could probably add something to extract the first text node or all direct child text nodes to the XPath and/or XmlReader API. Although I'm not sure what good names for those methods would be. Would that work for you?
You know, I may have forgotten an XML element could have multiple text nodes. I think you're making a lot of sense then. Trying to read a node with children as a string is probably undefined behaviour. I think a solution that returned either the direct child text nodes or all child nodes makes sense and then the user can do a collectFirst
or mkstring
or whatever based on their use case.
I have an XML element that looks like
that I'm trying to write an XmlReader for. My case class looks like:
The problem I'm having is trying to extract the card number. I've played around on the REPL and looked at the source code. My first thought was I could just read the root element
This is reading everything under the root node though. My next thought was maybe I could loop through the child nodes:
but that doesn't return the text node. My last thought was what if
<Card>
wasn't the root element. Would that change anything:So that seems to be giving the same behavior. It looks like under the hood stringReader is using the
text
function onNodeSeq
It looks like this behavior comes from there