xslt - How to add @xml:base to my .xml files? -
i need stylesheet convert docbook xml files include xml:base element section tags. how go doing (since xml:base needs system , node info???)
input:
<section xmlns="http://docbook.org/ns/docbook" xml:id="voidvisit" version="5"> <title>void</title> <section> <title>screenshot</title> <mediaobject> <imageobject> <imagedata fileref="screenshots/dialog.png" /> </imageobject> </mediaobject> </section> </section>
output:
... <section xml:id="void" version="5" xml:base="file:/c:/projects/my/proj/trunk/spec/module/components/void.xml"> <title>void</title> <section> <title>screenshot</title> <mediaobject> <imageobject> <imagedata fileref="screenshots/dialog.png"/> </imageobject> </mediaobject> </section> ...
in xslt 2.0 (xpath 2.0) functions static-base-uri()
, base-uri()
can used purpose.
here working example:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="/"> <doc xml:base="{base-uri()}"/> </xsl:template> </xsl:stylesheet>
when transformation applied, wanted result produced:
<doc xml:base="file:/c:/cvs-ddn/fxsl-xslt2/data/marrowtr.xml"/>
so, complete transformation in case is:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:d="http://docbook.org/ns/docbook"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/d:section"> <xsl:copy> <xsl:attribute name="xml:base" select="base-uri(/)"/> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
in xslt 1.0 solution is pass base-uri external parameter (global xsl:param>
) transformation.
Comments
Post a Comment