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