Do I need to expect problems when I add XML elements from a different namespace to an XSLT file? Or are they just ignored (as would be great)?
Background: In a large project, the user can use user-defined tags for text formatting and so on (e.g. \textbf{bold}. They are first transformed to a specific dialect in XML (first using a proprietary tool and then XSLT) and afterwards possibly converted to other dialects such as latex, framemaker, BB code, …
For this reason there are currently the following files:
- Config file for the proprietary tool which translates
\textbf{bold}to<Cmd Name="strong"><param Nr="1">bold</param></Cmd> - XSLT file which translates the XML code above to
<myns:strong>bold</myns:strong> - An XSD file describing the allowed tags and formats
- Multiple xsl files for translating
<myns:strong>bold</myns:strong>to different output formats (e.g. back to\textbf{bold})
Maintaining these files is very difficult because there is no real 1:1 mapping and adding a new command requires changing multiple files in the right way.
Therefore my idea would be to merge these. E.g. a single XML file would contain:
<!-- config file for proprietary tool -->
<repl:Cmd Name="strong"><repl:Param Nr="1"/></repl:Cmd>
<!-- converting to XML dialect -->
<xsl:template mode="Text" match="Cmd[@Name = 'textbf']">
<myns:strong>
<xsl:apply-templates select="Param[@Nr='1']" mode="Text"/>
</myns:strong>
</xsl:template>
<!-- XSD schema for validating XML -->
<xsd:element name="strong" type="tns:GenericTextType">
</xsd:element>
<!-- converting XML dialect to latex code -->
<xsl:template match="myns:strong" mode="Text_toLatex">
<xsl:text disable-output-escaping="yes">\textbf{</xsl:text>
<xsl:apply-templates mode="Text_toLatex"/>
<xsl:text disable-output-escaping="yes">}</xsl:text>
</xsl:template>
which would be much easier to maintain.
There will be no problems. XSLT programs are regular XML documents, you can generally add any elements you like (namespace or not) to the document and they become part of the program.
Note that you can use elements with a namespace everywhere, but elements without a namespace cannot be children of
<xsl:stylesheet>.If they are children of an
<xsl:template>, they will be output into the result. If they are children of the<xsl:stylesheet>itself, they are not output (i.e., they are “ignored”).All you must do is
exclude-result-prefixesdirective..
Nothing will happen with
<repl:Cmd>and<xsd:element>unless you write XSLT code that explicitly uses these nodes. They will be accessible through XPath likedocument('')/*/xsd:element(the*is a short-cut forxsl:stylesheet).In fact this is a common technique to store additional structured data – like configuration or look-up tables – in an XSLT document.
Side note: You generally should not use
disable-output-escaping. In your particular code it’s even superfluous.