DotJoshJohnson / vscode-xml

XML Tools for Visual Studio Code
MIT License
320 stars 87 forks source link

Feature: build XPath based attribute values #295

Open panmusk opened 4 years ago

panmusk commented 4 years ago

Description I would like to have the ability to build and xpath based on attribute values (not all, but only user-defined ordered list of key-attributes).

Example:

<root>
    <page id="pageOne">
        <section id="section1.2">
            <subsection id="subsection1.2.1"/>
            <subsection id="subsection1.2.2"/>
            <subsection id="subsection1.2.3"/>
            <subsection id="subsection1.2.4"/>
            <subsection id="subsection1.2.5"/>
        </section>
        <section id="section1.3">
            <subsection id="subsection1.3.1"/>
            <subsection id="subsection1.3.2"/>
            <subsection id="subsection1.3.3"/>
            <subsection id="subsection1.3.4"/>
            <subsection id="subsection1.3.5"/>
        </section>
        <section id="section1.4" name="someName">
            <subsection name="subsection1.4.1"/>
            <subsection name="subsection1.4.2"/>
            <subsection id="subsection1.4.3"/>
            <subsection name="subsection1.4.4"/>
            <subsection/>
        </section>
    </page>
</root>

let's assume that user-defined key-attributes are: ["id","name"] So for the subsection1.4.4 node generated XPath will be like /root/page[@id='pageOne']/section[@id='section1.4']/subsection[@name='subsection1.4.4'] Explanation: id was used in pageand section nodes because it has higher priority in key-attribute settings then name, and the name attribute was used in subsection node because it does not have the id attribute.

For the last one, attributeless subsection will be: /root/page[@id='pageOne']/section[@id='section1.4']/subsection[5] index 5 was used because this subsection node has none of the defined key-attributes.

I know that this makes sense only when the attributes are unique (unless the generated XPath won't point to a single node), so this attribute-base xpath-genreration feature should be either optional, or under another command.

Current Workarounds None. Just senseless scrolling to the top to see what are the values of the current node parents attributes.