2013-04-07 128 views
4

我有一個XML文檔:如何檢查是否元素節點包含XSL特定值

<?xml version="1.0" encoding="ISO-8859-1"?> 
<document> 
    <fruits> 
     <fruit id="1"> 
      <title>I like pineapples</title> 
      <description> a tropical plant with edible multiple fruit consisting of coalesced berries</description> 
     </fruit> 
     <fruit id="2"> 
      <title>I like watermelons</title> 
      <description>has a smooth exterior rind (green, yellow and sometimes white) and a juicy, sweet interior flesh</description> 
     </fruit> 
    </fruits> 
</document> 

我如何檢查title元素包含「菠蘿」,這樣我可以只顯示description該特定fruit

這裏是XSLT轉換我有:

<?xml version="1.0" encoding="iso-8859-1"?> 

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" omit-xml-declaration="yes" doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" 
      doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/> 
<xsl:template match="/"> 
    <xsl:element name="html"> 
     <xsl:element name="head">   
     <xsl:element name="title">Fruits</xsl:element> 
     </xsl:element> 

     <xsl:element name="body"> 
     <xsl:if test="/document/fruits/fruit/title[contains(text(),'pineapple')]"> 
      <xsl:value-of select="description"/> 
     </xsl:if> 
     </xsl:element> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

你有求索標題元素使用[contains(text(),'pineapple')]謂詞的紅色? – user998692 2013-04-07 13:12:44

+0

我試過 Sujal 2013-04-07 13:22:22

+0

在你的表達中,它應該是水果而不是fruis。而你的XML格式不正確,元素沒有正確關閉。如果你解決這些問題,我認爲它會奏效。 – user998692 2013-04-07 13:31:29

回答

6

這裏有一個稍微推驅動的方法來完成你想要的。

當這個XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 
    <xsl:output omit-xml-declaration="no" indent="yes" 
    doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" 
    doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd" /> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="/*"> 
    <html> 
     <head> 
     <title>Fruits</title> 
     </head> 
     <body> 
     <xsl:apply-templates 
      select="fruits/fruit[contains(title, 'pineapple')]" /> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="fruit"> 
    <xsl:apply-templates select="description" /> 
    </xsl:template> 
</xsl:stylesheet> 

......被應用於提供的XML:

<?xml version="1.0" encoding="utf-8"?> 
<document> 
    <fruits> 
    <fruit id="1"> 
     <title>I like pineapples</title> 
     <description>a tropical plant with edible multiple fruit 
     consisting of coalesced berries</description> 
    </fruit> 
    <fruit id="2"> 
     <title>I like watermelons</title> 
     <description>has a smooth exterior rind (green, yellow and 
     sometimes white) and a juicy, sweet interior 
     flesh</description> 
    </fruit> 
    </fruits> 
</document> 

......想要的結果產生:

<?xml version="1.0"?> 
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <title>Fruits</title> 
    </head> 
    <body>a tropical plant with edible multiple fruit consisting of coalesced berries</body> 
</html> 
+0

這種解決方案的作品,但它也顯示西瓜的標題和descritpion。產生的結果是: 熱帶植物,食用多種水果組成的融合漿果 我喜歡西瓜有光滑的外皮(綠色,黃色,有時是白色)和多汁,甜美的內部肉質 – Sujal 2013-04-07 17:45:13

+0

您使用的是什麼XSLT處理器?我已經使用過幾種(Expat,Saxon,libxslt等),並沒有得到你看到的結果。 – ABach 2013-04-07 17:46:25

+0

好的抱歉!你的解決方案工作,由@Dimitre Novatchev解決方案造成的結果。 – Sujal 2013-04-07 17:58:29

2

更簡單,幾乎完全 「推式」 的解決方案

<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="*"/> 

<xsl:template match="/*"> 
    <html> 
     <head> 
     <title>Fruits</title> 
     </head> 
     <body><xsl:apply-templates/></body> 
    </html> 
</xsl:template> 

<xsl:template match="fruit[contains(title, 'pineapple')]"> 
    <xsl:value-of select="description"/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當這種變換所提供的XML文檔應用:

<document> 
    <fruits> 
     <fruit id="1"> 
      <title>I like pineapples</title> 
      <description> a tropical plant with edible multiple fruit consisting of coalesced berries</description> 
     </fruit> 
     <fruit id="2"> 
      <title>I like watermelons</title> 
      <description>has a smooth exterior rind (green, yellow and sometimes white) and a juicy, sweet interior flesh</description> 
     </fruit> 
    </fruits> 
</document> 

想要的,正確的結果產生:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

     <title>Fruits</title> 
    </head> 
    <body> a tropical plant with edible multiple fruit consisting of coalesced berries</body> 
</html> 
+0

這種解決方案的作品,但它也顯示西瓜的標題和descritpion。產生的結果是: 熱帶植物與可食用的多種水果組成的融合漿果 我喜歡西瓜光滑的外皮(綠色,黃色,有時是白色)和多汁,甜美的內部肉類 – Sujal 2013-04-07 17:39:07

+0

@Sujal,對不起,有一個未成年人複製+粘貼問題。現在更正。 – 2013-04-07 17:50:05

相關問題