Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Copy perl script that uses list_elements.xsl's output to generate a s…

…tylesheet that will clone an XML file, explicitly cloning elements and attributes by name
  • Loading branch information...
commit dd553408e8a69b0b696cdcbf0efe238f1c5ca03e 1 parent 59e6f2e
authored April 20, 2012
82  tools/frameworks/stylesheets/make_cloning_stylesheet.pl
... ...
@@ -0,0 +1,82 @@
  1
+#!/usr/bin/perl -w
  2
+
  3
+# Kevin Lindsey
  4
+# 25/Aug/2010
  5
+
  6
+# This script consumes the output from running an XML file against
  7
+# list_elements.xsl.  It assumes the output has been sorted and reduced to
  8
+# unique entries only. Element names must preceed child and attribute entries.
  9
+#
  10
+# This script transforms the output into a XSLT stylesheet that effectively
  11
+# clones the XML that list_elements.xsl was run against. The resulting
  12
+# stylesheet explicitly copies attributes and matches children. Thus, the
  13
+# stylesheet can be used as a starting point when transforming one XML format
  14
+# to another
  15
+
  16
+my %elements;
  17
+
  18
+# build intermediate element list with attributes and children
  19
+while (<>) {
  20
+	chomp;
  21
+	
  22
+	next if /^\s*$/;
  23
+	
  24
+	if (/^([^>]+)>(.+)$/) {
  25
+		push @{$elements{$1}->{children}}, $2;
  26
+	} elsif (/^([^@]+)@(.+)$/) {
  27
+		push @{$elements{$1}->{attributes}}, $2;
  28
+	} else {
  29
+		$elements{$_} = {attributes => [], children => []};
  30
+	}
  31
+}
  32
+
  33
+# output cloning stylesheet
  34
+print <<EOS;
  35
+<xsl:stylesheet
  36
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  37
+  version="1.1">
  38
+
  39
+<xsl:output method="xml" indent="yes"/>
  40
+EOS
  41
+
  42
+foreach my $name ( sort keys %elements ) {
  43
+	print "\n";
  44
+	print "<xsl:template match=\"$name\">\n";
  45
+
  46
+	my $element = $elements{$name};
  47
+	my $attrs = $element->{attributes};
  48
+	my $children = $element->{children};
  49
+
  50
+	if (@$attrs or @$children) {
  51
+		
  52
+		print "  <xsl:copy>\n";
  53
+
  54
+		if (@$attrs) {
  55
+			foreach my $attr (@$attrs) {
  56
+				print "    <xsl:apply-templates select=\"\@$attr\"/>\n";
  57
+			}
  58
+		}
  59
+	
  60
+		if (@$children) {
  61
+			foreach my $child (@$children) {
  62
+				print "    <xsl:apply-templates select=\"$child\"/>\n";
  63
+			}
  64
+		}
  65
+		
  66
+		print "  </xsl:copy>\n";
  67
+	} else {
  68
+		print "  <xsl:copy-of select=\".\"/>\n";
  69
+	}
  70
+	
  71
+	print "</xsl:template>\n";
  72
+}
  73
+
  74
+print <<EOS;
  75
+
  76
+<xsl:template match="@*">
  77
+  <xsl:copy-of select="."/>
  78
+</xsl:template>
  79
+
  80
+</xsl:stylesheet>
  81
+EOS
  82
+

0 notes on commit dd55340

Please sign in to comment.
Something went wrong with that request. Please try again.