sspsujit
sspsujit

Reputation: 301

how to determine the level of an element by matching its id in xslt?

How to get output like below example? I want to generate ids for section elements based on their level and position. Please notice the result of <link>, where the matching ids are called.

Any help would be appreciable.

INPUT:

<book>
    <section id="s1">
        <section id="s7">
            <section id="s9">
            </section>
            <p>xxxxxx</p>
            <section id="s2">
            </section>
        </section>
        <section id="s17">
            <section id="s19">
            </section>
        </section>
    </section>
    <p>yyyyyyyyy</p>
    <section id="s201">
        <section id="s190">
            <link href="book1:s19"/>
        </section>
        <section id="s200">
            <link href="book1:s2"/>
        </section>
    </section>
</book>

OUTPUT should be as:

<book>
    <section id="s1" order="1">
        <section id="s1-1" order="2">
            <section id="s1-1-1" order="3">
            </section>
            <p>xxxxxx</p>
            <section id="s1-1-2" order="3">
            </section>
        </section>
        <section id="s1-2" order="2">
            <section id="s1-2-1" order="3">
            </section>
        </section>
    </section>
    <p>yyyyyyyyy</p>
    <section id="s2" order="1">
        <section id="s2-1" order="2">
            <a href="s1-2-1"/>
        </section>
        <section id="s2-2" order="2">
            <a href="s1-1-2"/>
        </section>
    </section>
</book>

By implementing below xslt, I am just able to generate the ids for <section> elements. But unable to get the correct output in <a href="{value_of_resulting_section_id matched with id in @href attribute of link}"> i.e. the resulting section id for the matched id in @href of link element.

<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:output method="xml"/>
<xsl:key name="KEY" match="section" use="id" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="book">
        <book><xsl:apply-templates/></book>
    </xsl:template>

    <xsl:template match="section">
        <xsl:variable name="ORDER" select="count(ancestor::section)+1"/>
        <xsl:element name="section">
            <xsl:if test="$ORDER=1">
                <xsl:attribute name="id"><xsl:text>s</xsl:text>
                    <xsl:value-of select="count(preceding-sibling::section)+1"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="$ORDER=2">
                <xsl:attribute name="id"><xsl:text>s</xsl:text>
                    <xsl:value-of select="($ORDER - 1) + count(parent::section/preceding-sibling::section)"/>
                    <xsl:text>.</xsl:text>
                    <xsl:value-of select="count(preceding-sibling::section)+1"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:if test="$ORDER=3">
                <xsl:attribute name="id"><xsl:text>s</xsl:text>
                    <xsl:value-of select="($ORDER - 2)"/>
                    <xsl:text>.</xsl:text>
                    <xsl:value-of select="count(parent::section/preceding-sibling::section)+1"/>
                    <xsl:text>.</xsl:text>
                    <xsl:value-of select="count(preceding-sibling::section)+1"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:attribute name="order">
                <xsl:value-of select="count(ancestor::section)+1"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="link">
        <xsl:element name="a">
            <xsl:attribute name="id">
                <xsl:value-of select="generate-id()"/>
            </xsl:attribute>
            <xsl:attribute name="href">
                <xsl:value-of select="count(//section[@id=substring-after(@href,':')])"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:transform>

Anybody has the answer please help? Thanks...

Upvotes: 0

Views: 171

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167726

Numbering can be done with xsl:number, the cross-references can be found with a key and the computation of the the new number can be factored out into a template of its own so that both the element as well as the referencing one can use it to compute the same id:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="xs math map array"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:key name="section-ref" match="section" use="@id"/>

  <xsl:template match="section">
      <xsl:copy>
          <xsl:attribute name="id">
            <xsl:apply-templates select="." mode="generate-id"/>
          </xsl:attribute>
          <xsl:attribute name="order" select="count(ancestor-or-self::section)"/>
          <xsl:apply-templates/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="link[@href]">
      <a>
          <xsl:attribute name="href">
              <xsl:apply-templates select="key('section-ref', substring-after(@href, ':'))" mode="generate-id"/>
          </xsl:attribute>
      </a>
  </xsl:template>

  <xsl:template match="section" mode="generate-id">
      <xsl:text>s</xsl:text>
      <xsl:number level="multiple" format="1-1"/>     
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/eiZQaFC is an online XSLT 3 sample but for XSLT 2 the code should work as well if you remove the xsl:mode and keep your identity transformation template.

Upvotes: 1

Related Questions