Changeset 4439


Ignore:
Timestamp:
02/05/14 10:18:37 (10 years ago)
Author:
andmor
Message:

Revised xquery escaping technique.
sloot.xquery now needs a wrapping <query> element around the xquery text.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cats/shared/urn.org.isocat.sloot/trunk/tools/xquery.dpml

    r4375 r4439  
    2323                        <literal type="xml">
    2424                                <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
     25                                        <xsl:import href="active:sloot.xquery-wrapers.escape"/>
    2526                                        <xsl:output method="text"/>
    2627                                        <xsl:param name="vararg"/>
    2728                                        <xsl:param name="debug"/>
    2829                                        <xsl:variable name="NL" select="system-property('line.separator')"/>
    29                                         <xsl:template match="/">
    30                                                 <xsl:variable name="root" select="*"/>
     30                                        <xsl:template match="query">
     31                                                <xsl:variable name="current-node" select="."/>
    3132                                                <xsl:variable name="query">
    3233                                                        <!-- turn the namespaces in scope of the root element into 'declare namespace' statements -->
    3334                                                        <xsl:for-each
    34                                                                 select="in-scope-prefixes($root)[not(.=('xml','xs','xsi','fn','local'))]">
     35                                                                select="in-scope-prefixes($current-node)[not(.=('xml','xs','xsi','fn','local'))]">
    3536                                                                <xsl:variable name="prefix" select="current()"/>
    3637                                                                <xsl:choose>
     
    3839                                                                                <xsl:text>declare default namespace "</xsl:text>
    3940                                                                                <xsl:value-of
    40                                                                                         select="namespace-uri-for-prefix($prefix,$root)"/>
     41                                                                                        select="namespace-uri-for-prefix($prefix,$current-node)"/>
    4142                                                                                <xsl:text>";</xsl:text>
    4243                                                                                <xsl:value-of select="$NL"/>
     
    4748                                                                                <xsl:text> = "</xsl:text>
    4849                                                                                <xsl:value-of
    49                                                                                         select="namespace-uri-for-prefix($prefix,$root)"/>
     50                                                                                        select="namespace-uri-for-prefix($prefix,$current-node)"/>
    5051                                                                                <xsl:text>";</xsl:text>
    5152                                                                                <xsl:value-of select="$NL"/>
     
    6162                                                                        />]</xsl:message>
    6263                                                                </xsl:if>
    63 
    6464                                                                <xsl:text>declare variable $</xsl:text>
    6565                                                                <xsl:value-of select="$var"/>
     
    6767                                                                <xsl:value-of select="$NL"/>
    6868                                                        </xsl:for-each>
    69                                                         <xsl:apply-templates/>
     69                                                        <xsl:for-each select="./node()">
     70                                                                <xsl:choose>
     71                                                                        <xsl:when test="self::text()">
     72                                                                                <xsl:apply-templates select="."/>
     73                                                                        </xsl:when>
     74                                                                        <xsl:otherwise>
     75                                                                                <xsl:apply-templates select="." mode="escape"/>
     76                                                                        </xsl:otherwise>
     77                                                                </xsl:choose>
     78                                                        </xsl:for-each>
    7079                                                </xsl:variable>
    7180                                                <xsl:if test="$debug">
     
    7382                                                </xsl:if>
    7483                                                <xsl:value-of select="$query"/>
    75                                         </xsl:template>
    76                                         <!-- turn an element node into an escaped XML string -->
    77                                         <xsl:template match="*">
    78                                                 <xsl:text>&lt;</xsl:text>
    79                                                 <xsl:value-of select="name()"/>
    80                                                 <xsl:apply-templates select="@*"/>
    81                                                 <xsl:text>&gt;</xsl:text>
    82                                                 <xsl:apply-templates/>
    83                                                 <xsl:text>&lt;/</xsl:text>
    84                                                 <xsl:value-of select="name()"/>
    85                                                 <xsl:text>&gt;</xsl:text>
    86                                         </xsl:template>
    87                                         <!-- turn an attribute node into an escaped XML string -->
    88                                         <xsl:template match="@*">
    89                                                 <xsl:text> </xsl:text>
    90                                                 <xsl:value-of select="name()"/>
    91                                                 <xsl:text>="</xsl:text>
    92                                                 <xsl:value-of
    93                                                         select="replace(replace(replace(replace(replace(.,'&amp;','&amp;amp;'),''&apos;','&amp;apos;'),'&lt;','&amp;lt;'),'&gt;','&amp;gt;'),'&quot;','&amp;quot;')"/>
    94                                                 <xsl:text>"</xsl:text>
    95                                         </xsl:template>
    96                                         <!-- turn a text node into an escaped XML string -->
    97                                         <xsl:template match="text()">
    98                                                 <xsl:value-of select="normalize-space(.)"/>
    9984                                        </xsl:template>
    10085                                </xsl:stylesheet>
Note: See TracChangeset for help on using the changeset viewer.