2015-09-10 42 views
-1

我有一個帶有布爾邏輯的XML結構,如下所示。我想生成各種不同的可能性和基本發展表達XSLT樣式表如何執行(a + b)(c + d)= ac + ad + bc + bd

(a + b)(c + d) = ac + ad + bc + bd 

如果解決方案適用於多層次即。 (e + f)或更多的條件,即(a + b + c),甚至更好。我嘗試了一些沒有成功的事情。

<Indicator operator="AND"> 
    <Indicator operator="OR"> 
     <IndicatorItem>a</IndicatorItem> 
     <IndicatorItem>b</IndicatorItem> 
    </Indicator> 
    <Indicator operator="OR"> 
     <IndicatorItem>c</IndicatorItem> 
     <IndicatorItem>d</IndicatorItem> 
    </Indicator> 
    <Indicator operator="OR"> 
     <IndicatorItem>e</IndicatorItem> 
     <IndicatorItem>f</IndicatorItem> 
    </Indicator> 
</Indicator> 
+1

這個問題並不完全清楚。我建議你給項目添加一些值,並告訴我們預期的結果。 –

+0

你是否期望表達式被返回,或者表達式被評估?給定的XML似乎與這個問題沒有關係。你說你嘗試了幾件事,你有什麼嘗試? – Abel

回答

2

如果我猜對了,你想枚舉一個OR表達式的所有組合。鑑於你的榜樣,其值增加了淨度:

XML

<Indicator operator="AND"> 
    <Indicator operator="OR"> 
     <IndicatorItem>a</IndicatorItem> 
     <IndicatorItem>b</IndicatorItem> 
    </Indicator> 
    <Indicator operator="OR"> 
     <IndicatorItem>c</IndicatorItem> 
     <IndicatorItem>d</IndicatorItem> 
    </Indicator> 
    <Indicator operator="OR"> 
     <IndicatorItem>e</IndicatorItem> 
     <IndicatorItem>f</IndicatorItem> 
    </Indicator> 
</Indicator> 

如下因素的樣式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="Indicator[@operator='AND']"> 
    <combinations> 
     <xsl:apply-templates select="Indicator[@operator='OR'][1]"/> 
    </combinations> 
</xsl:template> 

<xsl:template match="Indicator[@operator='OR']"> 
    <xsl:param name="prev"/> 
    <xsl:variable name="next" select="following-sibling::Indicator[@operator='OR']" /> 
    <xsl:for-each select="IndicatorItem"> 
     <xsl:variable name="this" select="concat($prev, .)" /> 
     <xsl:choose> 
      <xsl:when test="$next"> 
       <xsl:apply-templates select="$next[1]"> 
        <xsl:with-param name="prev" select="concat($this, ' AND ')"/> 
       </xsl:apply-templates> 
      </xsl:when> 
      <xsl:otherwise> 
       <combination> 
        <xsl:value-of select="$this"/> 
       </combination> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

將返回:

結果

<?xml version="1.0" encoding="UTF-8"?> 
<combinations> 
    <combination>a AND c AND e</combination> 
    <combination>a AND c AND f</combination> 
    <combination>a AND d AND e</combination> 
    <combination>a AND d AND f</combination> 
    <combination>b AND c AND e</combination> 
    <combination>b AND c AND f</combination> 
    <combination>b AND d AND e</combination> 
    <combination>b AND d AND f</combination> 
</combinations> 

這將爲任意數量的OR運營商和任何數量或運算項目工作。但是,它會而不是認爲是OR運算符的兄弟姐妹正在處理的任何AND運算符。

+0

我從來沒有從這個描述中猜出來,但是一個不錯的解決方案:) – Abel

+0

對不起,我的消息描述不清楚,但你說得對!這是非常優雅和偉大工程,令人印象深刻@ michael.hor257k,非常感謝。如何增加需要連接到所有組合的額外AND條件? '<指標運算符= 「AND」> X ý <指示燈操作者= 「OR」> b'/ IndicatorItem> ...' 應該產生類似 'x和y A和C e' 'x和y A和C F' – spirit

+1

@spirit你的基本思路 - 你自己做實際工作怎麼樣? - P.S.請不要在評論中張貼代碼,這是不可讀的。如果您遇到明顯不同的問題,請將其作爲新問題發佈(並關閉此問題)。 –

相關問題