如果是锚
<a class="section-ref">
具有
@href
链接到
@id
的
<div class="sect1">
这是一个
<div class="chunk">
,我想更改定位的值
@href
到
@id号
父块的。我不知道如何找到
@id号
通过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>
所需输出:
<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>
请注意,节ref anchor@href值更改为父块的@id(i100954):
<p>In the <a class="section-ref" href="i100954" id="i2397">next section</a>, we turn lorem ipsum.</p>