【问题标题】:xslt with 2 different XML files with same structure and multiple levelsxslt 具有 2 个具有相同结构和多个级别的不同 XML 文件
【发布时间】: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() 获得第一级/成功。但是我迷失了二级职位。

【问题讨论】:

    标签: xslt xslt-1.0 xslt-2.0


    【解决方案1】:

    您需要将外部位置作为参数传递,例如

    <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>
    

    <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>
    

    除了传递外部位置之外,您还可以选择传递document($en-xml-analytics)/Root/group-container[$pos] 并使用它。

    【讨论】:

    • 我试过这个,但我得到以下错误。 Error 1 at line 32:75 : For attribute 'tunnel' within an xsl:apply-templates parameter, the only permitted value is 'no' Error 2 at line 31:61 : Invalid element &lt;xsl:param&gt; within xsl:apply-templates Error 3 at line 32:75 : xsl:param must be immediately within a template, function or stylesheet
    • 是否需要从&lt;xsl:template match="group-container" mode="second-level"&gt;&lt;xsl:template match="group-container" mode="first-level"&gt;内部传递参数?
    • @user2628187,对不起,我在xsl:apply-templates中使用了错误的xsl:param,来传递你需要的参数xsl:with-param
    • @user2628187,至于您在第二条评论中的问题,我不太确定它是否询问您是否需要传递参数或正在寻找替代方案,或者您是否想知道从哪个模板来传递它。
    【解决方案2】:

    你为什么不简单地做:

    <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>
    

    请注意,结果是一个 XML 片段,没有单个根元素。

    【讨论】:

    • 非常感谢@Martin Honnen 的帮助。
    • 我不是马丁·霍南。而且我仍然认为你让这件事变得比它需要的更复杂。
    • 不确定这是什么意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    相关资源
    最近更新 更多