Using XSLT to preprocess #ifdefs in XML

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!

+2
xml preprocessor xslt
source share
3 answers

The question you are referring to looks like this based on an XML structure that uses XML elements for its ifdefs. In your case, the #ifdef lines #ifdef not XML elements, so you cannot match them with an XSLT template. You are better off using a non-XML tool (perhaps even a regular C preprocessor) to process ifdefs and pass the resulting XML to XSLT if you need to do other processing of XML information.

If you are happy to use real XML elements for your ifdefs, as in the previous question:

 <?xml version="1.0" encoding="UTF-8"?> <main> <fee>blah</fee> <ifdef select="OLD_VERSION"> <fi>blah blah</fi> </ifdef> </main> 

then the question arises of how to treat the xmlver command-line option as the only ifdef that you want to include, and not as the location of the file from which you want to load the entire set of definitions (as the previous question worked). This is much simpler:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:param name="xmlver" /> <xsl:template match="node() | @*"> <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy> </xsl:template> <xsl:template match="ifdef"> <!-- for the ifdef we want, include all its child elements, text nodes etc but not its attributes, for other ifdefs do nothing --> <xsl:if test="@select = $xmlver"> <xsl:apply-templates select="node()" /> </xsl:if> </xsl:template> </xsl:stylesheet> 

In my previous attempt, <xsl:template match="ifdef[@select=$xmlver]"> , which works on some processors but not on xsltproc ( technically not allowed by the XSLT specification , and xsltproc is more strict than my usual test harness, Xalan).

+1
source share

: instead of xsltproc can you just use m4 ?

 <?xml version="1.0" encoding="UTF-8"?> <main> <fee>blah</fee> ifdef(`X',` <fi>blah blah</fi> ') </main> 

.

 $ m4 jeter.xml <?xml version="1.0" encoding="UTF-8"?> <main> <fee>blah</fee> </main> 

.

 $ m4 -DX jeter.xml <?xml version="1.0" encoding="UTF-8"?> <main> <fee>blah</fee> <fi>blah blah</fi> </main> 
+1
source share

I would like to start with your general approach:

answers are not needed by anyone who is not familiar with XSLT. No one has an advanced search on the Internet has shown something useful - XSLT seems remarkably poorly documented and has little discussion on the Internet.

Basically, this tells us that you are trying to write a program in an unfamiliar language, and that you are trying to find information about this language by searching it. Is this a good learning strategy?

I would think that your problem had too many hits, and not too few, for example, "XSLT Tutorial" gives 2,300,000 views. The best of them deserve attention. It seems strange to describe it as "poorly documented and little discussed."

Personally, if I am going to start programming in a new language, I always start by reading a book on this subject. Online material is rarely as carefully designed, written, and reviewed as a textbook. Of course, I would say that, since I am the author of one of the most popular XSLT reference books, but, nevertheless, I believe that this is true.

Now I can help you with your problem. Not really, I'm sorry. Some people answer questions on this forum with proven code that you can use without understanding this. I do not: I just point people in the right direction; and I'm not going to give you code snippets in a language that you donโ€™t understand. Pointing out that you are in the right direction, in this case, means that you must learn the language before trying to use it.

0
source share

All Articles