问题
From xml want to fetch subchapter number of subchapter in xref in html using XSL. I am not able to count and fetch the correct sub-chapter numbering in xref link text.
Scenario in xml: xref is added in subchapter 2.2 and referenced to subchapter 1.2, subchapter 2.1 and 1.3
Expected result in html from xml
In html output xref should append the correct number such as Sub-section 1.2, Sub-section 2.1 and Sub-section 1.3 respectively.
Note: Number in subchapter as 1.1, 1.2, 1.3 etc is given for reference & for better understanding.
Solution required - Need to count chapter and subchapter sequence to fetch the correct subchapter number in xref link.
Sample xml (chapter is referred to diagnostic_adt/diagnostic_adt and subchapter is referred to as diagnostic_adt/diagnostic_adt/diagnostic_adt).
I have used the provided solution to get the number but I am not able to get the number in html.
sample xsl below
<xsl:key name="element-by-id" match="*[@id]" use="@id"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="xref[key('element-by-id', substring(@href, 2))]">
<xsl:apply-templates select="key('element-by-id', substring(@href, 2))" mode="compute-number"/>
</xsl:template>
<xsl:template match="diagnostic_adt" mode="compute-number">
<xsl:number level="multiple" count="/*//diagnostic_adt"/>
</xsl:template>
<xsl:template match="xref">
<xsl:choose>
<xsl:when>
<xsl:variable name="xrefid" select="translate(@href, '#', '')"/>
<xsl:variable name="nos"><xsl:apply-templates select="key('element-by-id', substring(@href, 2))" mode="compute-number"/></xsl:variable>
<xsl:variable name="countN">
<xsl:choose>
<xsl:when test="contains($xrefid,'idSE')">
<xsl:value-of select="count(//diagnostic[contains(@id,$xrefid)]/preceding-sibling::*[name()='diagnostic'])+1" />
</xsl:when>
<xsl:when test="contains($xrefid,'idT')">
<xsl:choose>
<xsl:when test="contains($xrefid,'idTa')">
<xsl:value-of select="count(//table[contains(@id,$xrefid)]/preceding-sibling::*[name()='table'])+1"/>
</xsl:when>
<xsl:otherwise>
<!--<xsl:value-of select="count(ancestor-or-self::diagnostic_adt[contains(@id,$xrefid)]/diagnostic_adt)+1"/>.<xsl:value-of select="count(//diagnostic_adt[contains(@id,$xrefid)]/preceding-sibling::diagnostic_adt)+1"/>-->
<xsl:value-of select="$nos"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:variable>
<script type="text/javascript">
generateIntXref( "<xsl:call-template name="get-label"><xsl:with-param name="code" select="'CLIQUER-ICI'"/></xsl:call-template><i>(
<xsl:value-of select='$countN'/>)</i>.", "<xsl:value-of select='$xrefid'/>", "<xsl:value-of select='$countN'/>" );
</script>
</xsl:when>
</xsl:choose>
</xsl:template>
sample xml
<diagnostic_adt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" appl="" id="TEMPLATE" xml:lang="en" xsi:noNamespaceSchemaLocation="urn:ptc:names:renault:dita:xsd:diagnostic_adt.xsd">
<diagtitle translate="yes">Test xref</diagtitle>
<diagbody>
</diagbody>
<diagnostic_adt id="idSE1">
<diagtitle translate="yes">chapter 1</diagtitle>
<diagbody>
<para translate="yes">
To return to the previous screen, click here:
<END-PROCEDURE />
</para>
</diagbody>
<diagnostic_adt id="idT1">
<diagtitle translate="yes">sub-chapter 1.1</diagtitle>
<diagbody>
<para translate="yes">Test</para>
</diagbody>
</diagnostic_adt>
<diagnostic_adt id="idT2">
<diagtitle translate="yes">sub-chapter 1.2</diagtitle>
<diagbody>
<para translate="yes">Test</para>
</diagbody>
</diagnostic_adt>
<diagnostic_adt id="idT5">
<diagtitle translate="yes">sub-chapter 1.3</diagtitle>
<diagbody>
<para translate="yes">Test</para>
<para translate="yes">
<xref format="dita" href="#idT2" scope="local" translate="yes" type="diagnostic_adt" />
</para>
</diagbody>
</diagnostic_adt>
</diagnostic_adt>
<diagnostic_adt id="idSE2">
<diagtitle translate="yes">chapter 2</diagtitle>
<diagbody>
<para translate="yes">
To return to the previous screen, click here:
<END-PROCEDURE />
</para>
</diagbody>
<diagnostic_adt id="idT3">
<diagtitle translate="yes">sub-chapter 2.1</diagtitle>
<diagbody>
<para translate="yes">
<xref format="dita" href="#idT2" scope="local" translate="yes" type="diagnostic_adt" />
</para>
<para translate="yes">
<xref format="dita" href="#idT5" scope="local" translate="yes" type="diagnostic_adt" />
</para>
<para translate="yes">
<xref format="dita" href="#idT4" scope="local" translate="yes" type="diagnostic_adt" />
</para>
</diagbody>
</diagnostic_adt>
<diagnostic_adt id="idT4">
<diagtitle translate="yes">sub-chapter 2.2</diagtitle>
<diagbody>
<para translate="yes">
<xref format="dita" href="#idT3" scope="local" translate="yes" type="diagnostic_adt" />
</para>
<para translate="yes">
<xref format="dita" href="#idT5" scope="local" translate="yes" type="diagnostic_adt" />
</para>
<para translate="yes">
<xref format="dita" href="#idT2" scope="local" translate="yes" type="diagnostic_adt" />
</para>
</diagbody>
</diagnostic_adt>
</diagnostic_adt>
</diagnostic_adt>
来源:https://stackoverflow.com/questions/62837599/from-xml-want-to-fetch-subchapter-number-in-xref-in-html-using-xsl