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

[Feature request] short option flags for --printed-node-format and --ignore-namespaces #59

Closed
goekce opened this issue Nov 16, 2020 · 5 comments

Comments

@goekce
Copy link

goekce commented Nov 16, 2020

I use xidel in a course where we parse XML files. We used xmllint for that, but xmllint does not support parsing without setting the namespaces, and I am grateful that I found xidel!

As my students are used to xmllint output, I have to use --printed-node-format=xml and --ignore-namespaces to get a similar output. xidel outputs the content of a node immediately without having to provide text(), which is convenient.

Example:

xidel https://www.uniprot.org/uniprot/P21817.xml \
  --printed-node-format=xml \
  --ignore-namespaces \
  -se '//protein'
<protein>
<recommendedName>
<fullName>Ryanodine receptor 1</fullName>
<shortName>RYR-1</shortName>
...
</protein>

It takes time for the users to write these long options. Is it possible to add shorter option flags for --printed-node-format and --ignore-namespaces? Or did I miss any alternative options?

@benibela
Copy link
Owner

There is --xml to set input and output format. Or use -se 'serialize(//protein)'

There is a nothing shorter for --ignore-namespaces. You could set it permanently though export XIDEL_OPTIONS=--ignore-namespaces. It is a hardly used option (I had even forgotten that it was there).

@goekce
Copy link
Author

goekce commented Nov 17, 2020

--xml is a convenient alternative with a minor inconvenience for my case (see below).

-se 'serialize(//protein)' sometimes messes up the output, and does not use coloring on my terminal:

$ xidel https://www.uniprot.org/uniprot/P21817.xml -se 'serialize(//fullname)'
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Ryanodine receptor 1</fullName><fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle calcium release channel</fullName><fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle ryanodine receptor</fullName><fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle-type ryanodine receptor</fullName><fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Type 1 ryanodine receptor</fullName>

I use --ignore-namespaces for instructional and experimenting purposes to declutter the output, e.g.:

$ xidel https://www.uniprot.org/uniprot/P21817.xml --xml -se '//fullname'
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Ryanodine receptor 1</fullName>
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle calcium release channel</fullName>
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle ryanodine receptor</fullName>
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Skeletal muscle-type ryanodine receptor</fullName>
<fullName xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Type 1 ryanodine receptor</fullName>
</xml>

with --ignore-namespaces:

$ xidel https://www.uniprot.org/uniprot/P21817.xml --xml -se '//fullname' --ignore-namespaces
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<fullName>Ryanodine receptor 1</fullName>
<fullName>Skeletal muscle calcium release channel</fullName>
<fullName>Skeletal muscle ryanodine receptor</fullName>
<fullName>Skeletal muscle-type ryanodine receptor</fullName>
<fullName>Type 1 ryanodine receptor</fullName>
</xml>

For experimenting on the shell, I still find --printed-node-format=xml better than --xml, though, because xml nodes are completely gone:

$ xidel chebi_27732_P21817.xml --printed-node-format=xml -se '//fullname' --ignore-namespaces
<fullName>Ryanodine receptor 1</fullName>
<fullName>Skeletal muscle calcium release channel</fullName>
<fullName>Skeletal muscle ryanodine receptor</fullName>
<fullName>Skeletal muscle-type ryanodine receptor</fullName>
<fullName>Type 1 ryanodine receptor</fullName>

My cases may be not very common, I can understand that! Nevertheless XIDEL_OPTIONS="--printed-node-format=xml --ignore-namespaces" is also a good way to avoid the long options in subsequent iterations of the command. I hope that --ignore-namespaces and --printed-node-format stay as options in future. Thanks Benito!

@goekce goekce closed this as completed Nov 17, 2020
@Reino17
Copy link

Reino17 commented Oct 26, 2021

Found this (old) "issue" of yours.

-se 'serialize(//protein)' sometimes messes up the output, and does not use coloring on my terminal:

Only HTML/XML and JSON output gets coloring. The output of serialize() is always a string. And in addition, if //fullname is a sequence of nodes, then serialize(//fullname) serializes/"stringifies" and "minifies"/concatenates these strings, as you've seen already.

At the moment the only way to "prettify" XML (when the input is not a sequence) is to use serialize()'s "indent"-option:

$ xidel https://www.uniprot.org/uniprot/P21817.xml -se 'serialize(//protein,{"indent":true()})'

And if you really want coloring :D :

$ xidel https://www.uniprot.org/uniprot/P21817.xml -se 'serialize(//protein,{"indent":true()})' | \
  xidel -se '.' --printed-node-format=xml

$ xidel https://www.uniprot.org/uniprot/P21817.xml -se '
  parse-xml(
    serialize(//protein,{"indent":true()})
  )
' --printed-node-format=xml

Pretty good chance xidel well get a dedicated "prettify" command-line option in the near future. See https://sourceforge.net/p/videlibri/mailman/message/37373117/.

@goekce
Copy link
Author

goekce commented Oct 27, 2021

I used to use xidel during my classes to teach Xpath to students who are new to the Unix shell. My goal was to show students a fast way of extracting data from an XML file without configuring the tool. Using your serialize() is a good solution which could be interesting for shell users who know how to create wrappers around shell commands.

Thanks for your followup @Reino17 !

@benibela
Copy link
Owner

@Reino17

That reminds me that I wanted to rename --printed-node-format=xml to something like --output-node-format=xml and add a --output-indent or --output-node-indent option

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

No branches or pull requests

3 participants