2011-05-07 43 views
4

感謝您的幫助!對此,我真的非常感激!我明白爲什麼我需要將值放在節點中,但我沒有使用模板但使用了函數......我只是無法弄清楚如何將這些節點和模板放入函數中?轉換輸出`>`而不是`>`

如果我只顯示我的XSLT文件,可能會更容易。下面你可以找到該文件,並讓說,例如這可能是$字符串把它傳遞給函數(不轉換XML文件):

<img src="Afbeeldingen Hotpot/beer.jpg" alt="afbeelding van een beer" title="beer" width="170" height="144" style="display:block; margin-left:auto; margin-right:auto; text-align:center;" style="float:center;" /> 

這是XSLT文件的完整內容:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"  xmlns:foo="http://www.wathever.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs functx" 
xmlns:functx="http://www.functx.com"> 

<xsl:import href="alle-functx-functies.xsl"/> 

<xsl:function name="foo:functionIfImage"> 
    <xsl:param name="string" as="xs:string"/> 
     <xsl:if test="contains($string,'.jpg')"> 
      <xsl:sequence select="foo:functionImage($string,'.jpg')"/> 
     </xsl:if> 
     <xsl:if test="contains($string,'.png')"> 
      <xsl:sequence select="foo:functionImage($string,'.png')"/> 
     </xsl:if> 
     <xsl:if test="contains($string,'.gif')"> 
      <xsl:sequence select="foo:functionImage($string,'.png')"/> 
     </xsl:if> 
     <xsl:if test="not(contains($string,'.jpg')) and not(contains($string,'.png')) and not(contains($string,'.gif'))"> 
      <xsl:sequence select="'iumi ondersteund alleen afbeeldingen van het formaat *.jpg, *.png of *.gif'"/> 
     </xsl:if> 
     <xsl:if test="not(contains($string,'img src='))"> 
      <xsl:sequence select="'bevat geen img src='"/> 
     </xsl:if> 

</xsl:function> 
<xsl:function name="foo:functionImage"> 
    <xsl:param name="string" as="xs:string"/> 
    <xsl:param name="type" as="xs:string"/> 

    <xsl:variable name="quot">&quot;</xsl:variable> 
    <xsl:variable name="beforePath" as="xs:string">img src="</xsl:variable> 
    <xsl:variable name="globalPath" as="xs:string" select="substring-before(substring-after($string, $beforePath), $quot)" /> 
     <xsl:variable name="beforeWidth" as="xs:string">width="</xsl:variable> 
     <xsl:variable name="width" as="xs:string" select="substring-before(substring-after($string, $beforeWidth), $quot)" /> 
      <xsl:variable name="beforeHeight" as="xs:string">height="</xsl:variable> 
      <xsl:variable name="height" as="xs:string" select="substring-before(substring-after($string, $beforeHeight), $quot)" /> 

    <xsl:if test="not(contains($globalPath,'http'))"> 
     <xsl:variable name="fileName" as="xs:string"><xsl:sequence select="functx:substring-after-last($globalPath,'/')"/></xsl:variable> 
     <xsl:variable name="compatibleData" as="xs:string"> 
      <xsl:sequence select="concat('&lt;img source=&quot;images/',$fileName,'&quot;',' width=&quot;',$width,'&quot; height=&quot;',$height,'&quot; /&gt;')"/> 
     </xsl:variable> 
     <xsl:value-of disable-output-escaping= "yes" select="$compatibleData" /> 
    </xsl:if> 
    <xsl:if test="contains($globalPath,'http')"> 
     <xsl:variable name="compatibleData" as="xs:string"> 
      <xsl:sequence select="concat('&lt;img source=&quot;',$globalPath,'&quot;',' width=&quot;',$width,'&quot; height=&quot;',$height,'&quot; /&gt;')"/> 
     </xsl:variable> 
     <xsl:value-of disable-output-escaping= "yes" select="$compatibleData" /> 
    </xsl:if> 

</xsl:function> 
</xsl:stylesheet> 

所以這一塊我的XSLT代碼給我錯誤的輸出:

