Xml 转换输出“>;”而不是`&;燃气轮机`

Xml 转换输出“>;”而不是`&;燃气轮机`,xml,xslt,xpath,Xml,Xslt,Xpath,谢谢你的帮助!我真的很感激!我理解为什么我需要将值放在节点中,但我不是在使用模板,而是在使用函数。。。我只是不知道如何将这些节点和模板放入函数中 如果只显示XSLT文件,可能会更容易。您可以在下面找到文件,例如,这可能是它传递给函数(未转换的XML文件)的$string: 这是XSLT文件的完整内容: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/

谢谢你的帮助!我真的很感激!我理解为什么我需要将值放在节点中,但我不是在使用模板,而是在使用函数。。。我只是不知道如何将这些节点和模板放入函数中

如果只显示XSLT文件,可能会更容易。您可以在下面找到文件,例如,这可能是它传递给函数(未转换的XML文件)的$string:

这是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>
转换后我想要的输出:

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

如何使输出显示
而不是

disable output escaping=“yes”的值不起作用…

您实际上可以在XSLT中键入

,它将传递到结果中

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



为什么尝试创建标记定义为
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>

我想您可能正在寻找:

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

您正试图输出实际的结构化(未缩放)XML,因此您需要

  • 将数据作为结构化XML提供给XSLT(正如@Jeff Swensen所说),或者
  • 将数据作为转义XML提供给XSLT(正如您所做的),然后禁用输出转义(如@rsp所说)。后者被认为是一种肮脏的方式,让XSLT做你想做的事情,而不真正了解发生了什么;它可能不起作用,这取决于XSLT处理器和控制序列化的内容
我的XSLT代码:


永远不要通过转义标记来销毁标记

每当使用XSLT生成XML文档时,它都会输出元素(以及其他类型的节点)——而不是字符串

使用

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


解释:使用AVT()使代码更短,可读性更强。当元素和属性名称事先已知时,始终使用AVT。

对于不能使用w/disable输出转义值(即,与的副本结合使用)的情况,这里有另一个选项:



嗯,我不明白。所以我必须把
放在元素或节点中?实际上这是一个简化的代码,我已经编辑了我的问题。。。那么答案是一样的吗?好问题,+1。关于最短、最简单、最容易理解/维护的解决方案,请参见我的答案:)@Dimitre,1)您可能想阅读“吃、射、叶”2)不要根据您的喜好否决有效的备选方案。答案的值不能因某人的否决票、喜欢或不喜欢而改变。关于您提出的解决方案的缺点,请参见@LarsH的答案。最大的缺点不仅是它不符合XSLT处理模型的体系结构,而且因为DOE不是XSLT的一个强制性功能,而且不能保证特定的XSLT处理器支持它。据我所知,FF使用的XSLT处理器不支持DOE。基于此,对能源部答案的任何否决都是有充分理由的。
<xsl:value-of disable-output-escaping= "yes" select="$compatibleData" />
<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"/> 
<img source="images/{$filename}" width="{$width}" height="{$height}" />