You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there an easy(ish) way of finding an immediate child element by tag name?
getElementsByTagName returns a node list of all matching tags, regardless of depth, I see theres a childNodes property not really sure how to iterate it properly ( I tried extracting a loop to a function but can't for the life of me work out what type I should declare a node argument as ( D newbie, it looks like some template specialised type? ).
Anyway, this is the function I have currently:
string mavenVersionFromString(string pomContents)
{
import std.experimental.xml;
auto domBuilder = pomContents.parser.cursor.domBuilder;
domBuilder.setSource(pomContents);
domBuilder.buildRecursive;
auto doc = domBuilder.getDocument;
auto projectNode = doc.getElementsByTagName("project").front;
auto versionNode = projectNode.getElementsByTagName("version").front;
auto pomVersion = versionNode.textContent;
return pomVersion != null ? pomVersion : "unknown";
}
The getElementsByTagName call for version is finding the /project/parent/version element rather than the desired /project/version ( oh I wish for XPath here ).
What would be the idiomatic D way of handling this?
As an aside, I found it interesting that I had to use getElementsByTagName and NOT getElementsByTagNameNS which I assumed I should do ( which I do in Java, and other XML processing libraries ) since the XML declares a top-level default namespace. Is that due to how the DOMBuilder is built?
From @talios on July 7, 2017 22:19
Is there an easy(ish) way of finding an immediate child element by tag name?
getElementsByTagName
returns a node list of all matching tags, regardless of depth, I see theres achildNodes
property not really sure how to iterate it properly ( I tried extracting a loop to a function but can't for the life of me work out what type I should declare a node argument as ( D newbie, it looks like some template specialised type? ).Anyway, this is the function I have currently:
and this is my unittest:
The
getElementsByTagName
call forversion
is finding the/project/parent/version
element rather than the desired/project/version
( oh I wish for XPath here ).What would be the idiomatic D way of handling this?
As an aside, I found it interesting that I had to use
getElementsByTagName
and NOTgetElementsByTagNameNS
which I assumed I should do ( which I do in Java, and other XML processing libraries ) since the XML declares a top-level default namespace. Is that due to how the DOMBuilder is built?Copied from original issue: lodo1995/experimental.xml#43
The text was updated successfully, but these errors were encountered: