Skip to content

Commit

Permalink
Copy perl script that uses list_elements.xsl's output to generate a s…
Browse files Browse the repository at this point in the history
…tylesheet that will clone an XML file, explicitly cloning elements and attributes by name
  • Loading branch information
Kevin Lindsey committed Apr 20, 2012
1 parent 59e6f2e commit dd55340
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 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;
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.1">
<xsl:output method="xml" indent="yes"/>
EOS

foreach my $name ( sort keys %elements ) {
print "\n";
print "<xsl:template match=\"$name\">\n";

my $element = $elements{$name};
my $attrs = $element->{attributes};
my $children = $element->{children};

if (@$attrs or @$children) {

print " <xsl:copy>\n";

if (@$attrs) {
foreach my $attr (@$attrs) {
print " <xsl:apply-templates select=\"\@$attr\"/>\n";
}
}

if (@$children) {
foreach my $child (@$children) {
print " <xsl:apply-templates select=\"$child\"/>\n";
}
}

print " </xsl:copy>\n";
} else {
print " <xsl:copy-of select=\".\"/>\n";
}

print "</xsl:template>\n";
}

print <<EOS;
<xsl:template match="@*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
EOS

0 comments on commit dd55340

Please sign in to comment.