感谢您的帮助!我真的很感激!我明白为什么我需要将值放在节点中,但我不是在使用模板而是在使用函数...我只是想不通如何将这些节点和模板放在函数中?
如果我只显示我的 XSLT 文件,也许会更容易。您可以在下面找到该文件,例如,这可能是它传递给函数(未转换的 XML 文件)的 $string:
<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">"</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('<img source="images/',$fileName,'"',' width="',$width,'" height="',$height,'" />')"/>
</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('<img source="',$globalPath,'"',' width="',$width,'" height="',$height,'" />')"/>
</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('<img source="',$globalPath,'"',' width="',$width,'" height="',$height,'" />')"/>
</xsl:variable>
<xsl:value-of disable-output-escaping= "yes" select="$compatibleData" />
转换后的输出:
<img source="images/beer.jpg" width="300" height="300" />
转换后我想要的输出:
<img source="images/beer.jpg" width="300" height="300" />
我怎样才能让输出说<
而不是 <
, 和 >
而不是 >
?
disable-output-escaping= "yes"的值不起作用...
最佳答案
My XSLT code:
<xsl:variable name="compatibleData" as="xs:string"><xsl:sequence select="concat('<img source="images/',$fileName,'"',' width="',$width,'" height="',$height,'" />')"/></xsl:variable>
<xsl:sequence select="$compatibleData"/>
永远不要通过转义来破坏标记!!!
每当使用 XSLT 生成 XML 文档时,它都会输出元素(和其他类型的)、节点——而不是字符串。
使用:
<img source="images/{$filename}" width="{$width}" height="{$height}" />
说明:使用AVT( Attribute-Value-Templates )使得代码更短,更易读。当元素和属性名称事先已知时,始终使用 AVT。
关于xml - 转换输出 `>` 而不是 `>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921101/