Reputation: 307
I have 2 xml files for French and English languages to generate a HTML. I'm accessing the content of English XML using xslt document() function. I have a problem with second level <group-title>/<title-name>
XML1 French
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<group-container>
<group-name>Comptes</group-name>
<group-title>
<title-name>Consulter des comptes</title-name>
</group-title>
<group-title>
<title-name>Comptes</title-name>
</group-title>
</group-container>
<group-container>
<group-name>Paiements</group-name>
<group-title>
<title-name>Historique</title-name>
</group-title>
<group-title>
<title-name>Nouveau compte</title-name>
</group-title>
</group-container>
<group-container>
<group-name>Cartes</group-name>
<group-title>
<title-name>Créer un virement</title-name>
</group-title>
<group-title>
<title-name>Virements</title-name>
</group-title>
</group-container>
</Root>
XML2 English
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<group-container>
<group-name>Accounts</group-name>
<group-title>
<title-name>open new account</title-name>
</group-title>
<group-title>
<title-name> linked accounts</title-name>
</group-title>
</group-container>
<group-container>
<group-name>Payments</group-name>
<group-title>
<title-name>History</title-name>
</group-title>
<group-title>
<title-name>New Payment</title-name>
</group-title>
</group-container>
<group-container>
<group-name>cards</group-name>
<group-title>
<title-name>Make a Payment</title-name>
</group-title>
<group-title>
<title-name>statement</title-name>
</group-title>
</group-container>
</Root>
XSLT
<xsl:variable name="en-xml-analytics">
<xsl:value-of select="//path to XML 2 English "/>
</xsl:variable>
<xsl:template match="Root">
<div class="first-level-inner">
<xsl:apply-templates select="./group-container" mode="first-level" />
</div>
<xsl:apply-templates select="./group-container" mode="second-level" />
</xsl:template>
<xsl:template match="group-container" mode="first-level">
<xsl:variable name="pos" select="position()"/>
<div>
<span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos]/group-name}">
<span>
<xsl:value-of select="group-name" />
</span>
</span>
</div>
</xsl:template>
<xsl:template match="group-container" mode="second-level">
<div class="second-level-inner">
<xsl:apply-templates select="group-title" mode="title" />
</div>
</xsl:template>
<xsl:template match="group-title" mode="title">
<xsl:variable name="pos1" select="position()"/>
<h5>
<xsl:value-of select="title-name" />
</h5>
<span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos1]/group-title[$pos1]/title-name}"/>
</xsl:template>
Expected output
<div class="first-level-inner">
<div>
<span data-webanalytics="menunavigation.Accounts">
<span>Comptes</span>
</span>
</div>
<div>
<span data-webanalytics="menunavigation.Payments">
<span>Paiements</span>
</span>
</div>
<div>
<span data-webanalytics="menunavigation.Cards">
<span>Cartes</span>
</span>
</div>
</div>
<div class="second-level-inner">
<h5>Consulter des comptes</h5>
<span data-webanalytics="menunavigation.open new account"/>
<h5>Comptes</h5>
<span data-webanalytics="menunavigation.linked accounts"/>
</div>
<div class="second-level-inner">
<h5>Historique</h5>
<span data-webanalytics="menunavigation.History"/>
<h5>Nouveau compte</h5>
<span data-webanalytics="menunavigation.New Payment"/>
</div>
<div class="second-level-inner">
<h5>Créer un virement</h5>
<span data-webanalytics="menunavigation.Make a Payment"/>
<h5>Virements</h5>
<span data-webanalytics="menunavigation.statement"/>
</div>
I can get the first level / successfully with position()
. However I'm lost with second level position.
Upvotes: 0
Views: 88
Reputation: 167401
You need to pass the outer position on as a parameter e.g.
<xsl:template match="group-container" mode="second-level">
<xsl:variable name="pos" select="position()"/>
<div class="second-level-inner">
<xsl:apply-templates select="group-title" mode="title">
<xsl:with-param name="pos" select="$pos" tunnel="yes"/>
</xsl:apply-templates>
</div>
</xsl:template>
plus
<xsl:template match="group-title" mode="title">
<xsl:param name="pos" tunnel="yes"/>
<xsl:variable name="pos1" select="position()"/>
<h5>
<xsl:value-of select="title-name" />
</h5>
<span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos]/group-title[$pos1]/title-name}"/>
</xsl:template>
Instead of passing the outer position on you could also opt to pass the document($en-xml-analytics)/Root/group-container[$pos]
and use that.
Upvotes: 1
Reputation: 116959
Why don't you do simply:
<xsl:template match="/Root">
<xsl:variable name="en-root" select="document($path-to-en-xml)/Root" />
<!-- first-level -->
<div class="first-level-inner">
<xsl:for-each select="group-container">
<xsl:variable name="i" select="position()" />
<div>
<span data-webanalytics="menunavigation.{$en-root/group-container[$i]/group-name}">
<span>
<xsl:value-of select="group-name"/>
</span>
</span>
</div>
</xsl:for-each>
</div>
<!-- second-level -->
<div class="second-level-inner">
<xsl:for-each select="group-container/group-title">
<xsl:variable name="j" select="position()" />
<h5>
<xsl:value-of select="title-name"/>
</h5>
<span data-webanalytics="menunavigation.{($en-root/group-container/group-title)[$j]/title-name}"/>
</xsl:for-each>
</div>
</xsl:template>
Note that the result is an XML fragment, with no single root element.
Upvotes: 1