xsl-list
[Top] [All Lists]

Re: [xsl] Re: Cosmetic sorting

2007-01-02 00:35:32
Dimitre Novatchev <dnovatchev(_at_)gmail(_dot_)com> kirjoitti:
On 12/29/06, Harry Liljestrom <harry(_dot_)liljestrom(_at_)saunalahti(_dot_)fi> 
wrote:
> Hi,
>
> I have following input XML structure, this is simplified:
>
> <policy-rules>
>   <fw-rule-order>
>      fwrule3
>      fwrule1
>      fwrule2
>   </fw-rule-order>
>   <fw-rules>
>      <rule name="fwrule1">
>         <direction>out</direction>
>         <action>pass<action>
>         <remote>192.168.129.31</remote>
>         <local>192.168.129.67</local>
>      </rule>
>      <rule name="fwrule2">
>         <direction>in</direction>
>         <action>drop<action>
>         <remote>192.168.129.32</remote>
>         <local>192.168.129.67</local>
>      </rule>
>      <rule name="fwrule3">
>         <direction>out</direction>
>         <action>pass<action>
>         <remote>192.168.129.33</remote>
>         <local>192.168.129.67</local>
>      </rule>
>   </fw-rules>
> </policy-rules>
>
> XML transformation in XSLT 1.0
> <!-- Global variables -->
> <xsl:variable name="rule-order">
>   <xsl:value-of select="normalize-space(/policy-rules/fw-rule-order)"/>
> </xsl:variable>
>
> <xsl:template match="/">
>   <policy>
>      <xsl:apply-templates select="//fw-rules/rule"/>
>   </policy>
> </xsl:template>
>
> <xsl:template match="//fw-rules/rule">
>   <xsl:variable name="precedence">
>      <xsl:call-template name="determineprecedence">
>         <xsl:with-param name="string" select="$rule-order"/>
>         <xsl:with-param name="value" select="@name"/>
>      </xsl:call-template>
>   </xsl:variable>
>   <rule type="{action}" precedence="{$precedence}">}">
>       <xsl:choose>
>          <xsl:when test="direction='in'">
>             <src>
>                <xsl:apply-templates select="remote"/>
>             </src>
>             <dst>
>                <xsl:apply-templates select="local"/>
>             </dst>
>          </xsl:when>
>          <xsl:otherwise>
>             <src>
>                <xsl:apply-templates select="local"/>
>             </src>
>             <dst>
>                <xsl:apply-templates select="remote"/>
>             </dst>
>          </xsl:otherwise>
>       </xsl:choose>
>   </rule>
> </xsl:template>
>
> <xsl:template match="remote">
>   <xsl:value-of select="."/>
> </xsl:template>
>
> <xsl:template match="local">
>   <xsl:value-of select="."/>
> </xsl:template>
>
> <!-- Named templates -->
> <xsl:template name="determineprecedence">
>   <xsl:param name="string"/>
>   <xsl:param name="value"/>
>   <!-- Returns the precedence of the rule -->
> </xsl:template>
>
>
> Output without sorting, after XSLT processor recursion:
> <policy>
>  <rule type="pass" precedence="1">
>     <src>192.168.129.67</src>
>     <dst>192.168.129.31</dst>
>  </rule>
>  <rule type="drop" precedence="2">
>     <src>192.168.129.32</src>
>     <dst>192.168.129.67</dst>
>  </rule>
>  <rule type="pass" precedence="0">
>     <src>192.168.129.67</src>
>     <dst>192.168.129.33</dst>
>  </rule>
> </policy>
>
> Desired output:
> <policy>
>  <rule type="pass" precedence="0">
>     <src>192.168.129.67</src>
>     <dst>192.168.129.33</dst>
>  </rule>
>  <rule type="pass" precedence="1">
>     <src>192.168.129.67</src>
>     <dst>192.168.129.31</dst>
>  </rule>
>  <rule type="drop" precedence="2">
>     <src>192.168.129.32</src>
>     <dst>192.168.129.67</dst>
>  </rule>
> </policy>
>
> I have been looking for some examples of the <xsl:sort> use. But I haven't 
catched any examples how this could be solved. This problem is only cosmetic, because of 
the precedence attribute. If there are many rules it is more comfortable to read the 
output file, if it is sorted by the precedence attribute as shown above.
>
> I'm wondering if it is possible apply some kind of post-sorting in the output 
tree structure, before it is outputted to XML file? If there exists some sorting 
examples in the web, please point it out to me.
>
> Atleast one solution could be forcing the apply of '//fw-rules/rule' template in 
the order <fw-rule-order> specifies.
>


If I understand what should be the result you're after, then this
transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <!-- Global variables -->
 <xsl:variable name="vDg" select="'0123456789 '"/>

 <xsl:variable name="rule-order">
  <xsl:value-of select="normalize-space(/policy-rules/fw-rule-order)"/>
 </xsl:variable>

 <xsl:variable name="vnruleOrder"
   select="translate($rule-order, translate($rule-order, $vDg,''), '')"
 />

 <xsl:template match="policy-rules">
   <policy>
     <xsl:apply-templates select="fw-rules/rule">
       <xsl:sort data-type="number" select=
         "string-length(
                   substring-before($vnruleOrder,
                                    translate(@name,
                                              translate(@name, $vDg,''),
                                              ''
                                              )
                                    )
                        )"
       />
     </xsl:apply-templates>
   </policy>
 </xsl:template>

 <xsl:template match="fw-rules/rule">
   <xsl:variable name="vInDir" select=
     "self::*[direction = 'in']"/>
   <rule type="{action}">

     <src>
       <xsl:apply-templates select=
         "remote[$vInDir] | local[not($vInDir)]"/>
     </src>
     <dst>
       <xsl:apply-templates select=
        "local[$vInDir] | remote[not($vInDir)]"/>
     </dst>
   </rule>
 </xsl:template>

 <xsl:template match="local | remote">
   <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided xml document (corrected to be well-formed):

<policy-rules>
 <fw-rule-order>
    fwrule3
    fwrule1
    fwrule2
 </fw-rule-order>
 <fw-rules>
    <rule name="fwrule1">
       <direction>out</direction>
       <action>pass</action>
       <remote>192.168.129.31</remote>
       <local>192.168.129.67</local>
    </rule>
    <rule name="fwrule2">
       <direction>in</direction>
       <action>drop</action>
       <remote>192.168.129.32</remote>
       <local>192.168.129.67</local>
    </rule>
    <rule name="fwrule3">
       <direction>out</direction>
       <action>pass</action>
       <remote>192.168.129.33</remote>
       <local>192.168.129.67</local>
    </rule>
 </fw-rules>
</policy-rules>

produces the wanted result:

<policy>
 <rule type="pass">
   <src>192.168.129.67</src>
   <dst>192.168.129.33</dst>
 </rule>
 <rule type="pass">
   <src>192.168.129.67</src>
   <dst>192.168.129.31</dst>
 </rule>
 <rule type="drop">
   <src>192.168.129.32</src>
   <dst>192.168.129.67</dst>
 </rule>
</policy>


Hope this helped.

--
Cheers,
Dimitre Novatchev

Great Thanks,

Sorry for the lacking well-formness in the example. Your solution helps me get 
further with the transformation I'm working on by now. I'm beginner in XSLT and 
you introduced me a new function (translate(...)), which I didn't know it 
exists in XSLT ;) I must say, more I learn XSLT, confirms me that XSLT is very 
powerful language.

Harry

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • Re: [xsl] Re: Cosmetic sorting, Harry Liljeström <=