yml2/xml2yml.ysl2
changeset 43 fb35b9db9ca1
equal deleted inserted replaced
42:700f4d003349 43:fb35b9db9ca1
       
     1 // xml2yml2.ysl2 version 2.4.4
       
     2 
       
     3 include yslt.yml2
       
     4 
       
     5 decl textstylesheet is estylesheet(*output="text") {
       
     6     output *output;
       
     7     const "space", !"'" + " " * 200 + "'"!;
       
     8     param "autoindent", 4;
       
     9     content;
       
    10 }, tstylesheet is textstylesheet;
       
    11 
       
    12 define operator "†" as call "convert" with "tag", "name()";
       
    13 
       
    14 tstylesheet {
       
    15     function "printNS"
       
    16         for "*/namespace::*[local-name()!='xml']" {
       
    17             > xmlns
       
    18             if "not(local-name()='')"
       
    19                 > :«local-name()»
       
    20             > ="«.»"
       
    21             if "position()<last()" > , 
       
    22         }
       
    23     
       
    24     template "/" {
       
    25         if "count(*/namespace::*)>1" {
       
    26             | decl «name(*)»(`call "printNS"`);
       
    27             |
       
    28         }
       
    29         apply "*", 0;
       
    30     }
       
    31 
       
    32     template "text()" {
       
    33         param "text", "normalize-space()";
       
    34 
       
    35         if "string-length($text)>0" choose {
       
    36             when "contains($text,'\\n')" choose {
       
    37                 when "string-length(substring-before($text,'\\n'))<string-length($text)-1" choose {
       
    38                     when "substring($text,string-length($text),1)='\\n'"
       
    39                         > \n||\n«$text»||\n\n
       
    40                     otherwise > > «str:replace($text,'\\n','\\\\n')»\n
       
    41                 }
       
    42                 otherwise > | «$text»\n
       
    43             }
       
    44             otherwise > > `copy "."`\n
       
    45         }
       
    46     }
       
    47 
       
    48     template "*" {
       
    49         0>
       
    50         call "convert" with "tag", "name()";
       
    51         apply "@*";
       
    52 
       
    53         choose {
       
    54             when "count(*)=0 and string-length(normalize-space())=0"
       
    55                 > ;\n
       
    56             when "count(*)=1 and string-length(normalize-space())=0" {
       
    57                 > \n
       
    58                 apply "*";
       
    59             }
       
    60             when "count(*)=0 and string-length(normalize-space())>0" {
       
    61                 >  
       
    62                 apply "text()";
       
    63             }
       
    64             otherwise {
       
    65                 >  {\n
       
    66                 for "*|text()" {
       
    67                     if "local-name()=''"
       
    68                         if "string-length(normalize-space())>0"
       
    69                             1>
       
    70                     apply ".";
       
    71                 }
       
    72                 | }
       
    73             }
       
    74         }
       
    75     }
       
    76 
       
    77     template "@*" {
       
    78         >  «name()»="«.»"
       
    79         if "position()<last()" > ,
       
    80     }
       
    81 
       
    82     function "convert" {
       
    83         param "tag";
       
    84 
       
    85         > «translate($tag, '-', '_')»
       
    86     }
       
    87 }