Re: [xsl] Can I use <xsl:key>

Subject: Re: [xsl] Can I use <xsl:key>
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Mon, 12 Sep 2005 11:15:57 +0200
On 9/7/05, aaron apigo <aaronjose_apigo2001@xxxxxxxxx> wrote:
> Hi Ragulf,
>   Thanks for your response. I see your point, I'm a
> beginner in XSLT programming, that's why sometimes I
> rely on this forum in search for my problem. I've
> tried different simple XML structure, and I transform
> it with want I want, but the XML that I posted really
> bothered me, I know that for some, it only simple, but
> as a newbie, its hard for me to transform that.

Hi Aaron,

I try to pay back the help I have received from this list by replying
to other newcomers. The problem is that I am only infrequently
checking this list, so mails styled directly to me is not a good idea
as it tends to make other people on this list not reply to your

I have looked at what you have written, and while you say it must be
simple, I have not found it so. The reason being, that you have gotten
to your problem in a wholly procedural way. If you are responsible for
the XML as well as the transformation, I would recommend that you
scratch all and restart, using a wholly different approach.

I can give you some pointers:

Make it a tree structure. Your understanding of the structure of the
information, in your example, is depending on textual values in an
attribute (@name), which is not good.

Having a solution dependent on Disable-Output-Escaping (DOE) is a very
very bad way of doing things. It is almost never necessary to use
such, and I believe that in your case it is not necessary either. You
can check this list for reasons why not to use it.

<xsl:template match="//text:user-field-get....

In a match attribute, the // in front are useless, as every
user-field-get element (in text namespace) is a child of the root

More of use would be if you wanted different behavior for a given
element, depending on what kind of element it is a child of.

<xsl:template match="Parent1/TheElement">Do something</xsl:template>
<xsl:template match="Parent2/TheElement">Do something else</xsl:template>

Here, both can be applied using <xsl:apply-templates
select="//TheElement"/> which selects all TheElement elements in the
document. You can use other access modifiers to narrow down the list
of TheElements in your set.

Even if you cannot change the XML, I would think you should make a
wholly different approach. What I said about using DOE is still very

In your example, I would make a solution more like (untested and
namespace removed for clarity of solution):

<xsl:template match="root">
  <xsl:apply-templates select="p"/>

<xsl:template match="p">
  <xsl:apply-templates select="span/user-field-get"/>

<xsl:template match="user-field-get[@name='given-name' or
@name='surname' or @name ='e-adress']">
  <xsl:element name="{@name}">
  <xsl:value-of select="../following-sibling::span/text()"/>

<xsl:template match="user-field-get">
  <!-- Do nothing here (this is to discard elements that have a name
atribute with value '/e-adress' or '/author', which are totally
unnecessary for the transformation).
  The more specific templates are chosen (that is, if
@name='given-name', then this template is not matches, but the one
above is) -->

I hope this is of help to you.

Ragulf Pickaxe :-)

Current Thread