【发布时间】:2021-05-08 22:10:21
【问题描述】:
我有 2 个用于法语和英语的 xml 文件来生成 HTML。我正在使用 xslt document() 函数访问英文 XML 的内容。我有二级问题<group-title>/<title-name>
XML1 法语
<?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 英文
<?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>
预期输出
<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>
我可以通过position() 获得第一级/成功。但是我迷失了二级职位。
【问题讨论】: