通过在一个XSL文件中引用该文件,从一个XML文件中检索属性

3
考虑我的“destinationURLLookUp.xml”文件具有以下结构。
           <?xml version="1.0" encoding="UTF-8"?>
               <destinationURLs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <destinationURL name="Default Page" value="/abc/ab"/>
                     <destinationURL name="Home Page" value="/abc/ac"/>

                </destinationURLs>

我正在使用以下XSLT代码在我的XSLT中加载此文件,并且必须检索正在处理的“值”的相应“名称”属性。
            <destinationURL> 

标签。

我尝试过的 XSLT 片段

        <xsl:variable name="prop" select="document('destinationURLLookUp.xml')"/>
                <xsl:variable name="rep2" select="$prop/destinationURLs/destinationURL[@value=@url]/@name"/>
                <xsl:value-of select="$rep2" />

其中@url是我将获取的URL值,该值与'value'属性匹配。

               <destinationURL> 

标签。我无法获得输出。我能获取相应的xpath来访问匹配的'value'属性的'name'属性吗?

以上是尝试使用$prop/destinationURLs/destinationURL[@value=@url]/@name。

2个回答

1

你的节点中没有url属性,所以@value=@url永远不会起作用。

如果你有一个从当前行使用的URL,请先将它放入一个变量中。

<xsl:variable name="url" select="@url"/>
<xsl:variable name="rep2" select="$prop/destinationURLs/destinationURL[@value=$url]/@name"/>

这显然是有效的,因为正确的上下文被隐式地用于第一个赋值变量。 - Emiliano Poggi

1

我认为您只是忘记明确指定当前内容的模板,用于@url属性。正确的XPath取决于您的指令所在的模板。

我说的是这样的东西:

<xsl:variable name="rep2" 
 select="$prop/destinationURLs/destinationURL[@value=current()/@url]/@name"/>

请注意 current()/@url

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接