我有,我必須找到模式,並把周圍的匹配特定模式的「標記的XML文件XSLT的xsl:分析字符串和正則表達式
<xml>
<foo>
<id>1</id>
<description>This is section 1, this is section 1 or 2, this is section 1 and 2</description>
</foo>
</xml>
現在文件我必須找到一個模式「第1節」,「第1節或第2節」和「第1節和第2節」,並在匹配單詞周圍加上「標記。
我寫了XSLT是這樣的:
<xsl:template match="text()">
<xsl:analyze-string select="." regex="section\s\d+|section\s\d+\s+or\s+\d|section\s\d+\s+and\s+\d">
<xsl:matching-substring>
<xsl:if test="matches(.,'section\s\d+')" >
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="matches(.,'section\s\d+\s+or\s+\d')" >
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="matches(.,'section\s\d+\s+and\s+\d')" >
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="current()"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
我這裏面臨的問題是,模式「部分1」中的所有其他模式相匹配,以及,所以我沒有得到期望的結果
上述變換結果是
<xml>
<foo>
<id>1</id>
<description>This is "section 1", this is "section 1" or 2, this is "section 1" and 2</description>
</foo>
</xml>
其中,因爲我想此輸出。
<xml>
<foo>
<id>1</id>
<description>This is "section 1", this is "section 1 or 2", this is "section 1 and 2"</description>
</foo>
</xml>
任何想法如何實現它...並獲得渴望的結果。
謝謝。
感謝大衛,它完美運作。只是想知道currenlty它僅限於「第1或第2節」,是否可以使它成爲通用的,例如它應該選擇「第1節或第2節或第3節」等。 – atif 2012-03-29 21:16:05
是的,只是改變?到一個*,也許改變(或|和)到(或|和|,),所以1,2或3應該工作以及(如果它可以接受和upvote你知道:-) – 2012-03-29 21:31:02