noussa
noussa

Reputation: 13

Declaring multiples Keys in XSLT

Thanks in advance for taking the time to read this.

I have an XML file where I need to declare a key for each section.

<chapter>
    <section> First section
        <section>Section 1</section>
        <section>Section 2</section>
        <section>Section 3

            <informaltable role="table">
                <thead>
                    <row>
                        <entry>Familly</entry>
                        <entry>Type</entry>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry>F1</entry>
                        <entry>T1</entry>
                    </row>
                    <row>
                        <entry>F1</entry>
                        <entry>T2</entry>
                    </row>
                </tbody>
            </informaltable>
        </section>
    </section>

    <section> Seconde section
        <section>Section 1</section>
        <section>Section 2</section>
        <section>Section 3

            <informaltable role="table">
                <thead>
                    <row>
                        <entry>Familly</entry>
                        <entry>Type</entry>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry>F2</entry>
                        <entry>T2</entry>
                    </row>
                    <row>
                        <entry>F1</entry>
                        <entry>T2</entry>
                    </row>
                </tbody>
            </informaltable>
        </section>
    </section>
</chapter>

Now I have the keys defined like so

<xsl:key name="byFamilly" match="d:chapter/d:section[1]//d:row" use="d:entry[1]"/>  
<xsl:key name="byFamilly" match="d:chapter/d:section[2]//d:row" use="d:entry[1]"/>  

In case of number of section >50 It’s possible to declare one key continent the different value of each section.

Can any one help me out how to do it.

Thank you.

Upvotes: 1

Views: 31

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167706

The usual technique in XSLT 1 is to insert the generated id from the ancestor (i.e. the section) in the key value e.g.

<xsl:key name="byFamilly" match="d:chapter/d:section//d:row" use="concat(generate-id(ancestor::d:section), '|', d:entry[1])"/>  

In XSLT 2 and later the key function has an optional third argument where you could pass in the section you want to restrict the lookup to so you wouldn't need to include the id in the key value.

Upvotes: 1

Related Questions