Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transformation output `>` instead of `>`

Tags:

xml

xslt

xpath

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">&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...

like image 778
user737917 Avatar asked Dec 13 '22 13:12

user737917


1 Answers

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.

like image 124
Dimitre Novatchev Avatar answered Dec 30 '22 03:12

Dimitre Novatchev