我有一個如下所示的 XML 檔案。
<paper>
<Question_01>
<QNo>1a</QNo>
<Question>What is Semantic Web? </Question>
</Question_01>
<Question_01>
<QNo>1b</QNo>
<Question>“Web 2.0 applications can be used to increase the profit of a business“ Discuss.</Question>
</Question_01>
<Question_01>
<QNo>1c</QNo>
<Question>Discuss the advantage of the Extensible Markup Language.</Question>
</Question_01>
</paper>
我想選擇從“W”開始的問題,因為 QNo - 1a 和 1b 應該是輸出 XSLT。所以我在 XSL 中撰寫了以下代碼。
<xsl:for-each select = "paper/Question_01">
<xsl:if test = "starts-with(Question, 'W')">
<li>
<xsl:value-of select = "Question"/>
</li>
</xsl:if>
</xsl:for-each>
但它只選擇 QNo - 1a。我如何撰寫 XSL 代碼,因為它同時選擇了 QNo - 1a 和 1b,并且好像相關句子可以以“W”后面的幾個特殊字符和空格開頭?謝謝你。
uj5u.com熱心網友回復:
您可以使用該translate功能來洗掉不需要的字符。但是你必須列出它們。這是給定示例的條件示例:
starts-with(normalize-space(translate(Question,'“','')), 'W')
因此,將所有不需要的字符添加到translate函式的適當引數中。這可能是僅針對幾個字符的首選方法。但是對于很多字符,不同的策略可能更適合(不一定是 XSLT-1.0 impl)。
您還可以將整個代碼片段簡化為
<xsl:for-each select = "paper/Question_01[starts-with(normalize-space(translate(Question,'“','')), 'W')]">
<li>
<xsl:value-of select = "Question"/>
</li>
</xsl:for-each>
兩者的輸出應該相同。
uj5u.com熱心網友回復:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select = "paper/Question_01">
<xsl:if test = "starts-with(normalize-space(translate(Question,'“','')), 'W')">
<li>
<xsl:value-of select = "Question"/>
</li>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/534585.html
標籤:XMLxslt
