开发者

Transformation output `>` instead of `>`

开发者 https://www.devze.com 2023-03-04 04:17 出处:网络
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 no

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 thi开发者_运维技巧s 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">&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>

So this piece of my XSLT code gives me the wrong output:

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

Output after transformation:

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

Output I want after transformation:

<img source="images/beer.jpg" width="300" height="300" />

How can I make the output say < instead of &lt;, and > instead of &gt;? value-of disable-output-escaping= "yes" does not work...


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

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.


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.


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>


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>


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="&gt;"/>
  </xsl:character-map>


I think you might be looking for:

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

精彩评论

暂无评论...
验证码 换一张
取 消