转换输出“>”而不是“>”
感谢您的帮助!我真的很感激!我明白为什么我需要将值放入节点中,但我不是使用模板而是使用函数...我只是不知道如何将这些节点和模板放入函数中?
如果我只显示我的 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" />
如何使输出显示 <
而不是 <
和 > ;
而不是<代码>>? 禁用输出转义=“是”的值不起作用...
Thanks for your help! I really appreciate it! I understand why I need to put the values in nodes, but I'm not working with templates but with functions... I just can't figure out how to put these nodes and templates in a function?
Maybe it's easier if I just show my XSLT file. beneath you can find the file, and let's say for example this could be the $string it passes to the function (of the not transformed XML file):
<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;" />
This is the complete content of the XSLT file:
<?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>
So this piece of my XSLT code gives me the wrong output:
<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" />
Output after transformation:
<img source="images/beer.jpg" width="300" height="300" />
Output I want after transformation:
<img source="images/beer.jpg" width="300" height="300" />
How can I make the output say <
instead of <
, and >
instead of >
?
value-of disable-output-escaping= "yes" does not work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
永远不要通过转义来破坏标记!!!
每当使用 XSLT 生成 XML 文档时,它都会输出元素(和其他类型)、节点——而不是字符串。
使用:
说明:AVT的使用(Attribute-Value-Templates)使代码更短、更具可读性。当元素和属性名称预先已知时,始终使用 AVT。
Never destroy markup by escaping it!!!
Whenever XSLT is used to generate an XML document, it outputs element (and other types of), nodes -- not strings.
Use:
Explanation: The use of AVT (Attribute-Value-Templates) makes the code shorter and more readable. Always use AVTs when the element and attribute names are known in advance.
您正在尝试输出实际的结构化(未转义)XML,因此您需要将
You are trying to output actual structured (unescaped) XML, so you need to either
实际上,您只需在 XSLT 中输入
即可,它就会传递到结果中。You can actually just type the
<br/>
into your XSLT and it will pass through into the result.为什么尝试创建标签定义为
xs:string
的变量?使用元素定义变量并使用它很容易:Why you try to create variable with tag definition as
xs:string
? It's easy to define variable with element and use it:对于无法使用 value-of w/disable-output-escaping(即与 copy-of 结合使用)的情况,这是另一个选项:
Here is another option, for the case where you cannot use value-of w/ disable-output-escaping (i.e., in conjunction with copy-of):
我想您可能正在寻找:
I think you might be looking for: