2011-06-15 43 views
13

爲了比較xml字符串值與多個字符串,我正在執行以下操作。如何與xslt中的多個字符串進行比較

<xsl:if test="/Lines/@name = 'John' or /Lines/@name = 'Steve' or /Lines/@name = 'Marc' " > 

任何一個可以告訴我,而不是使用「或」在上述情況下,我怎麼能檢查一個字符串是否以一套使用XSLT字符串的現有。

謝謝。

+0

好問題,+1。查看我對三種不同解決方案的回答,其中兩個解決方案適用於XSLT 1.0。 :) – 2011-06-15 12:48:24

回答

20

方式三:

  1. 使用管道(或其他適當的字符)分隔字符串

...

<xsl:template match= 
    "Lines[contains('|John|Steve|Mark|', 
        concat('|', @name, '|') 
       ) 
     ] 
    "> 
    <!-- Appropriate processing here --> 
</xsl:template> 

0.2 。 針對外部傳遞的參數進行測試。如果參數沒有外部設定,以及我們使用XSLT 1.0時,xxx:node-set()擴展功能需要使用到它轉換成普通節點集,訪問前的孩子

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

<!-- externally-specified parameter --> 
<xsl:param name="pNames"> 
    <n>John</n> 
    <n>Steve</n> 
    <n>Mark</n> 
</xsl:param> 

<xsl:template match="Lines"> 
    <xsl:if test="@name = $pNames/*"> 
    <!-- Appropriate processing here --> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

0.3。 在XSLT 2.0比對串序列

<xsl:template match="Lines[@name=('John','Steve','Mark')]"> 
    <!-- Appropriate processing here --> 
</xsl:template> 
+1

對於真正簽約的XSLT 1.0解決方案(#1)+1。 – 2011-06-15 12:54:11

+0

@empo:謝謝。 「真的簽約」是什麼意思? – 2011-06-15 12:56:29

+0

我的意思是「短」:)對不起我的英語。 – 2011-06-15 13:01:04

3

是的 - 我用子 - 把所有你的名字在一個字符串 - XSL:變量 - 那麼,如果包含真正的名字是有

例如

<xsl:variable name="months">**janfebmaraprmajjunjulaugsepoktnovdec</xsl:variable> 


<xsl:if test="contains($months,'feb')"> do stuff ... 
+0

這當然不會在我的情況下工作:( – Lamps 2011-06-15 11:55:17

+0

@Bhaskar - 我不明白爲什麼不,你需要適應它,並可能在可能的值之間添加分隔符,但它應該也一樣對於列表中的名稱列表的月份 – Spudley 2011-06-15 12:48:52

+0

是的,增加一個分隔符做的伎倆:) – Lamps 2011-06-16 13:23:26

1

對於空格分隔的話,你可以使用index-of(tokenize("list of allowed", "\s+"), "needle"))match去正則表達式,但我敢肯定有什麼比這更聰明。

+0

不,這正是我將使用時,列表是一個分隔字符串。 – 2018-01-05 15:46:30

9

XSLT 2.0只:<xsl:if test="/Lines/@name = ('John', 'Steve', 'Marc')">

隨着XSLT 1.0不能寫入表示字符串序列或一組字符串的,但如果你知道文字值,則可以構造一個組的節點例如文本表達式

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0" 
    xmlns:data="http://example.com/data" 
    exclude-result-prefixes="data"> 

    <data:data xmlns=""> 
    <value>John</value> 
    <value>Steve</value> 
    <value>Marc</value> 
    </data:data> 

    <xsl:variable name="values" select="document('')/xsl:stylesheet/data:data/value"/> 

    <xsl:template match="..."> 
    <xsl:if test="/Lines/@name = $values">..</xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 
+0

謝謝,有沒有其他的方式來做到這一點在1.0? – Lamps 2011-06-15 12:17:13

+0

謝謝,我會試試這個。 (可能只是明天) – Lamps 2011-06-15 13:17:01

+0

使用文檔('')/ xsl:stylesheet代替node-set()! – Vlax 2012-08-29 09:00:09

1

其他方法可行:

的XPath 2.0(XSLT 2.0)

matches(/Lines/@name, 'John|Steve|Marc') 

XSLT 1.0你有類似的功能matchesEXSLT提供。

注意

這不是確切對陣字符串,但正則表達式匹配,而你的情況似乎無論如何合適。

+0

functx示例錯誤 - 僅XSLT 2.0 - 請更正。 – 2011-06-15 12:55:17

+0

@ Dimitre-Novatchev:謝謝,更正爲EXSLT。 – 2011-06-15 12:57:28

+0

@ DImitre-Novatchev:認爲FunctX在XSLT 1.0中工作,即使從未嘗試過。 XPath 2.0函數有什麼區別呢? – 2011-06-15 13:04:09

相關問題