Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Copy certain nodes [Thread Next] Re: Copy certain nodesTo: NULL Date: 10/2/2006 12:25:00 AM mark wrote: > Peter Flynn napisal(a): >> You're asking two different things here. Do you want: >> >> a) all <file> elements which contain <size> and <mode> elements, >> reproducing only those elements; >> or >> b) all <file> elements regardless, but reproducing only <size> >> and <mode> if they have them. >> >> Your data and query is ambiguous in this regard. >> >> ///Peter > > Thanks for your replay. > One time it would be <size> and <modified>, the oher time just <owner>. > But always I want to preserve the source xml structure and contain in > <file> all elements given as a parameter. I hope I explained thing out. Close enough :-) This XSLT requires the parameter "include" to contain a string value of the required element type names, separates by spaces. There is probably a more elegant way but it's late. <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="include"/> <xsl:template match="root"> <root> <xsl:apply-templates/> </root> </xsl:template> <xsl:template match="file"> <xsl:variable name="elements"> <xsl:for-each select="*"> <xsl:if test="contains($include,name())"> <xsl:text>y</xsl:text> </xsl:if> </xsl:for-each> </xsl:variable> <xsl:if test="contains($elements,'y')"> <file name="{@name}"> <xsl:apply-templates select="*[contains($include,name())]" mode="included"/> </file> </xsl:if> </xsl:template> <xsl:template match="*" mode="included"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> How you pass the parameter depends on your XSLT processor and your operating environment. Using Saxon, I typed java -jar /saxon/saxon8.jar -o test.new test.xml test.xsl "include=\"modified owner\"" which gave me the result in test.new: <?xml version="1.0" encoding="UTF-8"?> <root> <file name="AAA"> <modified>2</modified> <owner>Tom</owner> </file> <file name="BBB"> <modified>6</modified> <owner>Al</owner> </file> <file name="CCC"> <modified>6</modified> <owner>Jack</owner> </file> <file name="DDD"> <modified>4</modified> <owner>Jane</owner> </file> </root> ///Peter -- XML FAQ: http://xml.silmaril.ie/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
