xml - XSLT Input file with settings - change multiple output files -
i'm using xslt edit multiple files. have input file settings file, looks this:
<root> <file> <folderin>/var/in/</folderin> <in>10</in> <folderout>/var/out</folderout> <out>20</out> <name>first file</name> </file> </root>
example file:
<root> <element1 id1="10"> ... </element1> <element2 id2="10" attribute="xyz"> ... </element2> </root>
and xslt file:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:template name="copyxml" match="/*"> <xsl:for-each select="file"> <xsl:variable name="inputfile" select="concat(folderin, in, '.xml')"/> <xsl:variable name="outputfile" select="concat(folderout, out, '.xml')"/> <xsl:result-document method="xml" href="{$outputfile}"> <xsl:copy-of select="document($inputfile)/*"/> </xsl:result-document> </xsl:for-each> </xsl:template> </xsl:stylesheet>
in /var/in/ there few hundred files want edit few (in example /var/in/10.xml exit file /var/out/20.xml). in new files want change id1 , id2 attribute value "out" param
i've managed copy specific files adequate names xslt, i'm having problem applying template change files. i've tried using apply-templates cannot make apply templates $outputfile, it's either not doing or appending $outputfile copy settings file;/ have idea how it?
edit: output example file:
<root> <element1 id1="20"> ... </element1> <element2 id2="20" attribute="xyz"> ... </element2> </root>
at moment stylesheet has:
<xsl:result-document method="xml" href="{$outputfile}"> <xsl:copy-of select="document($inputfile)/*"/> </xsl:result-document>
so, creating output file will contain copy of input file.
in order apply modifications, must apply templates input file, need have:
<xsl:result-document method="xml" href="{$outputfile}"> <!-- further process input file --> <xsl:apply-templates select="document($inputfile)/*"/> </xsl:result-document>
and of course stylesheet will have contain templates handle input file content, copying , modifying relevant parts.
you try incremental approach problem:
- firstly, define stylesheet rules transform a single input file single output file
- once these templates need, copy them stylesheet applied settings file
this stylesheet should need:
xslt 2.0:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <!-- template process settings file --> <xsl:template match="/"> <xsl:for-each select="root/file"> <xsl:variable name="inputfile" select="concat(folderin, in, '.xml')"/> <xsl:variable name="outputfile" select="concat(folderout, out, '.xml')"/> <xsl:result-document method="xml" href="{$outputfile}"> <xsl:apply-templates select="document($inputfile)/*" mode="processfiles"> <xsl:with-param name="outputvalue" select="out" tunnel="yes"/> </xsl:apply-templates> </xsl:result-document> </xsl:for-each> </xsl:template> <!-- template process input files --> <!-- identity transformation --> <xsl:template match="* | @* | text()" mode="processfiles"> <xsl:copy> <xsl:apply-templates select="* | @* | text()" mode="#current"/> </xsl:copy> </xsl:template> <!-- update parameters --> <xsl:template match="@id1 | @id2" mode="processfiles"> <xsl:param name="outputvalue" tunnel="yes"/> <xsl:attribute name="{name()}"> <xsl:value-of select="$outputvalue"/> </xsl:attribute> </xsl:template> </xsl:stylesheet>
notable points:
- you cannot copy input file, have apply templates in order change parts of it
- while applying templates external files, parameter called
outputvalue
passed around; using tunnel parameter spares trouble of explicitly passing every time callxsl:apply-templates
- i used templates
mode="processfile"
process content of external files; in simple situation not strictly necessary, while working larger stylesheet little "trick" helps avoiding priority conflicts between templates settings file , external files- an identity template copies elements, attribute , text nodes
- a specific template attributes
id1
,id2
uses tunnel parameter replace existing value desired one
Comments
Post a Comment