| 1 | package XML::Table2XML; | ||
| 2 | |||
| 3 | use 5.006001; use strict; use warnings; | ||
| 4 | use Encode 'decode'; | ||
| 5 | use Carp; | ||
| 6 | require Exporter; | ||
| 7 | our @ISA = qw(Exporter); | ||
| 8 | our @EXPORT_OK = qw(parseHeaderForXML addXMLLine commonParent offsetNodesXML); | ||
| 9 | our $VERSION = '1.42'; | ||
| 10 | my $LINEFEED = ""; | ||
| 11 | my $XMLDIRECTIVE = '<?xml version="1.0"?>'; | ||
| 12 | my $ENCODING = 'iso-8859-1'; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | XML::Table2XML - Generic conversion of tabular data to XML by reverting Excel's flattener methodology. | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use XML::Table2XML; | ||
| 21 | my $outXML = ""; | ||
| 22 | # first parse column path headers for attribute names, id columns and special common sibling mark ("//") | ||
| 23 | parseHeaderForXML("rootNodeName", ['/@id','/@name2','/a']); | ||
| 24 | # then walk through the whole data to build the actual XML string into $outXML | ||
| 25 | my @datarows = ([1,"testName","testA"], | ||
| 26 | [1,"testName","testB"], | ||
| 27 | [1,"testName","testC"]); | ||
| 28 | for my $lineData (@datarows) { | ||
| 29 | $outXML.=addXMLLine($lineData); | ||
| 30 | } | ||
| 31 | #finally finish the XML and reset the static vars | ||
| 32 | $outXML.=addXMLLine(undef); | ||
| 33 | print $outXML; | ||
| 34 | # yields: | ||
| 35 | # <?xml version="1.0"?> | ||
| 36 | # <rootNodeName id="1" name2="testName"><a>testA</a><a>testB</a><a>testC</a></rootNodeName> | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | table2xml is an algorithm having two functions that allow the conversion of tabular data to XML | ||
| 41 | without using XSLT. This is achieved by reverting the "Flattener" methodology used by Microsoft Excel | ||
| 42 | to convert the XML tree format to a two-dimensional table | ||
| 43 | (see Opening XML Files in Excel and INFO: Microsoft Excel 2002 and XML). | ||
| 44 | |||
| 45 | This reversion is achieved by: | ||
| 46 | |||
| 47 | 1. (possibly) modifying the flattened table a bit to enable a simpler processing of the data, | ||
| 48 | |||
| 49 | 2. sequentially processing the data column- and row wise. | ||
| 50 | |||
| 51 | The whole algorithm is done without the aid of any XML library, so it lends itself to easy translation | ||
| 52 | into other environments and languages. | ||
| 53 | |||
| 54 | Producing the XML: | ||
| 55 | |||
| 56 | 1. invoke parseHeaderForXML, using a line with the rootnode and path information. | ||
| 57 | |||
| 58 | 2. After parsing the header info, the table data can be processed row by row by calling addXMLLine. | ||
| 59 | The current data row is provided in the single argument lineData, the built XML is string returned and can be collected/written. | ||
| 60 | |||
| 61 | 3. A final call to addXMLLine with lineData == undef restores the static variables and finalizes the XML string (closes any still open tags). | ||
| 62 | |||
| 63 | =head2 Public Functions | ||
| 64 | |||
| 65 | =head3 parseHeaderForXML ($rootNodeName,\@header,$LINEBREAKS,$XMLDIRECTIVE,$ENCODING) | ||
| 66 | |||
| 67 | B<rootNodeName> is the name of the common root node. Any I</@rootAttributes> and I</#text> will be placed under | ||
| 68 | respectively after this root node. | ||
| 69 | |||
| 70 | B<header> is a list of paths denoting the "place" of the data in the targeted XML. Following special cases are allowed: | ||
| 71 | |||
| 72 | =over | ||
| 73 | |||
| 74 | =item * Plain elements | ||
| 75 | |||
| 76 | are denoted by I</node/subnode/subsubnode/etc.../elementName> | ||
| 77 | |||
| 78 | =item * Attributes | ||
| 79 | |||
| 80 | are denoted by I</node/subnode/subsubnode/etc.../@attributeName> | ||
| 81 | |||
| 82 | =item * "ID" nodes | ||
| 83 | |||
| 84 | are denoted by I</node/subnode/subsubnode/etc.../#id> (they are not being ouptut) | ||
| 85 | |||
| 86 | =item * special common sibling nodes | ||
| 87 | |||
| 88 | are denoted by a leading double slash (I<//>) | ||
| 89 | special common sibling nodes are used for nested common sibling nodes (e.g., C<< <root><a><b>test</b></a><otherData>...<root> >> | ||
| 90 | or C<< <root><a><b>test1</b><z>test2</z></a><otherData>...<root> >> ) must be located at the beginning of the last node within the nested sibling. | ||
| 91 | |||
| 92 | =item * a root text element | ||
| 93 | |||
| 94 | is denoted by I</#text> | ||
| 95 | |||
| 96 | =item * root attributes | ||
| 97 | |||
| 98 | are given as I</@rootNodeAttribute> | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | B<LINEBREAKS> specifies whether '\n' should be added after each datarow for easier readablity, default is no linebreaks | ||
| 103 | |||
| 104 | B<XMLDIRECTIVE> specifies any header being inserted before the root element, default is C<'<?xml version="1.0"?>'>. | ||
| 105 | |||
| 106 | B<ENCODING> denotes the Unicode Codification used to encode the string(s) returned by B<addXMLLine()>, default is C<'iso-8859-1'> | ||
| 107 | |||
| 108 | |||
| 109 | =head3 $returnedXML = addXMLLine(\@lineData) | ||
| 110 | |||
| 111 | B<lineData> is a list of data elements that are converted to XML following the parsed header information. | ||
| 112 | |||
| 113 | The produced XML is returned as a function value which can be concatenated or written to a file. | ||
| 114 | Bear in mind that the returned XML is just a part of a larger structure, so only after the last line has been processed and | ||
| 115 | B<addXMLLine(undef)> has been called, the XML structure is finished. | ||
| 116 | |||
| 117 | =head2 Prerequisites for column order and data layout | ||
| 118 | |||
| 119 | The layout of the columns (header = "data paths" and respective column data below) has to follow a certain layout: | ||
| 120 | |||
| 121 | =over | ||
| 122 | |||
| 123 | =item * child nodes always have to follow their parent nodes (i.e. /a/b/c is after /a or /a/b). | ||
| 124 | |||
| 125 | =item * "#id" columns and attributes belong to the same element node (e.g. /a/b/#id, /a/b/@att1 and /a/b/@att2) and therefore | ||
| 126 | have to be given consecutively and with the "#id" column first (attributes and element node order is not important). | ||
| 127 | |||
| 128 | =item * related subnodes have to be grouped together (i.e. /a/b, /a/c, /a/x, /a/x/@att, ...), other subnodes have to follow. | ||
| 129 | |||
| 130 | The layout of the data below has to be as follows (recursively similar within the blocks for any sub-blocks): | ||
| 131 | |||
| 132 | Block1PathHeaders Block2PathHeaders Block3PathHeaders | ||
| 133 | Block1Data EMPTY EMPTY | ||
| 134 | ... EMPTY EMPTY | ||
| 135 | Block1Data EMPTY EMPTY | ||
| 136 | EMPTY Block2Data EMPTY | ||
| 137 | EMPTY ... EMPTY | ||
| 138 | EMPTY Block2Data EMPTY | ||
| 139 | EMPTY EMPTY Block3Data | ||
| 140 | EMPTY EMPTY ... | ||
| 141 | EMPTY EMPTY Block3Data | ||
| 142 | |||
| 143 | where the corresponding XML would then look like: | ||
| 144 | |||
| 145 | <root> | ||
| 146 | <Block1> | ||
| 147 | <Block1subnode> | ||
| 148 | ... | ||
| 149 | </Block1subnode> | ||
| 150 | <Block1subnode> | ||
| 151 | ... | ||
| 152 | </Block1subnode> | ||
| 153 | .. | ||
| 154 | </Block1> | ||
| 155 | <Block2> | ||
| 156 | <Block2subnode> | ||
| 157 | ... | ||
| 158 | </Block2subnode> | ||
| 159 | <Block2subnode> | ||
| 160 | ... | ||
| 161 | </Block2subnode> | ||
| 162 | .. | ||
| 163 | </Block2> | ||
| 164 | <Block3> | ||
| 165 | <Block3subnode> | ||
| 166 | ... | ||
| 167 | </Block3subnode> | ||
| 168 | <Block3subnode> | ||
| 169 | ... | ||
| 170 | </Block3subnode> | ||
| 171 | .. | ||
| 172 | </Block3> | ||
| 173 | </root> | ||
| 174 | |||
| 175 | =item * Sibling nodes that are "common" to a whole subnode (e.g. C<< <subnode><commonSibling>value1</commonSibling><otherNodes>...</otherNodes></subnode> >>) | ||
| 176 | have to be first in the subnode and need to "span" the data there. | ||
| 177 | |||
| 178 | Example for subnode <a>: | ||
| 179 | |||
| 180 | <?xml version="1.0"?> | ||
| 181 | <root> | ||
| 182 | <a> | ||
| 183 | <z>TestB</z> | ||
| 184 | <c>TestA1</c> | ||
| 185 | <c>TestA2</c> | ||
| 186 | <c>TestA3</c> | ||
| 187 | <c>TestA4</c> | ||
| 188 | </a> | ||
| 189 | </root> | ||
| 190 | |||
| 191 | /root | ||
| 192 | /a/z/@x /a/c | ||
| 193 | TestB TestA1 | ||
| 194 | TestB TestA2 | ||
| 195 | TestB TestA3 | ||
| 196 | TestB TestA4 | ||
| 197 | |||
| 198 | |||
| 199 | =item * In case you happen to own MS Excel, the easiest way to get that layout is to follow the steps below: | ||
| 200 | |||
| 201 | =over | ||
| 202 | |||
| 203 | =item 1. Open Target XML File in Excel (don't forget the XML directive there: "<?xml version="1.0"?>" !!!!) | ||
| 204 | |||
| 205 | =item 2. remove any "#agg" columns (used to differentiate between numerical common siblings and "real" data) | ||
| 206 | |||
| 207 | =item 3. move the common root (or the common subnode) siblings leftmost of the root (or resp. Subnode) | ||
| 208 | |||
| 209 | Examples: | ||
| 210 | |||
| 211 | <?xml version="1.0"?> | ||
| 212 | <root> | ||
| 213 | <x z="testAttX">testX</x> | ||
| 214 | <a><b><c>TestA1</c> | ||
| 215 | <c>TestA2</c> | ||
| 216 | <c>TestA3</c> | ||
| 217 | <c>TestA4</c></b></a> | ||
| 218 | </root> | ||
| 219 | |||
| 220 | /root /root | ||
| 221 | /a/b/c /x /x/@z /x /x/@z /a/b/c | ||
| 222 | TestA1 testX testAttX testX testAttX TestA1 | ||
| 223 | TestA2 testX testAttX modify to-> testX testAttX TestA2 | ||
| 224 | TestA3 testX testAttX testX testAttX TestA3 | ||
| 225 | TestA4 testX testAttX testX testAttX TestA4 | ||
| 226 | |||
| 227 | <?xml version="1.0"?> | ||
| 228 | <root> | ||
| 229 | <a><z x="TestB"></z><b><c>TestA1</c> | ||
| 230 | <c>TestA2</c> | ||
| 231 | <c>TestA3</c> | ||
| 232 | <c>TestA4</c></b></a> | ||
| 233 | </root> | ||
| 234 | |||
| 235 | /root /root | ||
| 236 | /a/b/c /a/z/@x /a/z/@x /a/b/c | ||
| 237 | TestA1 TestB TestB TestA1 | ||
| 238 | TestA2 TestB modify to-> TestB TestA2 | ||
| 239 | TestA3 TestB TestB TestA3 | ||
| 240 | TestA4 TestB TestB TestA4 | ||
| 241 | |||
| 242 | =item 4. For nested common sibling nodes (e.g., C<< <root><a><b>test</b></a><otherData>...<root> >> or | ||
| 243 | C<< <root><a><b>test1</b><c>test2</c></a><otherData>...<root> >>), write a double slash at the beginning of the last node within the nested sibling. | ||
| 244 | |||
| 245 | Example (also includes column moving as in the examples above): | ||
| 246 | |||
| 247 | <?xml version="1.0"?> | ||
| 248 | <root> | ||
| 249 | <a n=""CW""><l c=""oalp""><p v=""A1""></p></l> | ||
| 250 | <f c=""oalvl""><p v=""W""></p></f> | ||
| 251 | <p n=""target""></p></a> | ||
| 252 | <a n=""CD""><l c=""oalp""><p v=""A1""></p></l> | ||
| 253 | <f c=""oalvl""><p v=""D""></p></f></a> | ||
| 254 | <r><pr v=""TEST""></pr> | ||
| 255 | <ar r=""test2""></ar> | ||
| 256 | <ar r=""test4""></ar></r> | ||
| 257 | </root> | ||
| 258 | |||
| 259 | /root | ||
| 260 | /a/@n /a/f/@c /a/f/p/@v /a/l/@c /a/l/p/@v /a/p/@n /r/ar/@r /r/pr/@v | ||
| 261 | CW oalvl W oalp A1 target | ||
| 262 | CD oalvl D oalp A1 | ||
| 263 | test2 TEST | ||
| 264 | test4 TEST | ||
| 265 | modify to --> | ||
| 266 | |||
| 267 | /root | ||
| 268 | /a/@n /a/l/@c //a/l/p/@v /a/f/@c //a/f/p/@v /a/p/@n /r/pr/@v /r/ar/@r | ||
| 269 | CW oalp A1 oalvl W target | ||
| 270 | CD oalp A1 oalvl D | ||
| 271 | TEST test2 | ||
| 272 | TEST test4 | ||
| 273 | |||
| 274 | |||
| 275 | =item 5. For a first column of a subnode list that is not being a "primary key" column | ||
| 276 | (i.e., having empty cells or continuous equal values), introduce an artificial #id column. | ||
| 277 | |||
| 278 | Examples: | ||
| 279 | |||
| 280 | <?xml version="1.0"?> | ||
| 281 | <root> | ||
| 282 | <a x="test1">testA</a> | ||
| 283 | <a x="test2"></a> | ||
| 284 | </root> | ||
| 285 | |||
| 286 | /root /root | ||
| 287 | /a /a/@x modify to-> /a/#id /a /a/@x | ||
| 288 | testA test1 1 testA test1 | ||
| 289 | test2 2 test2 | ||
| 290 | |||
| 291 | <?xml version="1.0"?> | ||
| 292 | <root> | ||
| 293 | <co><f><a>Numeric</a></f></co> | ||
| 294 | <co><f><a>VarChar</a></f></co> | ||
| 295 | <co><f><a>VarChar</a></f></co> | ||
| 296 | <co><f><a>VarChar</a></f></co> | ||
| 297 | <co><f><a>VarChar</a></f></co> | ||
| 298 | <co><f><a>DBTimeStamp</a></f></co> | ||
| 299 | <co><f><a>VarChar</a><fk>JOB_ID</fk><fl>JOB_TITLE</fl></f></co> | ||
| 300 | <co><f><a>Numeric</a><fk>TESTID</fk><fl>TESTn</fl></f></co> | ||
| 301 | <co><f><a>Numeric</a></f></co> | ||
| 302 | <co><f><a>Numeric</a><fk>EMPLOYEE_ID</fk><fl>FIRST_n</fl></f></co> | ||
| 303 | <co><f><a>Numeric</a><fk>DEPARTMENT_ID</fk><fl>DEPARTMENT_n</fl></f></co> | ||
| 304 | </root> | ||
| 305 | |||
| 306 | /root modify to-> /root | ||
| 307 | /co/f/a /co/f/fk /co/f/fl /co/#id /co/f/a /co/f/fk /co/f/fl | ||
| 308 | Numeric 1 Numeric | ||
| 309 | VarChar 2 VarChar | ||
| 310 | VarChar 3 VarChar | ||
| 311 | VarChar 4 VarChar | ||
| 312 | VarChar 5 VarChar | ||
| 313 | DBTimeStamp 6 DBTimeStamp | ||
| 314 | VarChar JOB_ID JOB_TITLE 7 VarChar JOB_ID JOB_TITLE | ||
| 315 | Numeric TESTID TESTn 8 Numeric TESTID TESTn | ||
| 316 | Numeric 9 Numeric | ||
| 317 | Numeric EMPLOYEE_ID FIRST_n 10 Numeric EMPLOYEE_ID FIRST_n | ||
| 318 | Numeric DEPARTMENT_ID DEPARTMENT_n 11 Numeric DEPARTMENT_ID DEPARTMENT_n | ||
| 319 | |||
| 320 | =item 6. Use the header row and rootnodeName for your data layout. | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =back | ||
| 325 | |||
| 326 | =head1 LIMITATIONS | ||
| 327 | |||
| 328 | Generally, pay close attention to the ordering of columns and constraints on the data as described above, | ||
| 329 | since the algorithm in writeLine doesn't check for validity, thus producing invalid XML in case of failing to follow preparation steps correctly. | ||
| 330 | |||
| 331 | In mixed content nodes, the only way to correctly (re)produce the XML is for ONE content being right after the node name. | ||
| 332 | There's currently no way to produce mixed content nodes with more than one text node (e.g., C<< <node>text1<subnode>Test</subnode>text2</node> >> and the like). | ||
| 333 | |||
| 334 | Same sequential parent nodes are "factored" out by the flattener, so the unflattening algorithm treats them as being factored out, | ||
| 335 | which means there is no way to exactly reproduce (C<< <a><b>test1</b></a><a><b>test2</b></a> >>, this would be processed as C<< <a><b>test1</b><b>test2</b></a> >>, which is semantically equal, but not the same...). | ||
| 336 | |||
| 337 | =head1 REFERENCE | ||
| 338 | |||
| 339 | for a detailed discussion of the flattening algorithmm in Excel see L<https://web.archive.org/web/20041124175116/http://support.microsoft.com/kb/282161/EN-US/> and L<https://web.archive.org/web/20050210015617/http://support.microsoft.com/kb/288215/EN-US/> | ||
| 340 | |||
| 341 | =head1 AUTHOR | ||
| 342 | |||
| 343 | Roland Kapl, rkapl@cpan.org | ||
| 344 | |||
| 345 | =head1 COPYRIGHT AND LICENSE | ||
| 346 | |||
| 347 | Copyright (C) 2006 by Roland Kapl | ||
| 348 | |||
| 349 | This library is free software; you can redistribute it and/or modify | ||
| 350 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 351 | at your option, any later version of Perl 5 you may have available. | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | my $rootNodeName; | ||
| 356 | my @colPaths; # the path informations in the columns | ||
| 357 | my @specialCommonSibling; # specifies columns marked with "//" at beginning | ||
| 358 | my @isIDCol; # whether the current column is an "id" column (../#id) | ||
| 359 | my @attrNames; # attribute name if column path specifies attribute (../@name) | ||
| 360 | |||
| 361 | # parseHeaderForXML | ||
| 362 | # | ||
| 363 | # parses the column path headers (header) for attributes (attrNames), whether headers are id columns (isIDCol) and | ||
| 364 | # special common sibling marks ("//", specialCommonSibling), also strips the attributes and ids from the | ||
| 365 | # node path (put into colPaths). | ||
| 366 | sub parseHeaderForXML { | ||
| 367 | my ($rootNode,$header,$LINEBREAKS,$XMLDIR,$ENCODE) = @_; | ||
| 368 | |||
| 369 | $LINEFEED = "\n" if $LINEBREAKS; | ||
| 370 | $XMLDIRECTIVE = $XMLDIR if $XMLDIR; | ||
| 371 | $ENCODING = $ENCODE if $ENCODE; | ||
| 372 | undef(@colPaths);undef(@attrNames);undef(@isIDCol);undef(@specialCommonSibling); | ||
| 373 | croak "no rootnode name given !" unless $rootNode; | ||
| 374 | $rootNodeName=$rootNode; | ||
| 375 | my $colCount = scalar @$header; | ||
| 376 | croak "no list of headers given !" unless $colCount > 0; | ||
| 377 | # Loop accross columns... | ||
| 378 | for my $colCounter (1..$colCount) { | ||
| 379 | my $thisCellValue = $header->[$colCounter-1]; | ||
| 380 | croak "no valid header given: ". $thisCellValue unless $thisCellValue =~ /\//; | ||
| 381 | $specialCommonSibling[$colCounter] = 0; | ||
| 382 | if ($thisCellValue =~ /^\/\//) { | ||
| 383 | $specialCommonSibling[$colCounter] = 1; | ||
| 384 | $thisCellValue =~ s/^\///; | ||
| 385 | } | ||
| 386 | $isIDCol[$colCounter] = 0; | ||
| 387 | $colPaths[$colCounter] = $thisCellValue; | ||
| 388 | if ($thisCellValue =~ /\/#id$/) { | ||
| 389 | $colPaths[$colCounter] = $thisCellValue; | ||
| 390 | $colPaths[$colCounter] =~ s/\/#id$//; | ||
| 391 | $isIDCol[$colCounter] = 1; | ||
| 392 | } | ||
| 393 | $attrNames[$colCounter] = ""; | ||
| 394 | if ($thisCellValue =~ /@/) { | ||
| 395 | ($attrNames[$colCounter]) = ($thisCellValue =~ /@(.*)/); | ||
| 396 | ($colPaths[$colCounter]) = ($thisCellValue =~ /(.*)\/@/); | ||
| 397 | } | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | # addXMLLine | ||
| 402 | # | ||
| 403 | # outputs (and optionally attaches) XML for a single data line (lineData), XML structure defined in rootNodeName, | ||
| 404 | # colPaths , attrNames, isIDCol And specialCommonSibling (see new) | ||
| 405 | { | ||
| 406 | # static vars.. | ||
| 407 | my $lastNodePath = ""; # the path of the last written column (after modification by siblingCloseXML) | ||
| 408 | my $lastNodeColPath = ""; # the path of the last written column (without modification by siblingCloseXML) | ||
| 409 | my $rowCounter = 0; # rowCounter is needed for checking whether we#re in first row (for attribute of rootnode) | ||
| 410 | my @lastColValue; # previous row's values | ||
| 411 | my $siblingCloseReset = 0; # did we reset siblingCloseXML after the last row? | ||
| 412 | |||
| 413 | sub addXMLLine { | ||
| 414 | my ($lineData) = @_; | ||
| 415 | my $strXML=""; | ||
| 416 | my $colCount = (scalar @colPaths) -1; | ||
| 417 | # use this (lineData = NULL) for resetting static vars and finishing resultXML | ||
| 418 | if (!defined($lineData)) { | ||
| 419 | my $retXML = decode($ENCODING,offsetNodesXML($lastNodePath, "", 1).$LINEFEED."</$rootNodeName>"); | ||
| 420 | undef @lastColValue; $rowCounter = 0; $lastNodePath = ""; $lastNodeColPath = ""; $siblingCloseReset = 0; | ||
| 421 | return $retXML; | ||
| 422 | } | ||
| 423 | if (!@lastColValue) { | ||
| 424 | $lastColValue[$_] = "" for (0..$colCount); | ||
| 425 | } | ||
| 426 | $rowCounter++; | ||
| 427 | # Now loop accross columns... | ||
| 428 | my $openXML = ""; my $attrXML = ""; my $closeXML = ""; | ||
| 429 | |||
| 430 | my $curColPath = ""; my $parentPath = ""; my $lastCommonNode = ""; my $curNode = ""; my $curPath = ""; | ||
| 431 | my $parentWasCreated = 0; my $allowSiblingClose = 0; my $addAnIDNode = 0; my $LFwritten = 0; my $noNodeYetCreated = 1; | ||
| 432 | my $curNodeValue = ""; my $lastPossibleParent = ""; my $preventCommonNodeClose = 0; my $siblingCloseXML = ""; | ||
| 433 | my $rootAttXML = ""; my $rootText = ""; | ||
| 434 | |||
| 435 | for my $colCounter (1 .. $colCount) { | ||
| 436 | my $thisCellValue = xmlCorrectChars($lineData->[$colCounter-1]); | ||
| 437 | |||
| 438 | # we have a different common node, so finish previous and begin a new common node | ||
| 439 | if ($curColPath ne $colPaths[$colCounter]) { | ||
| 440 | # reset parentPath | ||
| 441 | $parentPath = "" if commonParent($colPaths[$colCounter], $parentPath) ne $parentPath; | ||
| 442 | |||
| 443 | # find max. common parent against last column, | ||
| 444 | # if nothing found and lastPossibleParent not contained in current col then set to lastPossibleParent | ||
| 445 | # store current path as lastPossibleParent | ||
| 446 | $curPath = $colPaths[$colCounter]; | ||
| 447 | if ($thisCellValue ne "") { | ||
| 448 | $parentPath = commonParent($curPath, $lastPossibleParent); | ||
| 449 | $lastPossibleParent = $curPath; | ||
| 450 | } | ||
| 451 | |||
| 452 | # write combined previous colPath's XML parts and reset them, also do setting of siblingClose right after | ||
| 453 | if ($curNodeValue ne "" or $attrXML ne "" or $addAnIDNode) { | ||
| 454 | $strXML.= $closeXML.($rowCounter > 1 and !$LFwritten ? $LINEFEED : "").$openXML."<".$curNode.$attrXML.">".$curNodeValue; | ||
| 455 | $lastNodePath = $curColPath; $LFwritten = 1; $lastNodeColPath = $curColPath; $noNodeYetCreated = 0; | ||
| 456 | my $potCommonParent = commonParent($curColPath, $curPath); | ||
| 457 | $parentWasCreated = 0; | ||
| 458 | $parentWasCreated = 1 if $potCommonParent ne ""; | ||
| 459 | |||
| 460 | # siblingCloseXML is for closing nodes not being parent of next, thus allowing nodes to be a common sibling to a whole complex subelement! | ||
| 461 | # entry if last column (curColPath) not contained in current (curPath) | ||
| 462 | if ($allowSiblingClose and $potCommonParent ne $curColPath) { | ||
| 463 | if ($specialCommonSibling[$colCounter - 1]) { | ||
| 464 | $siblingCloseXML = offsetNodesXML($curColPath, $potCommonParent, 1); | ||
| 465 | # lastNodePath: /a/b/c/d -> potCommonParent: /a/b -> lastNodePath /a/b | ||
| 466 | $lastNodePath = $potCommonParent; | ||
| 467 | } | ||
| 468 | else { | ||
| 469 | my ($cutLastNode) = ($curColPath =~ /.*\/(.*?)$/); | ||
| 470 | $siblingCloseXML = "</$cutLastNode>"; | ||
| 471 | ($lastNodePath) = ($lastNodePath =~ /(.*)\//) if ($lastNodePath ne ""); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | # if we have remembered a siblingClose across rows (is closed at the end of the row and may not cause | ||
| 477 | # closing of the common root in the normal closeXML | ||
| 478 | if ($specialCommonSibling[$colCounter - 1] and $siblingCloseReset) { | ||
| 479 | $preventCommonNodeClose = 1; | ||
| 480 | } | ||
| 481 | |||
| 482 | # if we have a siblingClose, do it here | ||
| 483 | # (as it could also happen after some empty same common node columns, which wouldn#t enter into set siblingClose) | ||
| 484 | if ($siblingCloseXML ne "" and $thisCellValue ne "") { | ||
| 485 | $strXML.= $siblingCloseXML; | ||
| 486 | $siblingCloseXML = ""; | ||
| 487 | } | ||
| 488 | $curNodeValue = ""; $closeXML = ""; $openXML = ""; $attrXML = ""; $addAnIDNode = 0; | ||
| 489 | |||
| 490 | # creation of XML parts for new common node | ||
| 491 | $curColPath = $colPaths[$colCounter]; | ||
| 492 | ($curNode) = ($curColPath =~ /.*\/(.*?)$/); | ||
| 493 | |||
| 494 | # need that for closing common sibling (contained in current column) during next loop run | ||
| 495 | $allowSiblingClose = 0; | ||
| 496 | if ($thisCellValue ne "" and | ||
| 497 | ($thisCellValue ne $lastColValue[$colCounter] or $parentWasCreated) and | ||
| 498 | $colPaths[$colCounter] ne "/#text") { | ||
| 499 | $allowSiblingClose = 1; | ||
| 500 | } | ||
| 501 | |||
| 502 | # make the openXML and closeXML parts for current column (written to strXML in next loop) | ||
| 503 | # | ||
| 504 | # parentWasCreated is for cases where columns accidentally have the same value as above, | ||
| 505 | # but we know we are in a subnode of a parent (parentWasCreated = true) | ||
| 506 | if ($thisCellValue ne $lastColValue[$colCounter] or $parentWasCreated) { | ||
| 507 | |||
| 508 | # open new nodes from current parent to current path when stepping down subnodes into openXML | ||
| 509 | # only for nonempty cells and if we're in the same column as before, there's no need for open part | ||
| 510 | if ($thisCellValue ne "" and $lastNodeColPath ne $curColPath and | ||
| 511 | ($parentWasCreated or $parentPath eq "" or $noNodeYetCreated)) { | ||
| 512 | $openXML = offsetNodesXML($curColPath, $parentPath); | ||
| 513 | } | ||
| 514 | |||
| 515 | # close Nodes from last open path to current path into closeXML | ||
| 516 | if ($thisCellValue ne "" and $lastNodePath ne "" and !$parentWasCreated and $siblingCloseXML eq "") { | ||
| 517 | my $potCommonParent = commonParent($lastNodePath, $curColPath); | ||
| 518 | # decide whether to close last node of common parent: | ||
| 519 | # No closing if we are in the same column as before | ||
| 520 | my $notSameColumn = ($lastNodeColPath ne $curColPath); | ||
| 521 | # current column is parent of lastNodePath -> common node close ! | ||
| 522 | my $curColIsParentOfLastNode = ($potCommonParent eq $curColPath); | ||
| 523 | # to prevent premature closing for "parent rows", | ||
| 524 | # BUT ONLY if commonSibling was not closed in previous row | ||
| 525 | my $parentIsNotCommon = ($potCommonParent ne $lastCommonNode and !$preventCommonNodeClose); | ||
| 526 | my $commonNodeClose = ($notSameColumn and ($parentIsNotCommon or $curColIsParentOfLastNode)); | ||
| 527 | $closeXML = offsetNodesXML($lastNodePath, $curColPath, 1,$commonNodeClose); | ||
| 528 | } | ||
| 529 | } | ||
| 530 | |||
| 531 | # lastCommonNode: determines whether we can close up to common parent | ||
| 532 | # (common parent of lastNodePath and current column may not be the last common Node path) | ||
| 533 | if ($thisCellValue ne "" and ($thisCellValue eq $lastColValue[$colCounter]) and $noNodeYetCreated) { | ||
| 534 | $lastCommonNode = $colPaths[$colCounter]; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | |||
| 538 | # now for the content: id columns, plain elements and attributes... | ||
| 539 | if ($thisCellValue ne "" and ($thisCellValue ne $lastColValue[$colCounter] or $parentWasCreated)) { | ||
| 540 | # check whether last column was id column -> ceate node... | ||
| 541 | if ($isIDCol[$colCounter]) { | ||
| 542 | $addAnIDNode = 1; $parentWasCreated = 1; | ||
| 543 | } | ||
| 544 | |||
| 545 | # if we have a plain element node, remember value for later | ||
| 546 | if (!$isIDCol[$colCounter] and $attrNames[$colCounter] eq "") { | ||
| 547 | if ($rowCounter == 1 and $colPaths[$colCounter] eq "/#text") { | ||
| 548 | $rootText = $thisCellValue; | ||
| 549 | } | ||
| 550 | else { | ||
| 551 | $curNodeValue = $thisCellValue; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | # for attribute nodes create either with node name or just the attribute (if other was set before | ||
| 556 | if ($attrNames[$colCounter] ne "") { | ||
| 557 | if ($rowCounter == 1 and $colPaths[$colCounter] eq "") { | ||
| 558 | $rootAttXML.= " " . $attrNames[$colCounter] . '="' . $thisCellValue . '"'; | ||
| 559 | } | ||
| 560 | else { | ||
| 561 | $attrXML.= " " . $attrNames[$colCounter] . "=\"$thisCellValue\""; | ||
| 562 | } | ||
| 563 | $parentWasCreated = 1; | ||
| 564 | } | ||
| 565 | } | ||
| 566 | $lastColValue[$colCounter] = $thisCellValue; | ||
| 567 | }# column | ||
| 568 | |||
| 569 | # at end of row, write combined previous colPath's XML parts and reset | ||
| 570 | if ($curNodeValue ne "" or $attrXML ne "" or $addAnIDNode) { | ||
| 571 | $strXML.= $closeXML.($rowCounter > 1 and !$LFwritten ? $LINEFEED : "").$openXML."<".$curNode.$attrXML.">".$curNodeValue; | ||
| 572 | $lastNodePath = $curColPath; $LFwritten = 1; $lastNodeColPath = $curColPath; | ||
| 573 | } | ||
| 574 | # write out siblingCloseXML if it wasn't closed already (meaning that remainder of row was empty !) | ||
| 575 | $siblingCloseReset = 0; | ||
| 576 | if ($siblingCloseXML ne "") { | ||
| 577 | $siblingCloseReset = 1; | ||
| 578 | $strXML.= $siblingCloseXML; | ||
| 579 | $siblingCloseXML = "" | ||
| 580 | } | ||
| 581 | # only in first row: create root node attributes and root element | ||
| 582 | if ($rowCounter == 1) { | ||
| 583 | $strXML = $XMLDIRECTIVE.$LINEFEED."<".$rootNodeName.$rootAttXML.">".$rootText.$LINEFEED.$strXML; | ||
| 584 | } | ||
| 585 | return decode($ENCODING,$strXML); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | # commonParent | ||
| 590 | # | ||
| 591 | # return potential common parent path, e.g. | ||
| 592 | # /a/b/c/d and /a/b/g/j return common parent /a/b | ||
| 593 | sub commonParent { | ||
| 594 | my ($currentPath, $potParent) = @_; | ||
| 595 | my $commonParent = ""; | ||
| 596 | |||
| 597 | my @curColPathNodes = split ("\/", $currentPath); | ||
| 598 | my @parentPathNodes = split("\/", $potParent); | ||
| 599 | my $curColPathNodesCount = scalar(@curColPathNodes); | ||
| 600 | my $parentPathNodesCount = scalar(@parentPathNodes); | ||
| 601 | for my $i (1 .. $curColPathNodesCount - 1) { | ||
| 602 | last if $i > $parentPathNodesCount; | ||
| 603 | last if !defined($parentPathNodes[$i]) or $curColPathNodes[$i] ne $parentPathNodes[$i]; | ||
| 604 | $commonParent.= "/".$curColPathNodes[$i]; | ||
| 605 | } | ||
| 606 | return $commonParent; | ||
| 607 | } | ||
| 608 | |||
| 609 | # offsetNodesXML | ||
| 610 | # | ||
| 611 | # return offset path of NodesCurrent after subtracting NodesParent in XML opening or closing form (closeFlag=True) | ||
| 612 | # e.g. /a/b/ and /a/b/g/j (offset g/j) return <g><j> or </j></g> | ||
| 613 | # the last node is ignored for opening (as it is seperately treated by the algortithm) | ||
| 614 | # Additionally include the last node of the common root for closing if includeCommonRoot=True | ||
| 615 | # Following special cases exist: | ||
| 616 | # NodesParent = "": open or close NodesCurrent | ||
| 617 | # NodesCurrent = NodesParent and in case of closing: close the common last node | ||
| 618 | # NodesCurrent and NodesParent have no common parent: open or close NodesCurrent | ||
| 619 | sub offsetNodesXML { | ||
| 620 | my ($NodesCurrent, $NodesParent, $closeFlag, $includeCommonRoot) = @_; | ||
| 621 | |||
| 622 | my $theOffset; my $additionalTopNode = ""; my $offsetNodesXML; | ||
| 623 | my $potCommonParent = commonParent($NodesCurrent, $NodesParent); | ||
| 624 | if ($NodesParent eq "") { | ||
| 625 | $theOffset = $NodesCurrent; | ||
| 626 | # for closing same node columns, we use only the leaf node | ||
| 627 | } elsif ($NodesCurrent eq $NodesParent and $closeFlag) { | ||
| 628 | ($theOffset) = ($NodesParent =~ /.*(\/.*?)$/); | ||
| 629 | $potCommonParent = ""; | ||
| 630 | } elsif ($potCommonParent ne "") { | ||
| 631 | if ($includeCommonRoot and $closeFlag) { | ||
| 632 | ($additionalTopNode) = ($potCommonParent =~ /.*\/(.*?)$/); | ||
| 633 | } | ||
| 634 | $theOffset = $NodesCurrent; | ||
| 635 | $theOffset =~ s/^$potCommonParent//; | ||
| 636 | } else { | ||
| 637 | $theOffset = $NodesCurrent; | ||
| 638 | } | ||
| 639 | my @offsetNodes = split("\/", $theOffset); | ||
| 640 | my $closeFlagSign = ($closeFlag ? "/" : ""); | ||
| 641 | my $stepDir = ($closeFlag ? -1 : 1); | ||
| 642 | my $startNode = ($closeFlag ? scalar(@offsetNodes) -1 : 1); # when closing, always from last node down to common | ||
| 643 | my $endNode = ($closeFlag ? 1 : scalar(@offsetNodes) - 2); # when opening, open only up one before current (currentNode is set specially) | ||
| 644 | if (scalar(@offsetNodes) > 1) { | ||
| 645 | for (my $t = $startNode; ($endNode - $t + $stepDir) != 0; $t+=$stepDir) { | ||
| 646 | $offsetNodesXML.= "<" . $closeFlagSign . $offsetNodes[$t] . ">"; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | # add common node as final closing when closing and includeCommonRoot | ||
| 650 | $offsetNodesXML.= ($additionalTopNode ne "" ? "</$additionalTopNode>" : "") | ||
| 651 | } | ||
| 652 | |||
| 653 | # xmlCorrectChars | ||
| 654 | # | ||
| 655 | # return corrected (= quoted) XML special characters contained in nodeValue | ||
| 656 | sub xmlCorrectChars { | ||
| 657 | my ($nodeValue) = @_; | ||
| 658 | $nodeValue = "" if !defined($nodeValue); | ||
| 659 | $nodeValue =~ s/&/&/; | ||
| 660 | $nodeValue =~ s/</</; | ||
| 661 | $nodeValue =~ s/>/>/; | ||
| 662 | $nodeValue =~ s/"/"/; | ||
| 663 | $nodeValue =~ s/#/'/; | ||
| 664 | return $nodeValue; | ||
| 665 | } | ||
| 666 | |||
| 667 | 1; |