FOSSology  4.4.0
Open Source License Compliance by Open Source Software
xml2html.php
1 #!/usr/bin/php
2 <?php
3 /*
4 SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
5 
6 SPDX-License-Identifier: GPL-2.0-only
7 */
8 
18 /*
19  * xml2html
20  *
21  * xml2html [- h] -f <file> [-o <file>] -x <file>
22  *
23  * -h usage
24  * -f file the xml input file
25  * -o optional output file, will overwrite if it exists
26  * -x xsl file to use in the transformation
27  *
28  */
29 
30 $usage = "{$argv[0]} [-h] -f <file> [-o <file>] -x <file>\n" .
31  "Options:\n" .
32  "-h: usage\n" .
33  "-f <file>: the xml input file\n" .
34  "-o <file>: optional output filepath, overwritten if exists. StdOut Default\n" .
35  "-x <file>: the xsl style sheet file to use in the transformation\n";
36 
37 // process options
38 $options = getopt('hf:o:x:');
39 
40 if(array_key_exists('h',$options))
41 {
42  echo "$usage\n";
43  exit(0);
44 }
45 
46 if(array_key_exists('f',$options))
47 {
48  // make sure it exists and readable
49  if(is_readable($options['f']))
50  {
51  $xmlFile = $options['f'];
52  }
53  else
54  {
55  echo "FATAL: xml file {$options['f']} does not exist or cannot be read\n";
56  exit(1);
57  }
58 }
59 
60 if(array_key_exists('o',$options))
61 {
62  $outputFile = $options['o'];
63 }
64 
65 if(array_key_exists('x',$options))
66 {
67  // make sure it exists and readable
68  if(is_readable($options['x']))
69  {
70  $xslFile = $options['x'];
71  }
72  else
73  {
74  echo "FATAL: xsl file {$options['x']} does not exist or cannot be read\n";
75  exit(1);
76  }
77 }
78 
79 /* Debug
80 echo "XML2J: after parameter processing\n";
81 echo "infile:$xmlFile,\nout:$outputFile,\nxsl:$xslFile\n";
82 echo "XMLJ2: we are operating at:" . getcwd() . "\n";
83 */
84 
85 $xsl = new XSLTProcessor();
86 $xsldoc = new DOMDocument();
87 $xsldoc->load($xslFile);
88 $xsl->importStyleSheet($xsldoc);
89 
90 $xmldoc = new DOMDocument();
91 @$xmldoc->load($xmlFile);
92 @$transformed = $xsl->transformToXML($xmldoc);
93 
94 if($outputFile)
95 {
96  // open file and write output
97  $OF = fopen($outputFile, 'w') or
98  die("Fatal cannot open output file $outputFile\n");
99  $wrote = fwrite($OF, $transformed);
100  fclose($OF);
101 }
102 else
103 {
104  // no output file, echo to stdout
105  echo $transformed;
106 }
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:66
if(! $Test && $OptionQ) if($stdin_flag) if($Verbose) else
Definition: cp2foss.php:552