Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Kevin Lindsey
committed
Apr 20, 2012
1 parent
59e6f2e
commit dd55340
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|