Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Utility for building complex XPath expressions from simple ones #93

Closed
janssenhenning opened this issue Nov 6, 2021 · 1 comment
Labels

Comments

@janssenhenning
Copy link
Contributor

janssenhenning commented Nov 6, 2021

This issue sketches a design to enable easier construction of complex xpath expressions from simple ones without needing to fall back to knowing the full xpath.

The current workflow for this is

#Get the simple xpath from the schema dictionary
simple = schema_dict.tag_xpath(tag_name)

#Build your complex xpath manually with string manipulation
...

#Evaluating the complex xpath

This is used in the xml_setters set_species for example to select specific species

This building of the complex xpath could be designed in a more concise way. This is just a sketch how the usage of this functionality could look. What I have in mind is only adding so called predicates and not wildcard expressions or entering relative expressions https://www.w3schools.com/xml/xpath_syntax.asp

lo_path = schema_dict.tag_xpath('lo', contains='species')

builder = XPathBuilder(lo_path, filters={
                           'species': {'index': {'<=': 3 }}, #Get the first three species nodes
                           'lo': {'type': {'==': 'SCLO'}}
                })
xpath = builder.build() #/fleurInput/atomSpecies/species[position()<=3]/lo[@type='SCLO']

The syntax of the filters dictionary is borrowed from the AiiDA Querybuilder. I don't know if this is the best approach but it seems like a robust starting point to me.

Ultimately this allows to add an argument filters or where to specify these filters for xml_setters/xml_getters

More things that can be added on to this are:

  • Subclassing the basic XPathBuilder with SchemaXPathBuilder with additional validation and functionality from teh schema dictionaries
  • Instead of the example /fleurInput/atomSpecies/species[position()<=3]/lo[@type='SCLO'] the recommended safe way to do these variable expressions is /fleurInput/atomSpecies/species[position()<=$index]/lo[@type=$type] and passing the actual values as variables to the Xpath engine. This can also be supported by this builder mechanism
@janssenhenning
Copy link
Contributor Author

Closed via #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant