From dd553408e8a69b0b696cdcbf0efe238f1c5ca03e Mon Sep 17 00:00:00 2001 From: Kevin Lindsey Date: Fri, 20 Apr 2012 11:05:13 -0500 Subject: [PATCH] Copy perl script that uses list_elements.xsl's output to generate a stylesheet that will clone an XML file, explicitly cloning elements and attributes by name --- .../stylesheets/make_cloning_stylesheet.pl | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 tools/frameworks/stylesheets/make_cloning_stylesheet.pl diff --git a/tools/frameworks/stylesheets/make_cloning_stylesheet.pl b/tools/frameworks/stylesheets/make_cloning_stylesheet.pl new file mode 100755 index 0000000..4105be8 --- /dev/null +++ b/tools/frameworks/stylesheets/make_cloning_stylesheet.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl -w + +# Kevin Lindsey +# 25/Aug/2010 + +# This script consumes the output from running an XML file against +# list_elements.xsl. It assumes the output has been sorted and reduced to +# unique entries only. Element names must preceed child and attribute entries. +# +# This script transforms the output into a XSLT stylesheet that effectively +# clones the XML that list_elements.xsl was run against. The resulting +# stylesheet explicitly copies attributes and matches children. Thus, the +# stylesheet can be used as a starting point when transforming one XML format +# to another + +my %elements; + +# build intermediate element list with attributes and children +while (<>) { + chomp; + + next if /^\s*$/; + + if (/^([^>]+)>(.+)$/) { + push @{$elements{$1}->{children}}, $2; + } elsif (/^([^@]+)@(.+)$/) { + push @{$elements{$1}->{attributes}}, $2; + } else { + $elements{$_} = {attributes => [], children => []}; + } +} + +# output cloning stylesheet +print < + + +EOS + +foreach my $name ( sort keys %elements ) { + print "\n"; + print "\n"; + + my $element = $elements{$name}; + my $attrs = $element->{attributes}; + my $children = $element->{children}; + + if (@$attrs or @$children) { + + print " \n"; + + if (@$attrs) { + foreach my $attr (@$attrs) { + print " \n"; + } + } + + if (@$children) { + foreach my $child (@$children) { + print " \n"; + } + } + + print " \n"; + } else { + print " \n"; + } + + print "\n"; +} + +print < + + + + +EOS +