2010-03-19 87 views
2

我正在循环查看节点的值。XSLT:如果节点= A,则设置B = 1否则B = 2

If Node = B, then B has one of two possible meanings. 
    --If Node = A has been previously found in the file, then the value for A 
    should be sent as 1. 
    --If Node = A has NOT been found in the file, the the value for A should 
    be sent as 2. 

where file is the xml source to be transformed 

我想不出如何做到这一点。如果我正在使用允许变量重新分配/更改的编程语言,那么很容易。但是,使用XSLT变量设置一次。

回答

0

调查xsl:choose,xsl:when,xsl:if。你可以做

<xsl:if test="A=1"> 
Set in here 
</xsl:if> 

<xsl:choose> 
    <xsl:when test="A=1"> 
     <xsl:otherwise> 
</xsl:choose> 
+0

感谢您的回答,并介绍选择。但我想我没有清楚地解释我的问题。已发布另一个问题: XSLT:对于每个节点转换,如果A = 2和A = 1都找到了,那么可以这样做 – Larry 2010-03-19 15:41:47

8

您提供的代码有什么都没有做XSLT。在提出这些问题之前,请阅读一本关于XSLT的好书。

这里是做的非常著名的方式我猜是你的问题的含义是:

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

<xsl:template match="/"> 
    <xsl:variable name="vA"> 
    <xsl:choose> 
     <xsl:when test="//B">1</xsl:when> 
     <xsl:otherwise>2</xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 

    $vA = <xsl:value-of select="$vA"/> 
</xsl:template> 
</xsl:stylesheet> 

当下面的XML文档应用这种转变:

<c> 
    <d/> 
</c> 

结果是

$vA = 2 

当这个文档上施加:

<c> 
    <d> 
    <B/> 
    </d> 
</c> 

的reult是

$vA = 1 

有一个较短的方式,以获得相同的结果

<xsl:variable name="vA" select="not(//B) +1"/> 
+0

感谢您的回答,以及您的示例。我将铭记未来。但我想我没有清楚地解释我的问题。已发布另一个问题: XSLT:对于每个节点转换,如果A = 2和A = 1都找到了,那么可以这样做 – Larry 2010-03-19 15:42:35

+0

@Larry,SO搜索找不到您的新问题。 – 2016-10-14 14:08:06

相关问题