This is apparently an almost carbon copy. Using XSLT as an XML Pre-Processor. But since the OP of this question did not publish a complete example, even though it was being asked answers, no one is familiar with XSLT. None of the extensive web searches found anything useful - XSLT seems very poorly documented and has little discussion on the Internet.
Anyway...
I have an XML file, say foo.xml, as follows (greatly simplified, obviously):
<?xml version="1.0" encoding="UTF-8"?> <main> <fee>blah</fee> <ifdef select="OLD_VERSION"> <fi>blah blah</fi> </ifdef> </main>
(C-style #ifdef is replaced with an ifdef block in the light of Jan Roberts answer)
I want to run the xsltproc command on linux, as shown below:
xsltproc --stringparam xmlver NEW_VERSION --nonet foo.xslt foo.xml
and for this, use the following XSLT file, foo.xslt, to exclude the #ifdef 'section:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:param name="xmlver" required="yes"/> <xsl:template match="node() | @*"> <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy> </xsl:template> <xsl:variable name="defines" select="document($xmlver)/defines"/> <xsl:template match="ifdef"> <xsl:variable name="this" select="."/> <xsl:for-each select="$defines[def = $this/@select]"> <xsl:apply-templates select="$this/node()" /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
(I used the answers to the question mentioned above to build this XSLT, but the missing component is where / how to include the value โxmlver.โ Of course, there is no guarantee that this is correct in the above, but itโs basically what I I ask - how does it all come together in such a way that it works?)
Any constructive answers will be highly appreciated and will undoubtedly be useful to many people with a similar requirement in the future; but please donโt get tired, dogmatic "Why do you do this?" answer!
xml preprocessor xslt
John R Ramsden
source share