Search code examples
xmlxsltxpathxslt-2.0

XSLT: replace the value of an attribute based on the existing attribute value


If an anchor <a class="section-ref"> has an @href value that links to an @id of a <div class="sect1"> which is a child of a <div class="chunk">, I want to change the value of the anchor @href to the @id of the parent chunk. I'm unsure how to find the @id of the parent chunk through XSLT/XPath.

XML:

<book>
<div>
    <p>In the <a class="section-ref" href="#i2398" id="i2397">next section</a>, we turn lorem ipsum.</p>
</div>
<div class="extend" id="i100949">
    <div class="check" id="i100950">
        <h1 class="title">Check</h1>
        <a class="other-ref" href="folder/other-ref.xml" id="i100953"></a>
    </div>
</div>
<div class="chunk" id="i100954">
    <h1 class="title">8.4</h1>
    <div class="other-section" id="i100955">
        <p id="i100956"> Lorem Ipsum</p>
    </div>
    <div class="sect1" id="i2398">
        <h1 class="title">Lorem Ipsum</h1>
        <p>Blah blah blah</p>
    </div>
</div>

XSLT:

<!-- Identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!--Change section-ref @href to parent chunk @id if they are @ids of a sect1-->

<xsl:template match="a[@class='section-ref']">
    <xsl:variable name="section-id" select="substring-after(./@href, '#')"/>
    <xsl:variable name="section" select="$section-id = //div[@class='chunk']/div[@class='sect1']/@id"/>

    <xsl:choose>
        <xsl:when test="$section">
            <xsl:copy>
                <xsl:attribute name="href" select="NEED XPATH HERE"/>
                <xsl:apply-templates select="(node() | @*) except @href"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="node() | @*"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Desired output:

<book>
<div>
    <p>In the <a class="section-ref" href="i100954" id="i2397">next section</a>, we turn lorem ipsum.</p>
</div>
<div class="extend" id="i100949">
    <div class="check" id="i100950">
        <h1 class="title">Check</h1>
        <a class="other-ref" href="folder/other-ref.xml" id="i100953"/>
    </div>
</div>
<div class="chunk" id="i100954">
    <h1 class="title">8.4</h1>
    <div class="other-section" id="i100955">
        <p id="i100956"> Lorem Ipsum</p>
    </div>
    <div class="sect1" id="i2398">
        <h1 class="title">Lorem Ipsum</h1>
        <p>Blah blah blah</p>
    </div>
</div>

Note that the section-ref anchor @href value changes to the @id of the parent chunk (i100954):

<p>In the <a class="section-ref" href="i100954" id="i2397">next section</a>, we turn lorem ipsum.</p>

Solution

  • I would define a key to reference the div and then you can select the parent div's id:

      <xsl:key name="ref" match="div[@class = 'sect1']" use="@id"/>
    
      <xsl:template match="a[@class = 'section-ref' and key('ref', substring(@href, 2))]/@href">
          <xsl:attribute name="{name()}" select="'#' || key('ref', substring(., 2))/parent::div/@id"/>
      </xsl:template>
    

    https://xsltfiddle.liberty-development.net/jyH9rM8 has a sample, it is XSLT 3 but for XSLT you just need to use concat instead of the || operator to construct the new attribute value and then you need to use the identity transformation template (you already have) instead of the xsl:mode.