| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
## ------------------------------------------------------------------------- |
| 4 |
## $Id: 14_xpath.pl,v 1.1 2003/02/21 08:53:39 joko Exp $ |
| 5 |
## ------------------------------------------------------------------------- |
| 6 |
## $Log: 14_xpath.pl,v $ |
| 7 |
## ------------------------------------------------------------------------- |
| 8 |
|
| 9 |
|
| 10 |
=pod |
| 11 |
|
| 12 |
|
| 13 |
=head3 Overview |
| 14 |
|
| 15 |
This is not the same xupdate currently available from CPAN at |
| 16 |
http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/xupdate . |
| 17 |
|
| 18 |
Its intention - however - is identical: |
| 19 |
xupdate - Process XUpdate commands over an XML document |
| 20 |
|
| 21 |
=head3 Their implementations differ: |
| 22 |
xupdate (by Petr Pajas) uses ... |
| 23 |
XML::XUpdate::LibXML - Simple implementation of XUpdate format |
| 24 |
|
| 25 |
... which is based on XML::LibXML which in turn is: |
| 26 |
This module is an interface to the gnome libxml2 DOM parser (no SAX parser support yet), |
| 27 |
and the DOM tree. It also provides an XML::XPath-like findnodes() interface, providing |
| 28 |
access to the XPath API in libxml2. |
| 29 |
|
| 30 |
|
| 31 |
=head3 In short: |
| 32 |
|
| 33 |
S: It would be nice to have a pure perl thingy which does the same stuff.... |
| 34 |
|
| 35 |
Q: Can we achieve compliance with its API? (just a subset ....) |
| 36 |
|
| 37 |
Q: Can we achieve the processing declared as a XSL document processed using CPAN's XML::XSLT? |
| 38 |
|
| 39 |
Q: Proposal: XML::XUpdate::XSLT!? |
| 40 |
|
| 41 |
Q: Can we mimic/use the interface of the - already established - 'xupdate' program??? |
| 42 |
|
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
|
| 47 |
package main; |
| 48 |
|
| 49 |
use strict; |
| 50 |
use warnings; |
| 51 |
|
| 52 |
BEGIN { |
| 53 |
use lib '../../../nfo/perl/libs'; |
| 54 |
} |
| 55 |
|
| 56 |
use Data::Dumper; |
| 57 |
use XML::XSLT; |
| 58 |
#use XML::DOM; |
| 59 |
#use XML::DOM::Document; |
| 60 |
use shortcuts::files qw( f2s ); |
| 61 |
|
| 62 |
my $file = 'c:/home/amo/develop/top-scores.net/ts/backend/var/resources/control/import_select.xml'; |
| 63 |
|
| 64 |
my $stylesheet = '<?xml version="1.0" encoding="ISO-8859-1"?> |
| 65 |
|
| 66 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 67 |
|
| 68 |
<!-- |
| 69 |
|
| 70 |
Purpose of this XML Stylesheet is to implement a set of templates |
| 71 |
to do simple xml node manipulation. (kinda XML API) |
| 72 |
Main target is "flexible insertion of new xml child nodes". |
| 73 |
|
| 74 |
If this attempt works out well, it will be continued by a follow-up |
| 75 |
trying to implement XUpdate in XSLT. |
| 76 |
|
| 77 |
|
| 78 |
References: |
| 79 |
- XUpdate: |
| 80 |
Requirements: http://www.xmldb.org/xupdate/xupdate-req.html |
| 81 |
Working Draft: http://www.xmldb.org/xupdate/xupdate-wd.html |
| 82 |
- XML API: |
| 83 |
- XML::XUpdate::LibXML: http://search.cpan.org/author/PAJAS/XML-XUpdate-LibXML-0.4.0/lib/XML/XUpdate/LibXML.pm |
| 84 |
- XSL / XSLT: |
| 85 |
http://www.w3.org/TR/xslt |
| 86 |
http://www.xsl-rp.de/ |
| 87 |
http://xml.klute-thiemann.de/w3c-de/REC-xslt-20020318/ |
| 88 |
http://xmlxslt.sourceforge.net/ |
| 89 |
- misc pointers: |
| 90 |
"Re: Modify XML documents": http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/1265431 |
| 91 |
XSL Extensions: http://xmlsoft.org/XSLT/extensions.html |
| 92 |
EXSLT: http://www.exslt.org/set/functions/difference/index.html |
| 93 |
"how to insert element at required position in document tree?": http://p2p.wrox.com/archive/xslt/2001-06/98.asp |
| 94 |
XML APIs for Databases: http://www.javaworld.com/javaworld/jw-01-2000/jw-01-dbxml.html |
| 95 |
|
| 96 |
--> |
| 97 |
|
| 98 |
<xsl:output method="raw"/> |
| 99 |
|
| 100 |
<!-- 0. The root node dispatcher. --> |
| 101 |
<xsl:template match="*"><xsl:call-template name="passthru"/></xsl:template> |
| 102 |
<!-- <xsl:template match="source"><xsl:call-template name="insertChildNode"/></xsl:template> --> |
| 103 |
|
| 104 |
<!-- 1. This is the passthru logic (copy all). --> |
| 105 |
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template> |
| 106 |
|
| 107 |
<!-- 2. This is the manipulation logic: insertChildNode --> |
| 108 |
|
| 109 |
<!-- 2.a. The "API" method. --> |
| 110 |
<xsl:template name="insertChildNode"> |
| 111 |
|
| 112 |
<!-- manipulate node --> |
| 113 |
<xsl:comment> node - begin </xsl:comment> |
| 114 |
|
| 115 |
<!-- <xsl:copy-of select="source" /> --> |
| 116 |
<xsl:copy> |
| 117 |
|
| 118 |
<!-- Pass through children 1:1. --> |
| 119 |
<xsl:apply-templates /> |
| 120 |
|
| 121 |
<!-- Call one of the manipulators - this implements injection behavior. --> |
| 122 |
<xsl:call-template name="clone_last_element" /> |
| 123 |
<xsl:call-template name="inject_template" /> |
| 124 |
|
| 125 |
</xsl:copy> |
| 126 |
<xsl:comment> node - end </xsl:comment> |
| 127 |
|
| 128 |
</xsl:template> |
| 129 |
|
| 130 |
|
| 131 |
<!-- 2.b. The context finder. --> |
| 132 |
|
| 133 |
<!-- 2.c. The node manipulators: They mungle the current context. --> |
| 134 |
|
| 135 |
<!-- copy over node 1:1 (use last element as a template) --> |
| 136 |
<xsl:template name="clone_last_element"> |
| 137 |
<xsl:copy-of select="*[last()]" /> |
| 138 |
</xsl:template> |
| 139 |
|
| 140 |
<xsl:template name="clone_element_nr"> |
| 141 |
<xsl:copy-of select="*[last()]" /> |
| 142 |
</xsl:template> |
| 143 |
|
| 144 |
<!-- creates instance of new element --> |
| 145 |
<xsl:template name="create_fresh_element"> |
| 146 |
<xsl:element name="new">content</xsl:element> |
| 147 |
</xsl:template> |
| 148 |
|
| 149 |
|
| 150 |
<!-- inject other stuff --> |
| 151 |
<xsl:template name="inject_template"> |
| 152 |
<xsl:comment> Hello World! </xsl:comment> |
| 153 |
|
| 154 |
</xsl:template> |
| 155 |
|
| 156 |
|
| 157 |
<!-- Fine stuff from others. --> |
| 158 |
|
| 159 |
<!-- by Mike Kay, Software AG [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] --> |
| 160 |
<!-- enhancements & comments: Andreas Motl, rotamente.de --> |
| 161 |
<xsl:template match="source"> |
| 162 |
<xsl:copy> |
| 163 |
<xsl:copy-of select="@*" /> |
| 164 |
<xsl:apply-templates /> |
| 165 |
<!-- multiple handlers here now: a) create fressshhhh element, b) clone s.th. --> |
| 166 |
<!-- TODO: Do switch/case or couple of if/then statements here to be able to handle that "at runtime". --> |
| 167 |
<xsl:call-template name="create_fresh_element" /> |
| 168 |
<xsl:call-template name="clone_last_element" /> |
| 169 |
</xsl:copy> |
| 170 |
</xsl:template> |
| 171 |
|
| 172 |
|
| 173 |
</xsl:stylesheet> |
| 174 |
|
| 175 |
'; |
| 176 |
|
| 177 |
sub main { |
| 178 |
|
| 179 |
my $xslt = XML::XSLT->new( |
| 180 |
Source => $stylesheet, |
| 181 |
#debug => 1, |
| 182 |
warnings => 1 |
| 183 |
); |
| 184 |
|
| 185 |
#print f2s($file), "\n"; |
| 186 |
|
| 187 |
$xslt->open_xml( Source => f2s($file) ); |
| 188 |
|
| 189 |
$xslt->process(); |
| 190 |
print $xslt->toString(), "\n"; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
main(); |
| 195 |
print "ready.", "\n"; |
| 196 |
|
| 197 |
|
| 198 |
1; |
| 199 |
__END__ |