- 首页
- 其他开发
- xslt 带有 2 个具有相同结构和多个级别的不同 XML 文件
xslt 带有 2 个具有相同结构和多个级别的不同 XML 文件
[英] xslt with 2 different XML files with same structure and multiple levels
本文介绍了xslt 带有 2 个具有相同结构和多个级别的不同 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个法语和英语的 xml 文件来生成 HTML.我正在使用 xslt document() 函数访问英文 XML 的内容.我有第二级 /
的问题
XML1 法语
<根><组容器><group-name>Comptes</group-name><组标题><title-name>Consulter des Comptes</title-name></group-title><组标题><title-name>Comptes</title-name></group-title></group-container><组容器><group-name>Paiements</group-name><组标题><title-name>历史</title-name></group-title><组标题><title-name>Nouveau compte</title-name></group-title></group-container><组容器><group-name>Cartes</group-name><组标题><title-name>Créer un virement</title-name></group-title><组标题><title-name>Virements</title-name></group-title></group-container></Root>
XML2 英文
<根><组容器><group-name>帐户</group-name><组标题><title-name>开设新账户</title-name></group-title><组标题><标题名称>关联账户</title-name></group-title></group-container><组容器><group-name>付款</group-name><组标题><title-name>历史</title-name></group-title><组标题><title-name>新支付</title-name></group-title></group-container><组容器><group-name>卡片</group-name><组标题><title-name>付款</title-name></group-title><组标题><title-name>statement</title-name></group-title></group-container></Root>
XSLT
<xsl:value-of select="//XML 2 英文路径"/></xsl:变量><xsl:template match="Root"><div class="first-level-inner"><xsl:apply-templates select="./group-container";模式=第一级"/>
<xsl:apply-templates select="./group-container";模式=二级"/></xsl:模板><xsl:template match="group-container";模式=第一级"><xsl:variable name="pos";选择=位置()"/><div><span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos]/group-name}"><跨度><xsl:value-of select="group-name";/></span></span>
</xsl:模板><xsl:template match="group-container";模式=第二级"><div class="second-level-inner"><xsl:apply-templates select="group-title";模式=标题"/>
</xsl:模板><xsl:template match="group-title";模式=标题"><xsl:variable name="pos1";选择=位置()"/><h5><xsl:value-of select="title-name";/><span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos1]/group-title[$pos1]/title-name}"/></xsl:模板>
预期输出
<div><span data-webanalytics="menunavigation.Accounts"><span>计算</span></span>
<div><span data-webanalytics="menunavigation.Payments"><span>Paiements</span></span>
<div><span data-webanalytics="menunavigation.Cards"><span>Cartes</span></span>
<div class="second-level-inner"><h5>Consulter des Comptes</h5><span data-webanalytics="menunavigation.open new account"/><h5>计算</h5><span data-webanalytics="menunavigation.linked accounts"/>
<div class="second-level-inner"><h5>历史</h5><span data-webanalytics="menunavigation.History"/><h5>Nouveau compte</h5><span data-webanalytics="menunavigation.New Payment"/>
<div class="second-level-inner"><h5>Créer un virement</h5><span data-webanalytics="menunavigation.Make a Payment"/><h5>病毒</h5><span data-webanalytics="menunavigation.statement"/>
我可以使用 position()
获得第一级/成功.但是我已经失去了二级位置.
解决方案
您需要将外部位置作为参数传递,例如
<xsl:variable name="pos";选择=位置()"/><div class="second-level-inner"><xsl:apply-templates select="group-title";模式=标题"><xsl:with-param name="pos";选择=$pos"隧道=是"/></xsl:apply-templates></xsl:模板>
加上
<xsl:param name="pos";隧道=是"/><xsl:variable name="pos1";选择=位置()"/><h5><xsl:value-of select="title-name";/><span data-webanalytics="menunavigation.{document($en-xml-analytics)/Root/group-container[$pos]/group-title[$pos1]/title-name}"/></xsl:模板>
除了传递外部位置,您还可以选择传递 document($en-xml-analytics)/Root/group-container[$pos]
并使用它.
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.
解决方案
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.
这篇关于xslt 带有 2 个具有相同结构和多个级别的不同 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文