xml - 转换输出 `>` 而不是 `>`

标签 xml xslt xpath

感谢您的帮助!我真的很感激!我明白为什么我需要将值放在节点中,但我不是在使用模板而是在使用函数...我只是想不通如何将这些节点和模板放在函数中?

如果我只显示我的 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">&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; ? disable-output-escaping= "yes"的值不起作用...

最佳答案

My XSLT code:

<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。

关于xml - 转换输出 `>` 而不是 `&gt;`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921101/

相关文章:

xml - Golang rss xml解析 <atom10 :link overrides <link>

java - Android:从 .java 文件设置 strings.xml 值

xsl 文档中的 css

javascript - 访问深度嵌套 DOM 节点的智能方法

html - 为什么 xpath 在 html 标签之外返回文本?

javascript - jquery读取xml子节点上的多个xml属性

c# - 可以使用 XmlAttributes 有选择地 XmlIgnore 吗?

xslt - 如何在 XSLT 中轻松生成唯一字符串?

javascript - 传递值 <xsl :value-of> to a java script function

java 。 Selenium 网络驱动程序。从表格中获取单元格值