XSLT: how to match attribute having a specific value?

Discussion in 'XML' started by Gerald Aichholzer, May 25, 2005.

  1. Hello NG,

    I have an XHMTL-file and would like to replace attribute
    values using XSLT. The XHTML-file contains the following
    code:

    <applet code="MyApplet/MyApplet.class"
    archive="prog/MyApplet.jar"
    width="100%"
    height="99%">
    <param name="image" value="MyAppletImage"/>
    <param name="select" value="MyAppletSelect"/>
    </applet>

    I would like to replace MyAppletImage resp. MyAppletSelect
    with the correspondg values.

    I thought about using the identity transformation plus
    a construct like the following (which does not work,
    unfortunately):

    <xsl:template match="@value='MyAppletImage'">
    <xsl:attribute name="value">
    <xsl:value-of select="$image"/>
    </xsl:attribute>
    </xsl:template>

    The purpose is to replace only these two attributes and
    otherwise leave the original file as it is.

    Any help is appreciated.

    Thanx in advance,
    Gerald
    Gerald Aichholzer, May 25, 2005
    #1
    1. Advertising

  2. Gerald Aichholzer <> writes:

    > Hello NG,
    >
    > I have an XHMTL-file and would like to replace attribute
    > values using XSLT. The XHTML-file contains the following
    > code:
    >
    > <applet code="MyApplet/MyApplet.class"
    > archive="prog/MyApplet.jar"
    > width="100%"
    > height="99%">
    > <param name="image" value="MyAppletImage"/>
    > <param name="select" value="MyAppletSelect"/>
    > </applet>
    >
    > I would like to replace MyAppletImage resp. MyAppletSelect
    > with the correspondg values.
    >
    > I thought about using the identity transformation plus
    > a construct like the following (which does not work,
    > unfortunately):
    >
    > <xsl:template match="@value='MyAppletImage'">


    match patterns have to have the syntax of an XPath expression that
    selects a node set. That is a boolean valued expression. You want
    <xsl:template match="@value[.='MyAppletImage']">

    > <xsl:attribute name="value">
    > <xsl:value-of select="$image"/>

    $image doesn't appear to be defined here so this must be a global
    parameter or variable. That's OK so long as you are replacing all such
    attributes by the same image value.


    > </xsl:attribute>
    > </xsl:template>
    >
    > The purpose is to replace only these two attributes and
    > otherwise leave the original file as it is.
    >
    > Any help is appreciated.
    >
    > Thanx in advance,
    > Gerald


    David
    David Carlisle, May 25, 2005
    #2
    1. Advertising

  3. Hello David,

    David Carlisle wrote:
    > Gerald Aichholzer <> writes:
    >
    >>I have an XHMTL-file and would like to replace attribute
    >>values using XSLT. The XHTML-file contains the following
    >>code:
    >>
    >> <applet code="MyApplet/MyApplet.class"
    >> archive="prog/MyApplet.jar"
    >> width="100%"
    >> height="99%">
    >> <param name="image" value="MyAppletImage"/>
    >> <param name="select" value="MyAppletSelect"/>
    >> </applet>
    >>
    >>I would like to replace MyAppletImage resp. MyAppletSelect
    >>with the correspondg values.
    >>
    >>I thought about using the identity transformation plus
    >>a construct like the following (which does not work,
    >>unfortunately):
    >>
    >> <xsl:template match="@value='MyAppletImage'">

    >
    >
    > match patterns have to have the syntax of an XPath expression that
    > selects a node set. That is a boolean valued expression. You want


    thank you for pointing this out - I should have known that.

    > <xsl:template match="@value[.='MyAppletImage']">


    This solution works perfectly :)

    >> <xsl:attribute name="value">
    >> <xsl:value-of select="$image"/>

    >
    > $image doesn't appear to be defined here so this must be a global
    > parameter or variable. That's OK so long as you are replacing all such
    > attributes by the same image value.


    $image is a global variable (xsl:param). Ideally I would touch
    only value-attributes of param-elements. How could I achieve
    this?

    <xsl:template match="@value[.='MyAppletImage' and ??='param'>


    Thanx for your help,
    Gerald
    Gerald Aichholzer, May 25, 2005
    #3
  4. <xsl:template match="@value[.='MyAppletImage' and ??='param'>

    if your source is xml with html element noames in no-namespace

    <xsl:template match="@value[.='MyAppletImage' and parent::param]>

    or if it is xhtml


    <xsl:template match="@value[.='MyAppletImage' and parent::h:param]>

    where h is bound by
    xmlns:h="http://www.w3.org/1999/xhtml"
    either on this element or on the xsl:stylesheet element.

    David
    David Carlisle, May 25, 2005
    #4
  5. <xsl:template match="@value[.='MyAppletImage' and parent::param]">
    <xsl:template match="@value[.='MyAppletImage' and parent::h:param]">

    or perhaps more naturally (and probably handled more efficiently by your
    processor)

    <xsl:template match="param/@value[.='MyAppletImage']">
    <xsl:template match="h:param/@value[.='MyAppletImage']">

    David
    David Carlisle, May 25, 2005
    #5
  6. Hello David,

    David Carlisle wrote:
    > <xsl:template match="@value[.='MyAppletImage' and parent::param]">
    > <xsl:template match="@value[.='MyAppletImage' and parent::h:param]">
    >
    > or perhaps more naturally (and probably handled more efficiently by your
    > processor)
    >
    > <xsl:template match="param/@value[.='MyAppletImage']">
    > <xsl:template match="h:param/@value[.='MyAppletImage']">
    >


    thank you for your help - works flawlessly :)

    Gerald
    Gerald Aichholzer, May 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bprimuslevy
    Replies:
    1
    Views:
    421
    Soren Kuula
    Aug 23, 2006
  2. =?UTF-8?b?5byg6Z+h5q2m?=
    Replies:
    3
    Views:
    691
    DeepthiShri
    Oct 23, 2006
  3. Stefan_W
    Replies:
    1
    Views:
    8,136
    Stefan_W
    Nov 5, 2008
  4. Marek Mänd
    Replies:
    1
    Views:
    284
    Martin Honnen
    Feb 20, 2005
  5. William FERRERES
    Replies:
    7
    Views:
    198
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page