2011-01-27 63 views
2

我需要编写一个XSLT来获取给定节点的最近字典值的值。例如,我的结构可能是如下有条件的兄弟值

<rootnode> 
<rootcontainer> 
    <dictionary> 
    <key1> value /<key1> 
    </dictionary> 
    <pages> 
    <page1> 
    <!--xslt goes here--> 
    </page1> 
    </pages> 
</rootcontainer> 
<dictionary> 
    <key1> 
    independent value 
    </key1> 
    <key2> 
    value 2 
    </key2> 
</dictionary> 
</rootnode> 

我想创建变量$key1$key2page1$key1的值将是“值”,并且$key2的值将是“值2”。如果rootcontainer\dictionary\key1不存在,则$key1的值将是“独立值”。

我希望这是有道理的。

+0

好问题,+1。请参阅我的答案,了解最紧凑和最优雅的(迄今为止)单个单行XPath解决方案。 :) – 2011-01-27 20:32:17

+0

你的问题不清楚。为了*“把最近的字典值的值赋给给一个[n]节点”*,你需要提供输入样本和上下文节点。 *“在'page1`里面创建变量`$ key1`和`$ key2`”*对我来说没有意义,除非你想说你想输出变量`$ key1`和`$ key`的值到元素`page1`内容模板。 – 2011-01-28 18:16:06

回答

0

我不知道我理解的问题,但你可以用两种方法分配条件的值赋给变量:

<xsl:choose> 
    <xsl:when test="your test condition"> 
     <xsl:variable name="key1" select="your value"> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:variable name="key1" select="your alternative value"> 
    </xsl:otherwise> 
</xsl:choose> 

或者更succintly:

<xsl:variable name="key1" select="if(your test condition) then your value else your alternative value;"/> 

更新:谢谢以更新问题。我现在就放弃它。

<xsl:template match="page1"> 
<xsl:choose> 
    <xsl:when test="../../preceding-sibling:dictionary[1]/key1"> 
     <xsl:variable name="key1" select="../../preceding-sibling:dictionary[1]/key1"> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:variable name="key1" select="../../../following-sibling:dictionary[1]/key1"> 
    </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 

所以的$key1的值将是在前面的字典中的<key1>节点,如果有一个,并且在下面的字典中的<key1>节点如果不存在。那是对的吗?

(您可以使用if/then/else结构太多,如果你想,但我用xsl:choose,因为它可能更容易阅读。)

+0

嗨Biziclop ..感谢您的回应...我更新了贴出我的XML结构。希望能增加一点清晰度。我的问题是,这个key1中变量的名称应该取自字典下节点的名称,并且选择值应该是距离相关节点最近的key1值.... – 2011-01-27 19:48:33

2

这里是定义所需的变量,一个紧凑的方式:

<xsl:variable name="vKey1" select= 
    "(/*/rootcontainer/dictionary/key1 
    | 
    /*/dictionary/key1 
    ) 
    [1] 
    "/> 

<xsl:variable name="vKey2" select= 
    "(/*/rootcontainer/dictionary/key2 
    | 
    /*/dictionary/key2 
    ) 
    [1] 
    "/> 

当包裹在一个简单的XSLT样式表:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vKey1" select= 
    "(/*/rootcontainer/dictionary/key1 
    | 
    /*/dictionary/key1 
    ) 
    [1] 
    "/> 

<xsl:variable name="vKey2" select= 
    "(/*/rootcontainer/dictionary/key2 
    | 
    /*/dictionary/key2 
    ) 
    [1] 
    "/> 

<xsl:template match="/"> 
    Key1: <xsl:value-of select="$vKey1"/> 
    Key2: <xsl:value-of select="$vKey2"/> 
</xsl:template> 
</xsl:stylesheet> 

和施加在提供的XML文档(校正,因为它是严重畸形):

<rootnode> 
    <rootcontainer> 
     <dictionary> 
      <key1> value </key1> 
     </dictionary> 
     <pages> 
      <page1> </page1> 
     </pages> 
    </rootcontainer> 
    <dictionary> 
     <key1> independent value </key1> 
     <key2> value 2 </key2> 
    </dictionary> 
</rootnode> 

有用,正确的结果产生

Key1: value 
    Key2: value 2 

说明

该表达式:

(/*/rootcontainer/dictionary/key1 
| 
/*/dictionary/key1 
) 
    [1] 

装置

取(可能)的两个要素的节点集,并从中取在文档顺序的第一个。

因为这两个元素中的第二个以文档顺序出现,所以它将成为第一个(也是选定的),仅当围绕联合(|)运算符的两个XPath表达式中的第一个不选择任何元件。