Re: [xsl] unused elements and attributes

Subject: Re: [xsl] unused elements and attributes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 20 Jul 2001 16:05:21 +0100
Hi Samina,

> In my XSL stylesheet I have created a new namespace called moreover.
> Is there any way in XSL to create new attributes for all the unused
> attributes and elements(from the XML doc being passed in)?

As you describe, then you want to create copies of the article, url,
headline_text and harvest_time elements as they stand. You can do this
with an identity template like:

<xsl:template match="article | url | headline_text | harvest_time">
  <xsl:copy>
    <!-- copy any attributes -->
    <xsl:copy-of select="@*" />
    <!-- apply templates to content -->
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

For all the other elements, you want to create new elements with the
same local name but with a different namespace, which you've
associated with the 'moreover' prefix, probably in the xsl:stylesheet
start tag with:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:moreover="http://www.moreover.com/xml";>
...
</xsl:stylesheet>

You can create elements with generated names using the xsl:element
instruction, with the name attribute holding an attribute value
template for the name of the new element. In your case the new name
should be 'moreover:' (the prefix for the moreover namespace) + the
local name of the element. So you need a template that looks like:

<!-- match any element that doesn't have its own template -->
<xsl:template match="*">
  <!-- create element in moreover namespace -->
  <xsl:element name="moreover:{local-name()}">
    <!-- copy attributes -->
    <xsl:copy-of select="@*" />
    <!-- apply templates to get content -->
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

The xsl:apply-templates in this template will get the text of an
element if it contains text, or move on to its child elements if it
contains child elements.

BTW, you will probably want to add 'moreovernews' to the list of
elements that are copied rather than moved into the moreover
namespace.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread