Re: [xsl] Adding prefix to elements when I import a XML file

Subject: Re: [xsl] Adding prefix to elements when I import a XML file
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 31 Oct 2007 11:06:26 +0100
Hi Virginia,

That is an FAQ. Whether you use XSLT 1.0 or 2.0, you can use the document(...) function to process an external xml document (we reserve the term "import" usually for xsl files imported with xsl:import) just the way you would process any other node. I.e.:

<xsl:apply-templates select="document(....)/document" />

Then, secondly, you need to have modified copy template (copy-of makes a deep-copy and does not give you the option to modify its children, use xsl:copy instead), like this:

<xsl:template match="document">
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates  />
   </xsl:copy>
</xsl:template>

<xsl:template match="*">
   <!-- create element with new prefix/namespace -->
   <xsl:element name="xed:{local-name()}" >
      <!-- copy attributes, if any -->
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
   </xsl:element>
</xsl:template>

<!-- non-element nodes -->
<xsl:template match="node()">
   <xsl:copy-of select="." />
</xsl:template>

Thirdly, you must make sure that the xed: prefix exists in the xsl:stylesheet declaration. In XSLT 2.0, you can also use the following, perhaps more clearer syntax:

<xsl:element name="{local-name()}" namespace="http://mynamespace";>

Note that you seem to request a certain prefix, "xed:", but that in XML the prefix does not matter, only the namespace that the prefix is bound to matters.

HTH,
Cheers,
Abel Braaksma

PS: if you process several documents from external sources and you also process a main document, you'd be safest to switch to a different mode when processing your external docs, i.e.:

<xsl:apply-template select="document(....)/document" mode="external-doc" />

and

   <xsl:template match="...." mode="external-doc">
   ...
   </

La RKO wrote:
Suppose that I want to import a xml file into another
file.
The file that I want to import is like:
<document>
<head>this is a headline</head>
<text>this is the text</text>
<caption>this is the caption</caption>
</document>
to do that I use copy-of-select=document(file.xml)...
and it works fine. But I want at the same time to add a prefix to some of
the tags of the imported file, in order to have a
output like:
<document>
<xed:head>this is a headline</xed:head>
<xed:text>this is the text</xed:text>
<xed:caption>this is the caption</xed:caption>
</document>
My question is how can I do that? Import and
transforming at the same time?
Thanks in advance,


Virginia


______________________________________________ Pregunta, Responde, Descubre. Comparte tus consejos y opiniones con los usuarios de Yahoo! Respuestas http://es.answers.yahoo.com/info/welcome

Current Thread