XSLT match question

Discussion in 'XML' started by Rainer Herbst, Sep 30, 2003.

  1. Hi *,

    please consider the following problem:
    I have a XML document which includes some html elements.
    I want to replace only the <div> element:

    I specified two templates, one matches everything ("*"),
    one matches only the "div". As far as I understand, the most specific
    rule should apply, i.e. the div rule if the element is a <div>.

    But the div rule is never applied! Where is my mistake?

    TIA!
    Rainer Herbst


    The XML document:
    <?xml version="1.0" encoding="UTF-8"?>
    <page>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="HTML Tidy, see www.w3.org" name="generator"/>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
    <meta content="index" name="robots"/>
    <meta content="Universitaet Potsdam" name="keywords"/>
    <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/>
    <meta content="FrontPage.Editor.Document" name="ProgId"/>
    <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
    icon"/>
    <title>Homepage der Universität Potsdam</title>
    </head>
    <body vlink="#808080" link="#336699" bgcolor="#ffffff">
    <div>wrong!!!</div>
    <p align="center">unterschrift</p>
    </body>
    </html>
    <dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0"
    name="pressemitteilungen" lastModified="1064833858000" date="29.09.03
    13:10" size="4096" sort="name" reverse="false" requested="true">
    <dir:file name="first.xml" lastModified="1064833858000" date="29.09.03
    13:10" size="62"/>
    </dir:directory>
    </page>


    My stylesheet:

    <?xml version="1.0" standalone="no"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dir="http://apache.org/cocoon/directory/2.0" >


    <!-- all elements -->
    <xsl:template match="*">
    <xsl:copy select=".">
    <xsl:for-each select="@*">
    <xsl:attribute name="{name(.)}">
    <xsl:value-of select="."/>
    </xsl:attribute>
    </xsl:for-each>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="div">
    <xsl:for-each
    select="/page/dir:directory[@name='pressemitteilungen']/dir:file">
    <xsl:value-of select="@name"/>
    </xsl:for-each>
    </xsl:template>

    <xsl:template match="/page">
    <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="dir:directory">
    </xsl:template>

    </xsl:stylesheet>



    --
    ------------------------------------------------
    Rainer Herbst Linux - Registered
    ZEIK User #319157
    Universität Potsdam Usual disclaimers applied!
    ------------------------------------------------
     
    Rainer Herbst, Sep 30, 2003
    #1
    1. Advertising

  2. Rainer Herbst wrote:
    > Hi *,
    >
    > please consider the following problem:
    > I have a XML document which includes some html elements.
    > I want to replace only the <div> element:
    >
    > I specified two templates, one matches everything ("*"),
    > one matches only the "div". As far as I understand, the most specific
    > rule should apply, i.e. the div rule if the element is a <div>.
    >


    That's not true, not the most specific rule is applied. The first rule
    applied is the firts rule that is declared, for instance, in your sample
    the first rule in that the expression match results true is:

    <xsl:template match="*">

    on the other hand you can declare a template with a specific priority
    (with attribute priority). In this case, nor the first tempalte is
    applied, the template with more priority is applied, in your sample if
    you daclare that the second template has more priority this will be applied.

    Francesc Guim Bernat

    http://francesc.guim.net


    > But the div rule is never applied! Where is my mistake?
    >
    > TIA!
    > Rainer Herbst
    >
    >
    > The XML document:
    > <?xml version="1.0" encoding="UTF-8"?>
    > <page>
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head>
    > <meta content="HTML Tidy, see www.w3.org" name="generator"/>
    > <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
    > <meta content="index" name="robots"/>
    > <meta content="Universitaet Potsdam" name="keywords"/>
    > <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/>
    > <meta content="FrontPage.Editor.Document" name="ProgId"/>
    > <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
    > icon"/>
    > <title>Homepage der Universität Potsdam</title>
    > </head>
    > <body vlink="#808080" link="#336699" bgcolor="#ffffff">
    > <div>wrong!!!</div>
    > <p align="center">unterschrift</p>
    > </body>
    > </html>
    > <dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0"
    > name="pressemitteilungen" lastModified="1064833858000" date="29.09.03
    > 13:10" size="4096" sort="name" reverse="false" requested="true">
    > <dir:file name="first.xml" lastModified="1064833858000" date="29.09.03
    > 13:10" size="62"/>
    > </dir:directory>
    > </page>
    >
    >
    > My stylesheet:
    >
    > <?xml version="1.0" standalone="no"?>
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:dir="http://apache.org/cocoon/directory/2.0" >
    >
    >
    > <!-- all elements -->
    > <xsl:template match="*">
    > <xsl:copy select=".">
    > <xsl:for-each select="@*">
    > <xsl:attribute name="{name(.)}">
    > <xsl:value-of select="."/>
    > </xsl:attribute>
    > </xsl:for-each>
    > <xsl:apply-templates/>
    > </xsl:copy>
    > </xsl:template>
    >
    > <xsl:template match="div">
    > <xsl:for-each
    > select="/page/dir:directory[@name='pressemitteilungen']/dir:file">
    > <xsl:value-of select="@name"/>
    > </xsl:for-each>
    > </xsl:template>
    >
    > <xsl:template match="/page">
    > <xsl:apply-templates />
    > </xsl:template>
    >
    > <xsl:template match="dir:directory">
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    >
    >
     
    Francesc Guim Bernat, Sep 30, 2003
    #2
    1. Advertising

  3. In article <blbba3$sn$-potsdam.de>, Rainer Herbst wrote:
    > Hi *,
    >
    > please consider the following problem:
    > I have a XML document which includes some html elements.
    > I want to replace only the <div> element:
    >
    > I specified two templates, one matches everything ("*"),
    > one matches only the "div". As far as I understand, the most specific
    > rule should apply, i.e. the div rule if the element is a <div>.
    >
    > But the div rule is never applied! Where is my mistake?
    >
    > TIA!
    > Rainer Herbst
    >
    >
    > The XML document:
    > <?xml version="1.0" encoding="UTF-8"?>


    The document is not encoded in UTF-8. See later comment.

    > <page>
    > <html xmlns="http://www.w3.org/1999/xhtml">


    Here you give this html element and it sub-elements a namespace. The
    matches in the XSL must be on the namespace qualified element name.

    > <head>
    > <meta content="HTML Tidy, see www.w3.org" name="generator"/>
    > <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
    > <meta content="index" name="robots"/>
    > <meta content="Universitaet Potsdam" name="keywords"/>
    > <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/>
    > <meta content="FrontPage.Editor.Document" name="ProgId"/>
    > <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
    > icon"/>
    > <title>Homepage der Universität Potsdam</title>


    The above line has a non UTF-8 sequence of bytes after "Universit".

    > </head>
    > <body vlink="#808080" link="#336699" bgcolor="#ffffff">
    > <div>wrong!!!</div>
    > <p align="center">unterschrift</p>
    > </body>
    > </html>


    [snip]

    > My stylesheet:
    >
    > <?xml version="1.0" standalone="no"?>
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:dir="http://apache.org/cocoon/directory/2.0" >
    >
    >
    > <!-- all elements -->
    > <xsl:template match="*">
    > <xsl:copy select=".">


    Note: the copy element does not have a select attribute.

    > <xsl:for-each select="@*">
    > <xsl:attribute name="{name(.)}">
    > <xsl:value-of select="."/>
    > </xsl:attribute>
    > </xsl:for-each>
    > <xsl:apply-templates/>
    > </xsl:copy>
    > </xsl:template>
    >
    > <xsl:template match="div">


    This does not match the div elements in the document because the div
    elements in the document are in a namespace. You need to match on
    something like xhtml:div where the xhtml: prefix is associated with
    the correct namespace using an attribute like

    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    [snip]
     
    A. Bolmarcich, Sep 30, 2003
    #3
  4. In article <>,
    Francesc Guim Bernat <> wrote:
    %
    %
    % Rainer Herbst wrote:

    % > one matches only the "div". As far as I understand, the most specific
    % > rule should apply, i.e. the div rule if the element is a <div>.

    % That's not true, not the most specific rule is applied. The first rule
    % applied is the firts rule that is declared, for instance, in your sample
    % the first rule in that the expression match results true is:
    %
    % <xsl:template match="*">
    %
    % on the other hand you can declare a template with a specific priority

    If you don't apply a priority, one will be applied for you. The effect
    of this implicit priority is, roughly speaking, that a more specific
    rule will be applied over a more general rule, assuming they both
    match the same node.

    If you have two templates which have the same priority, and they
    match the same node, you have an error, although processors are
    allowed to deal with it. I think they generally take the last
    template defined.

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Sep 30, 2003
    #4
  5. >
    > If you don't apply a priority, one will be applied for you. The effect
    > of this implicit priority is, roughly speaking, that a more specific
    > rule will be applied over a more general rule, assuming they both
    > match the same node.


    I'm not agree with you, is possible that i'm wrong, but i'll expose my
    arguments. I've this xml document:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <node name="MyName">
    Text
    </node>
    <node name="YourName">
    Text
    </node>
    </root>

    and i apply this simple template:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="root">
    <newRoot>
    <xsl:for-each select="node">
    <xsl:apply-templates select="."/>
    </xsl:for-each>
    </newRoot>
    </xsl:template>
    <xsl:template match="node">
    <msg> I'm the less specific template </msg>
    </xsl:template>
    <xsl:template match="node[name='MyName']">
    <msg> I'm one of the most specific template (MyName) </msg>
    </xsl:template>
    <xsl:template match="node[name='YourName']">
    <msg> I'm one of the most specific template (Your name) </msg>
    </xsl:template>
    </xsl:stylesheet>

    Where the second template is less specific than the third and fourth
    template.
    The result of apply this last stylesheet on the first xml is this:


    <?xml version="1.0" encoding="UTF-8"?>
    <newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <msg> I'm the less specific template </msg>
    <msg> I'm the less specific template </msg>
    </newRoot>

    in this sample the less specific rule have been applied, having both
    templates the same priority. Then i think that nor the most spicific
    rule is applied , is applied the firts one (with the same priority).

    Francesc Guim

    http://francesc.guim.net
     
    Francesc Guim Bernat, Oct 1, 2003
    #5
  6. Francesc Guim Bernat schrieb:
    >>
    >> If you don't apply a priority, one will be applied for you. The effect
    >> of this implicit priority is, roughly speaking, that a more specific
    >> rule will be applied over a more general rule, assuming they both
    >> match the same node.

    >
    >
    > I'm not agree with you, is possible that i'm wrong, but i'll expose my
    > arguments. I've this xml document:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <root>
    > <node name="MyName">
    > Text
    > </node>
    > <node name="YourName">
    > Text
    > </node>
    > </root>
    >
    > and i apply this simple template:
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:fo="http://www.w3.org/1999/XSL/Format">
    > <xsl:template match="root">
    > <newRoot>
    > <xsl:for-each select="node">
    > <xsl:apply-templates select="."/>
    > </xsl:for-each>
    > </newRoot>
    > </xsl:template>
    > <xsl:template match="node">
    > <msg> I'm the less specific template </msg>
    > </xsl:template>
    > <xsl:template match="node[name='MyName']">
    > <msg> I'm one of the most specific template (MyName) </msg>
    > </xsl:template>
    > <xsl:template match="node[name='YourName']">
    > <msg> I'm one of the most specific template (Your name) </msg>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > Where the second template is less specific than the third and fourth
    > template.
    > The result of apply this last stylesheet on the first xml is this:
    >
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format">
    > <msg> I'm the less specific template </msg>
    > <msg> I'm the less specific template </msg>
    > </newRoot>
    >
    > in this sample the less specific rule have been applied, having both
    > templates the same priority. Then i think that nor the most spicific
    > rule is applied , is applied the firts one (with the same priority).
    >
    > Francesc Guim
    >
    > http://francesc.guim.net
    >

    shouldn't it be [@name='MyName']?


    --
    ------------------------------------------------
    Rainer Herbst Linux - Registered
    ZEIK User #319157
    Universität Potsdam Usual disclaimers applied!
    ------------------------------------------------
     
    Rainer Herbst, Oct 1, 2003
    #6
  7. Yes, my StyleSheet is wrong. You're in the correct way,i don't know why,
    but i thought that i had red what i was saying in some XLST book, and of
    course i didn't. Thanks for your replies.

    Francesc Guim

    pd: the correct output was :

    <?xml version="1.0" encoding="UTF-8"?><newRoot
    xmlns:fo="http://www.w3.org/1999/XSL/Format"><msg> I'm one of the most
    specific template (MyName) </msg><msg> I'm one of the most specific
    template (Your name) </msg></newRoot>


    Rainer Herbst wrote:
    > Francesc Guim Bernat schrieb:
    >
    >>>
    >>> If you don't apply a priority, one will be applied for you. The effect
    >>> of this implicit priority is, roughly speaking, that a more specific
    >>> rule will be applied over a more general rule, assuming they both
    >>> match the same node.

    >>
    >>
    >>
    >> I'm not agree with you, is possible that i'm wrong, but i'll expose my
    >> arguments. I've this xml document:
    >>
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <root>
    >> <node name="MyName">
    >> Text
    >> </node>
    >> <node name="YourName">
    >> Text
    >> </node>
    >> </root>
    >>
    >> and i apply this simple template:
    >>
    >> <xsl:stylesheet version="1.0"
    >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >> xmlns:fo="http://www.w3.org/1999/XSL/Format">
    >> <xsl:template match="root">
    >> <newRoot>
    >> <xsl:for-each select="node">
    >> <xsl:apply-templates select="."/>
    >> </xsl:for-each>
    >> </newRoot>
    >> </xsl:template>
    >> <xsl:template match="node">
    >> <msg> I'm the less specific template </msg>
    >> </xsl:template>
    >> <xsl:template match="node[name='MyName']">
    >> <msg> I'm one of the most specific template (MyName) </msg>
    >> </xsl:template>
    >> <xsl:template match="node[name='YourName']">
    >> <msg> I'm one of the most specific template (Your name) </msg>
    >> </xsl:template>
    >> </xsl:stylesheet>
    >>
    >> Where the second template is less specific than the third and fourth
    >> template.
    >> The result of apply this last stylesheet on the first xml is this:
    >>
    >>
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format">
    >> <msg> I'm the less specific template </msg>
    >> <msg> I'm the less specific template </msg>
    >> </newRoot>
    >>
    >> in this sample the less specific rule have been applied, having both
    >> templates the same priority. Then i think that nor the most spicific
    >> rule is applied , is applied the firts one (with the same priority).
    >>
    >> Francesc Guim
    >>
    >> http://francesc.guim.net
    >>

    > shouldn't it be [@name='MyName']?
    >
    >
     
    Francesc Guim Bernat, Oct 1, 2003
    #7
    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. hiwa
    Replies:
    0
    Views:
    640
  2. Victor
    Replies:
    2
    Views:
    649
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    373
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    490
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    168
Loading...

Share This Page