2011-05-07 36 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> 
相关问题