【问题标题】:Matching different node handles to get a node value with XSLT使用 XSLT 匹配不同的节点句柄以获取节点值
【发布时间】:2011-06-28 15:30:52
【问题描述】:

可用的 XSLT 是 1.0。

我正在使用基于 XML 的 CMS (Symphony CMS) 中的双语言网站工作,并且需要将类别名称的英文版本替换为法语版本。

这是我的源 XML。

<data>
    <our-news-categories-for-list-fr>
        <entry id="118">
            <title-fr handle="technology">Technologie</title-fr>
        </entry>
        <entry id="117">
            <title-fr handle="healthcare">Santé</title-fr>
        </entry>
    </our-news-categories-for-list-fr>
    <our-news-article-fr>
        <entry id="100">
            <categories>
                <item id="117" handle="healthcare" section-handle="our-news-categories" section-name="Our News categories">Healthcare</item>
                <item id="118" handle="technology" section-handle="our-news-categories" section-name="Our News categories">Technology</item>
            </categories>
            <main-text-fr mode="formatted"><p>Blah blah</p></main-text-fr>
        </entry>
    </our-news-article-fr>
</data>

这是我目前用于法语版的 XSLT 的一部分。

<xsl:template match="data">
    <xsl:apply-templates select="our-news-article-fr/entry"/>
</xsl:template>

<xsl:template match="our-news-article-fr/entry">
    <xsl:if test="categories/item">
        <p class="category">In:</p>
        <ul class="category">
            <xsl:for-each select="categories/item">
                <li><a href="{/data/params/root}/{/data/params/root-page}/our-news/categorie/{@handle}/"><xsl:value-of select="."/></a></li>
            </xsl:for-each>
        </ul>
    </xsl:if>
</xsl:template match>

问题:锚点 (&lt;xsl:value-of select="."/&gt;) 的可见文本给出了类别标题的英文版本。

以下节点的句柄匹配(所有句柄都是英文),所以我想我应该能够匹配另一个。

/data/our-news-categories-for-list-fr/entry/title-fr/@handle(title-fr节点的值是类别标题的法语翻译)

/data/our-news-article-fr/entry/categories/item/@handle

我是 XSLT 的新手,正在努力寻找如何做到这一点。

非常感谢。

【问题讨论】:

  • Santé 不能是有效标签?
  • @user639175 谢谢 - 已更正。
  • +1 好问题。请参阅我的答案以了解如何使用简单的 XPath 位置路径来执行此操作。

标签: xslt matching xslt-1.0 symphony-cms


【解决方案1】:

&lt;xsl:key name="k1" match="our-news-categories-for-list-fr/entry" use="@id"/&gt; 添加为XSLT 样式表元素的子元素。然后使用例如&lt;li&gt;&lt;a href="{/data/params/root}/{/data/params/root-page}/our-news/categorie/{@handle}/"&gt;&lt;xsl:value-of select="key('k1', @id)/title-fr"/&gt;&lt;/a&gt;&lt;/li&gt;.

【讨论】:

    【解决方案2】:
    ../our-news-categories-for-list-fr/entry/title-fr/text() instead of @handle should do it 
    
    Your problem is that you are in 
       <our-news-article-fr> 
    and need to reference
       <our-news-categories-for-list-fr>
    

    所以我做了一个父 .. 走上树然后走下入口节点

    【讨论】:

    • our-news-categories-for-list-fr 中有多个 entry 节点 - 如何根据我所在节点的句柄获得正确的节点?
    • ../our-news-categories-for-list-fr/entry/title-fr[@id = current()/@id]/text()
    【解决方案3】:

    xsl:for-each 重复指令中,上下文是our-news-article-fr/entry/categories/item。如果您使用.,则选择当前上下文,这就是您在那里收到英文版本的原因。

    另一种方法(不是说最简单和最好的方法)是简单地指定一个 XPath 表达式来定位正确的节点。您可以使用ancestor:: 轴从当前上下文转到data,然后使用您的测试节点。所需的谓词必须使用current() 函数匹配当前上下文:

    <xsl:value-of select="
        ancestor::data[1]/
         our-news-categories-for-list-fr/
          entry/
           title-fr
           [@handle=current()/@handle]
     "/>
    

    如果data 是您文档的根目录,您显然可以使用绝对位置路径:

         /
         data/
          our-news-categories-for-list-fr/
           entry/
            title-fr
            [@handle=current()/@handle]
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 2021-08-24
      • 2012-08-22
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      相关资源
      最近更新 更多