2014-08-29 55 views
1

我有一个关于在我正在处理的项目上重构som XSLT代码的问题。在XSLT我现在我该如何重构这个XSLT

<pdf:sometext text="{$metadata/namespace:template 
        /namespace:template_collection 
        //namespace:option 
        [namespace:identificator 
         = $report_option_identifier] 
        /namespace:name}"/> 

的问题是,XSLT需要扩展打更多的节点(在原著XML的新版本,以略低改变了命名空间和略有改变标签)。

我想出了这个代码:

<xsl:variable name="report_template_collection" 
       select="$metadata 
         /namespace:template 
         /namespace:template_collection 
         | 
         $metadata 
         /namespace2:templateV2 
         /namespace2:template_collectionV2" /> 
<xsl:variable name="current_report_option" 
       select="namespace:option | namespace2:optionV2" /> 
<xsl:variable name="incomplete_report_option_text" 
       select="$report_template_collection 
         //$current_report_option 
         [namespace:identificator 
         = $report_option_identifier] 
         /namespace:name"/> 

<pdf:sometext text="{$incomplete_report_option_text}"/> 

但是在编译时它带有的部份错误:

Unexpected token '$' in the expression. $report_template_collection// -->$<-- current_report_option[namespace:opt...

所以我的问题是:我如何重构XSLT占新的命名空间(命名为V2)和另一个命名空间。重要的是相同的XSLT符合XML的所有版本(包括旧版本和新版本)。

在此先感谢!

+3

你的问题是相当无法回答的,因为它是现在。所有这些变量的目的是什么?您提到了不同版本的XML输入,但不显示任何内容。我建议您按照以下顺序编辑您的问题并显示以下内容:1个句子,解释您的目标是什么,出了什么问题,带有问题的_full_,最小XSLT样式表,XML输入以及您期望的XML输出。 – 2014-08-29 09:43:31

+0

错误消息告诉你,其中一个'select'属性的值不是XPath表达式。你的问题出现在表达部分......'// current_report_option' ... - 因为只有你知道你在那里说什么,只有你可以修复它。 – 2014-08-29 14:35:13

回答

0

对于xpath表达式中所做的事情,您不能引用$current_report_option。您不能像使用宏那样使用XSLT变量,这就是您尝试执行的操作。 $current_report_option的类型是一个节点集。

如果我正确地解释你的意图,你如何试图用(错误地)$current_report_option,你应该这样做以下代替:

<xsl:variable name="incomplete_report_option_text" 
       select="$report_template_collection 
         //*[self::namespace:option or 
          self::namespace2:optionV2] 
         [namespace:identificator 
         = $report_option_identifier] 
         /namespace:name"/> 

我换成你的$current_report_option使用与节点测试检查旧的或新的选项节点类型。

1

形式为A/B/$C的XPath表达式在XPath 2.0中实际上是合法的,但在XPath 1.0中不合法。但它可能并不意味着你的想法。像@ewh我怀疑(没有任何证据),你在想象如果变量$C绑定到表达式D|E,那么A/B/$C是另一种写作方式A/B/(D|E)。情况并非如此; $C绑定到一个值(一系列节点),而不是一个表达式。

你可以使用一个函数,而不是一个变量:

<xsl:function name="f:current_report_option"> 
    <xsl:param name="node" as="node()"/> 
    <xsl:sequence select="$node/(D|E)"/> 
</xsl:function> 

<xsl:variable name="X" select="A/B/f:current_report_option(.)"/>