改造後
<xsl:variable name="compatibleData" as="xs:string"> 
      <xsl:sequence select="concat('&lt;img source=&quot;',$globalPath,'&quot;',' width=&quot;',$width,'&quot; height=&quot;',$height,'&quot; /&gt;')"/> 
</xsl:variable> 
<xsl:value-of disable-output-escaping= "yes" select="$compatibleData" /> 

輸出:

&lt;img source="images/beer.jpg" width="300" height="300" /&gt; 

輸出我想改造後:

<img source="images/beer.jpg" width="300" height="300" /> 

我怎樣才能使輸出說<代替&lt;,並>代替&gt;? value-of disable-output-escaping =「yes」不起作用...

+0

問得好,+1 。看到我的答案爲最短,最簡單,最容易理解/可維護的解決方案:) – 2011-05-07 23:03:43

回答

3

實際上,您可以在您的XSLT中輸入<br/>,並將其傳遞到結果中。

<MyElement> 
    <xsl:value-of select="/My/Element/Value"/> 
    <br/> 
</MyElement> 
+0

嗯,我不明白。所以我必須將
放入元素或節點中?其實這是一個簡化的代碼,我編輯了我的問題......答案是一樣的嗎? – user737917 2011-05-07 15:54:35

1

爲什麼你嘗試創建標籤定義爲xs:string的變量?這很容易與元素定義變量並使用它:

<xsl:variable name="compatibleData"> 
    <img source="images/{$filename}" width="{$width}" height="{$height}"/> 
</xsl:variable> 

<xsl:template match="something"> 
    <xsl:copy-of select="$compatibleData"/> 
</xsl:template> 
+0

+1爲一個很好的答案。 – 2011-05-08 16:48:00

-1

我想你可能會尋找:

<xsl:value-of disable-output-escaping= "yes" select="$compatibleData" /> 
+0

@Dimitre,1)你可能想閱讀「吃,拍和葉」2)不要根據你的喜好和不喜歡有效的替代方案。 – rsp 2011-05-08 08:33:33

+0

答案的價值無法通過某人的讚譽,喜好或不喜歡來改變。對於您提出的解決方案的缺點,請參閱@LarsH的答案。最大的缺點不僅在於它違背了XSLT處理模型的架構,而且因爲DOE不是XSLT的強制性功能,也不能保證特定的XSLT處理器完全支持它。據我所知,FF使用的XSLT處理器不支持DOE。基於此,能源部答案的任何倒退都是有根據的。 – 2011-05-08 16:46:20

4

您正試圖輸出的實際結構(轉義)XML,所以你需要或者

  • 提供的數據,XSLT作爲結構化的XML(如@Jeff斯文森說的),或
  • 提供的數據作爲XSLT逃脫XML(因爲你正在做的),然後禁用輸出轉義(如@rsp所述)。後者被認爲是讓XSLT做你想做而不真正理解正在發生的事情的一種骯髒的方式;它可能無法正常工作,具體取決於您的XSLT處理器以及控制序列化的內容。
+1

+1得到正確的答案。 – 2011-05-08 16:47:30

6

我的XSLT代碼:

<xsl:variable name="compatibleData" as="xs:string"><xsl:sequence select="concat('&lt;img source=&quot;images/',$fileName,'&quot;',' width=&quot;',$width,'&quot; height=&quot;',$height,'&quot; /&gt;')"/></xsl:variable> 
    <xsl:sequence select="$compatibleData"/> 

切勿逃避它破壞標記!

每當使用XSLT生成XML文檔時,它將輸出元素(和其他類型),節點 - 不是字符串。

使用

<img source="images/{$filename}" width="{$width}" height="{$height}" /> 

說明:使用AVT的(Attribute-Value-Templates)使得代碼短和更具有可讀性。預先知道元素和屬性名稱時始終使用AVT。

0

這裏是另一種選擇,因爲你不能用價值的情況下,W /禁用輸出轉義(即與-的副本一起):

<xsl:stylesheet...> 
    <xsl:output use-character-maps="special-chars"/> 
    <xsl:character-map name="special-chars"> 
    <xsl:output-character character=">" string="&gt;"/> 
    </xsl:character-map> 
相關問題