我对XSLT很陌生,对它的语法还不太熟悉。
我有一个变量(name="pageName"),它包含中的一个页面元素。例如:
/data/full-tree/page@handle='en'/page@handle='learn'/page@handle='tutorials‘
在html页面中显示指向此页面的链接之前,我希望确保页面元素存在,因此我编写了以下if测试:
<xsl:if test="$pageName" >
<a>
<xsl:attribute name="href" >
<xsl:value-of select='$theLink' />
</xsl:attribute>
Click here
</a>
</xsl:if>但是它并不像预期的那样工作,因为即使xpath所指向的页面元素不在树中,测试结果也始终是正确的。另一方面,当我将变量的内容直接写入测试变量时,如下所示:
<xsl:if test="/data/full-tree/page[@handle='en']/page[@handle='learn']/page[@handle='tutorials']
<a>
<xsl:attribute name="href" >
<xsl:value-of select='$theLink' />
</xsl:attribute>
Click here
</a>
</xsl:if> 当页面元素存在时,它似乎工作并显示链接,但当页面元素不存在时,则不显示链接。
我的问题是如何正确地使用变量?
编辑
我将尽量使我的上下文更详细。
xml源树(表示网站的结构)如下所示
<data>
<full-tree>
<page handle="root" id="12">
<name>Root Page</name>
<types>
<type>index</type>
</types>
</page>
<page handle="en" id="1">
<name>EN Root</name>
<types>
<type>en</type>
</types>
<page handle="home" id="3">
<name>Home</name>
<types>
<type>en</type>
</types>
</page>
<page handle="about" id="7">
<name>About</name>
<types>
<type>en</type>
</types>
<page handle="about-zoraldia" id="8">
<name>About zoraldia</name>
<types>
<type>en</type>
</types>
</page>
</page>
<page handle="learn" id="21">
<name>Learn</name>
<types>
<type>en</type>
</types>
<page handle="tutorials" id="22">
<name>Tutorials</name>
<types>
<type>en</type>
</types>
<page handle="symphony" id="24">
<name>Symphony</name>
<types>
<type>en</type>
</types>
<page handle="create-site" id="23">
<name>Create a bilingual Web Site with the Symphony CMS</name>
<types>
<type>en</type>
<type>web-site</type>
</types>
</page>
<page handle="symphony-menu-page" id="48">
<name>Add level 3 and 4 Menu Pages</name>
<types>
<type>en</type>
<type>symphony</type>
</types>
</page>
</page>
</page>
</page>
<page handle="think" id="49">
<name>Think</name>
<types>
<type>en</type>
</types>
<page handle="free-software" id="50">
<name>Free Software</name>
<types>
<type>en</type>
</types>
<page handle="miscellaneous" id="51">
<name>Miscellaneous Articles</name>
<types>
<type>en</type>
</types>
<page handle="philo" id="52">
<name>Philosophy and Values of Free Software</name>
<types>
<type>en</type>
</types>
</page>
</page>
</page>
</page>
</page>
<page handle="maintenance" id="9">
<name>Maintenance notification</name>
<types>
<type>maintenance</type>
</types>
</page>
</full-tree>
<data>这代表英文页,但法语中也有一个待定结构。我想要做的是,当访问者在一个法语页面,显示一个EN链接,只有当页面已经翻译。
因此,我使用了法语路径(例如/ fr /learn/tutorials ),只需将fr与en交换,从而将/en/learn/tutorials转换为$enLink变量。
我的目标是显示这个链接,只有当页面存在时才能正常工作。
我使用的变量的定义是
<xsl:variable name="pending">
<xsl:call-template name="build-xpath">
<xsl:with-param name="output-string" select="'/data/full-tree'" />
<xsl:with-param name="input-string" select="substring-after($enLink,'/')" />
</xsl:call-template>
</xsl:variable>build算法如下:
<xsl:template name="build-xpath" >
<xsl:param name="output-string" />
<xsl:param name="input-string" />
<xsl:choose>
<xsl:when test="contains($input-string, '/')" >
<xsl:variable name="new-output">
<xsl:value-of select="$output-string" />/page[@handle='<xsl:value-of select="substring-before($input-string,'/')"/>']
</xsl:variable>
<xsl:variable name="new-input" >
<xsl:value-of select="substring-after($input-string,'/')" />
</xsl:variable >
<xsl:call-template name="build-xpath" >
<xsl:with-param name="output-string" select="$new-output" />
<xsl:with-param name="input-string" select="$new-input" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$output-string"/>/page[@handle='<xsl:value-of select="$input-string"/>']
</xsl:otherwise>
</xsl:choose>
</xsl:template>该算法返回一个挂起变量(我最初称为pageName )。
用
<xsl:value-of select="$pending" />我看到绳子了
/data/full-tree/page@handle='en'/page@handle='learn'/page@handle='tutorials‘
这就是我对算法的期望。
让我陷入麻烦的是,当我将这个字符串放入另一个变量时,anotherVar说
<xsl:value-of select="$anotherVariable" 不显示字符串,而是显示节点的内容,在本例中:
教程
这似乎很正常,但是为什么在$pending中显示的是字符串本身呢?
发布于 2014-09-20 16:13:27
因为变量是字符串,所以在布尔测试中计算它总是返回true,因为只有空字符串的计算结果为false。
为了将变量计算为节点,首先需要将字符串转换为Xpath表达式。这可以在XSLT3.0中本地完成。在以前的版本中,您需要使用扩展函数,比如EXSLT:()函数(如果处理器支持EXSLT:)。
可能有一种更简单的方法来处理这个问题,但是我们没有看到足够的整个图片来确定(我在您的输入中没有看到任何法语元素)。例如,我怀疑使用一些公共ID来测试相应节点的存在会更加简单。
https://stackoverflow.com/questions/25949162
复制相似问题