转换输出“>”而不是“>”

发布于 2024-11-05 19:21:04 字数 4748 浏览 0 评论 0原文

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

如果我只显示我的 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;> ; 而不是<代码>>? 禁用输出转义=“是”的值不起作用...

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):

&#x003C;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;" /&#x003E;

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

红墙和绿瓦 2024-11-12 19:21:04

我的 XSLT 代码:

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

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"/> 

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:

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

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.

奶茶白久 2024-11-12 19:21:04

您正在尝试输出实际的结构化(未转义)XML,因此您需要将

  • 数据作为结构化 XML 提供给 XSLT(如@Jeff Swensen 所说),或者
  • 将数据作为转义 XML 提供给 XSLT(正如您所做的那样),然后禁用输出转义(如 @rsp 所说)。后者被认为是一种肮脏的方式,让 XSLT 在没有真正了解正在发生的事情的情况下执行您想要的操作;它可能不起作用,具体取决于您的 XSLT 处理器以及控制序列化的内容。

You are trying to output actual structured (unescaped) XML, so you need to either

  • provide the data to XSLT as structured XML (as @Jeff Swensen said), or
  • provide the data to XSLT as escaped XML (as you are doing) and then disable escaping on output (as @rsp said). This latter is considered a dirty way of getting XSLT to do what you want without really understanding what's going on; and it may not work, depending on your XSLT processor and what is controlling serialization.
花辞树 2024-11-12 19:21:04

实际上,您只需在 XSLT 中输入
即可,它就会传递到结果中。

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

You can actually just type the <br/> into your XSLT and it will pass through into the result.

<MyElement>
  <xsl:value-of select="/My/Element/Value"/>
  <br/>
</MyElement>
〆凄凉。 2024-11-12 19:21:04

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

Why you try to create variable with tag definition as xs:string? It's easy to define variable with element and use it:

<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>
苍风燃霜 2024-11-12 19:21:04

对于无法使用 value-of w/disable-output-escaping(即与 copy-of 结合使用)的情况,这是另一个选项:

<xsl:stylesheet...>
  <xsl:output use-character-maps="special-chars"/>
  <xsl:character-map name="special-chars">
    <xsl:output-character character=">" string=">"/>
  </xsl:character-map>

Here is another option, for the case where you cannot use value-of w/ disable-output-escaping (i.e., in conjunction with copy-of):

<xsl:stylesheet...>
  <xsl:output use-character-maps="special-chars"/>
  <xsl:character-map name="special-chars">
    <xsl:output-character character=">" string=">"/>
  </xsl:character-map>
横笛休吹塞上声 2024-11-12 19:21:04

我想您可能正在寻找:

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

I think you might be looking for:

<xsl:value-of disable-output-escaping= "yes" select="$compatibleData" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文