雖然目前尚不清楚在所有來自這個問題你想做的事,這裏是一個答案,它的一部分:
是像這可能使用 XSLT/XPath 1.0中?我知道2.0有一個 令牌化功能,這將是 完美的,但不幸的是 我正在使用的CMS尚未 支持2.0。
令牌化已在XSLT 1.0中完成了很多年。儘管可以編寫自己的用於標記字符串的遞歸模板,但應該記住,FXSL庫中已經有了這樣一個解決方案,它可以保證工作,比典型的標記化實現更強大,並且不存在已知的錯誤 - 隨時可以使用。
這是str-split-to-words
模板,這裏是用它的一個典型的例子:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', 	 '"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
</xsl:template>
<xsl:template match="word">
<xsl:value-of select="concat(position(), ' ', ., ' ')"/>
</xsl:template>
</xsl:stylesheet>
當這種轉變是在下面的XML文檔應用:
<t>Sorry, kid, first-borns really are smarter.
First-borns are typically smarter, while
younger siblings get better grades and
are more outgoing, the researchers say</t>
的想要,正確的結果產生:
1 Sorry
2 kid
3 first-borns
4 really
5 are
6 smarter.
7 First-borns
8 are
9 typically
10 smarter
11 while
12 younger
13 siblings
14 get
15 better
16 grades
17 and
18 are
19 more
20 outgoing
21 the
22 researchers
23 say
請注意該模板接受名爲pDelimiters
的參數,其中可以指定多個分隔符。
更新:我終於明白了OP想要解決這個問題。這裏是我的解決方案,這再次使用str-split-to-words
模板標記化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<!-- to be applied upon: test-strSplit-to-Words2.xml -->
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vCategories">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select=
"/*/select-criteria/Categories"/>
<xsl:with-param name="pDelimiters"
select="'|'"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="*/pages/Page">
<xsl:with-param name="pCategories" select=
"ext:node-set($vCategories)"/>
<xsl:with-param name="pMatchType" select=
"*/select-criteria/MatchType"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Page">
<xsl:param name="pCategories"/>
<xsl:param name="pMatchType" select="any"/>
<xsl:variable name="vDecoratedCurrent"
select="concat('|', @CategoryIds, '|')"/>
<xsl:variable name="vSelected" select=
"$pCategories/*
[$pMatchType = 'any']
[contains($vDecoratedCurrent,
concat('|', ., '|')
)
][1]
or
not($pCategories/*[not(contains($vDecoratedCurrent,
concat('|', ., '|')
)
)
][1]
)
"/>
<xsl:copy-of select="self::node()[$vSelected]"/>
</xsl:template>
</xsl:stylesheet>
當這一轉型是這個XML文檔的應用:
<t>
<select-criteria>
<Categories>851|849</Categories>
<MatchType>any</MatchType>
</select-criteria>
<pages>
<Page CategoryIds="848|849|850|851">Page 1</Page>
<Page CategoryIds="849|850|">Page 2</Page>
<Page CategoryIds="848|850|">Page 3</Page>
<Page CategoryIds="848|849|850|851">Page 4</Page>
<Page CategoryIds="848|850|851">Page 5</Page>
<Page CategoryIds="848|849|850">Page 6</Page>
</pages>
</t>
想要的,正確的結果產生:
<Page CategoryIds="848|849|850|851">Page 1</Page>
<Page CategoryIds="849|850|">Page 2</Page>
<Page CategoryIds="848|849|850|851">Page 4</Page>
<Page CategoryIds="848|850|851">Page 5</Page>
<Page CategoryIds="848|849|850">Page 6</Page>
W XML文檔中的母雞我們指定:
<MatchType>all</MatchType>
我們再次得到了想要的,正確的結果:
<Page CategoryIds="848|849|850|851">Page 1</Page>
<Page CategoryIds="848|849|850|851">Page 4</Page>
| | http://www.javapractices.com/topic/TopicAction.do?Id=96 – garik 2010-08-13 08:11:26
符號化已在XSLT 1.0已經做了很多年,我想回答你的問題。唯一的問題是我完全*不知道你想實現什麼 - 請編輯你的問題並提供:1.源XML文檔(儘可能簡短)。 2.想要的結果。 3.解釋輸出的哪些部分必須根據輸入的哪些部分計算。 – 2010-08-13 12:43:48
好吧,現在你有兩個解決方案 - 亞歷杭德羅和我的 - 都工作,都好。我終於明白你想要什麼(對於你將來的問題,你表達問題的方式很難理解),並根據我的(高)標準提出了一個我認爲很好的解決方案。如果解決方案中的某些內容不明確並接受其中一個,現在就由您來提問。而只是說「謝謝你的努力」。 – 2010-08-13 23:51:50