Skip to content
This repository has been archived by the owner on Jun 13, 2018. It is now read-only.

Iterating over attributes #105

Open
saleyn opened this issue Nov 13, 2015 · 4 comments
Open

Iterating over attributes #105

saleyn opened this issue Nov 13, 2015 · 4 comments

Comments

@saleyn
Copy link
Contributor

saleyn commented Nov 13, 2015

It's unclear from documentation how to iterate over attributes of a given XML node.
I tried the following, without success:

.global.xxx = xml.load_string ("<a value = \"2\" x=\"1\">Hello</a>")
.for xxx. as i
    echo i
.endfor

This prints:

2015/11/13 13:32:55: gsl/4 M: Hello

What I need is an iterator that would iterate through all attributes of "a" node: "value", "x".
Any help?

@hintjens
Copy link
Member

I don't think this is possible. Iteration means a list, means elements.

On Fri, Nov 13, 2015 at 7:35 PM, Serge Aleynikov [email protected]
wrote:

It's unclear from documentation how to iterate over attributes of a given
XML node.
I tried the following, without success:

.global.xxx = xml.load_string ("<a value = "2" x="1">Hello")
.for xxx. as i
echo i
.endfor

This prints:

2015/11/13 13:32:55: gsl/4 M: Hello

What I need is an iterator that would iterate through "value", "x".
Any help?


Reply to this email directly or view it on GitHub
#105.

@saleyn
Copy link
Contributor Author

saleyn commented Nov 13, 2015

So, there is no way for a for/while loop to address the attributes of elements other then to accessing an individual element explicitly by name? Seems like a rather big limitation...

@evoskuil
Copy link
Member

Only if you don't control the schema.

@saleyn
Copy link
Contributor Author

saleyn commented Nov 13, 2015

Perhaps I am not thinking in "gsl" way? I have an XML document that I need to use to generate source code that would have a function that returns some node from this XML as an XML string. For that purpose I need to go through the XML doc using selective filtering and output some nodes with conditional filtering of their attributes. I know the schema upfront, but the XML elements have many optional attributes, and some of them need to be filtered out, so I cannot use XML.string() function "as-is". Here's an example:

<protocol app="app1" version="1.0">
    <schema name="secdef" table-group="group1" perm="true" insert="false">
        <deployment>
            <node type="ui"/>
        </deployment>
        <field name="symbol"/>
        <field name="isin"/>
    </schema>
    <schema name="algo" table-group="group2" perm="true" delete="true">
        <deployment>
            <node type="xxx"/>
        </deployment>
        <field name="symbol" key="true"/>
        <field name="isin" visible="false"/>
    </schema>
</protocol>

From this XML I need to produce a string which filters out some attributes, and includes others, e.g.:

<<"<protocol app=\"app1\">
    <schema name=\"secdef\" insert=\"false\">
        <field name=\"symbol\"/>
        <field name=\"isin\"/>
    </schema>
    <schema name=\"algo\" delete=\"true\">
        <field name=\"symbol\" key=\"true\"/>
        <field name=\"isin\"/>
    </schema>
</protocol>">>

Here iterating over children nodes is easy, but iterating over node's attributes and filtering out some of them is the problem I am having trouble with using gsl.

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

No branches or pull requests

3 participants