2010-03-19 88 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

相關問題