Conflicting templates

Discussion in 'XML' started by patrik.nyman@orient.su.se, Mar 6, 2007.

  1. Guest

    I have this templates to mark up hyphenation over line breaks:

    <xsl:template match="reg[@type='hyp']">
    <xsl:apply-templates select="@orig"/>
    </xsl:template>

    <xsl:template match="reg[@type='hyp']/@orig">
    <xsl:call-template name="html-hyphens"/>
    </xsl:template>

    <xsl:template name="html-hyphens">
    <xsl:param name="w" select="."/>
    <xsl:variable name="car"
    select="substring-before($w,'|')"/>
    <xsl:variable name="cdr"
    select="substring-after($w,'|')"/>
    <xsl:choose>
    <xsl:when test="$cdr">
    <xsl:value-of select="concat($car,'-')"/>
    <br/>
    <xsl:call-template name="html-hyphens">
    <xsl:with-param name="w" select="$cdr"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$w"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    I also have this simple template for italic text:

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

    This works fine if I write something like:

    <it><reg type="hyp" orig="Hyphe|nated">Hyphenated</reg></it>

    Now I can choose if I want to preserve linebreaks or not.
    But as it happens, in some of the texts I'm working on,
    sometimes ony part of the word is in italics (or formatted
    some other way), like <it>Hyphe</it>nated, and since I
    can't do

    <reg type="hyp" orig="<it>Hyphe</it>|nated"><it>Hyphe</it>nated</
    reg>

    I must write something like:

    <choice type="hyp">
    <orig><it>Hyphe</it>|nated</orig>
    <reg><it>Hyphe</it>nated</reg>
    </choice>

    So for this I added the following templates:

    <xsl:template match="choice[@type='hyp']">
    <xsl:apply-templates select="./orig"/>
    </xsl:template>

    <xsl:template match="choice[@type='hyp']/orig">
    <xsl:call-template name="html-hyphens"/>
    </xsl:template>

    But the combination of the <it> and <choose> elements
    are not working. Can anyone help me out?

    Thanks a lot
    /Patrik Nyman
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. wrote:
    > I have this templates to mark up hyphenation over line breaks:


    I'll try to look at this in more detail later, but... Y'know, this is
    the sort of task that I would suggest handling in the redering engine,
    rather than at the XML level. The renderer is the level which knows
    where line breaks are going to have to be inserted, and is traditionally
    where hyphenation is done as a result. That's generally driven by a
    dictionary, though it could certainly take advantage of hints in the source.





    >
    > <xsl:template match="reg[@type='hyp']">
    > <xsl:apply-templates select="@orig"/>
    > </xsl:template>
    >
    > <xsl:template match="reg[@type='hyp']/@orig">
    > <xsl:call-template name="html-hyphens"/>
    > </xsl:template>
    >
    > <xsl:template name="html-hyphens">
    > <xsl:param name="w" select="."/>
    > <xsl:variable name="car"
    > select="substring-before($w,'|')"/>
    > <xsl:variable name="cdr"
    > select="substring-after($w,'|')"/>
    > <xsl:choose>
    > <xsl:when test="$cdr">
    > <xsl:value-of select="concat($car,'-')"/>
    > <br/>
    > <xsl:call-template name="html-hyphens">
    > <xsl:with-param name="w" select="$cdr"/>
    > </xsl:call-template>
    > </xsl:when>
    > <xsl:eek:therwise>
    > <xsl:value-of select="$w"/>
    > </xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:template>
    >
    > I also have this simple template for italic text:
    >
    > <xsl:template match ="it">
    > <i><xsl:apply-templates/></i>
    > </xsl:template>
    >
    > This works fine if I write something like:
    >
    > <it><reg type="hyp" orig="Hyphe|nated">Hyphenated</reg></it>
    >
    > Now I can choose if I want to preserve linebreaks or not.
    > But as it happens, in some of the texts I'm working on,
    > sometimes ony part of the word is in italics (or formatted
    > some other way), like <it>Hyphe</it>nated, and since I
    > can't do
    >
    > <reg type="hyp" orig="<it>Hyphe</it>|nated"><it>Hyphe</it>nated</
    > reg>
    >
    > I must write something like:
    >
    > <choice type="hyp">
    > <orig><it>Hyphe</it>|nated</orig>
    > <reg><it>Hyphe</it>nated</reg>
    > </choice>
    >
    > So for this I added the following templates:
    >
    > <xsl:template match="choice[@type='hyp']">
    > <xsl:apply-templates select="./orig"/>
    > </xsl:template>
    >
    > <xsl:template match="choice[@type='hyp']/orig">
    > <xsl:call-template name="html-hyphens"/>
    > </xsl:template>
    >
    > But the combination of the <it> and <choose> elements
    > are not working. Can anyone help me out?
    >
    > Thanks a lot
    > /Patrik Nyman
    >



    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 6, 2007
    #2
    1. Advertising

  3. (Apologies for not trimming the quote.)
     
    Joseph Kesselman, Mar 6, 2007
    #3
  4. Guest

    On 6 Mar, 16:16, Joseph Kesselman <> wrote:
    > wrote:
    > > I have this templates to mark up hyphenation over line breaks:

    >
    > I'll try to look at this in more detail later, but... Y'know, this is
    > the sort of task that I would suggest handling in the redering engine,
    > rather than at the XML level. The renderer is the level which knows
    > where line breaks are going to have to be inserted, and is traditionally
    > where hyphenation is done as a result. That's generally driven by a
    > dictionary, though it could certainly take advantage of hints in the source.


    I fully agree, but in this particular case, I'm doing markup of
    pre-existing texts (from 18th century books) and need to
    be able to make different versions, one that preserves the
    original line breaks, and one where the rendering engine
    (i.e., LaTeX) decides where to break lines. Hence
    my markup, and my problem!
     
    , Mar 6, 2007
    #4
  5. wrote:

    > I fully agree, but in this particular case, I'm doing markup of
    > pre-existing texts (from 18th century books) and need to
    > be able to make different versions, one that preserves the
    > original line breaks, and one where the rendering engine
    > (i.e., LaTeX) decides where to break lines. Hence
    > my markup, and my problem!


    Why dont you use one of the existing packages
    for scholarly critical editions ?

    http://www.ucl.ac.uk/~ucgadkw/edmac/index.html
    http://ednotes.sty.de.vu/
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Mar 6, 2007
    #5
  6. J├╝rgen Kahrs wrote:
    > http://ednotes.sty.de.vu/


    Hm. ledmac is LaTeX-based, and I know folks have experiemented with XML
    front-end syntaxes for LaTeX...

    But I'm not going to dismiss this out of hand; sometimes inventing a new
    markup is legitimate in order to get the behaviors you want. (After all
    that's how edmac/ledmac started). So let's ignore the question of why
    two alternate renderings are desired think about how we achieve them. My
    instinct says stylesheet modes, but I want to look at the question again.

    I'm a bit distracted right now; I'll try to get back to this later.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 6, 2007
    #6
  7. wrote:
    > But the combination of the <it> and <choose> elements
    > are not working.


    One obvious question: Do you have templates for <orig> and <reg>? Or at
    least an explicit identity template which is catching these? If not,
    you'll get XSLT's built-in default template, which just returns the
    contained text -- and as a result you'll lose any markup below that
    point, specifically the <it>.


    ("Not working" isn't a very useful description. It'd help if you could
    post a minimal example of an input document, and a complete stylesheet
    that contains just enough logic to demonstrate what you're doing and
    what isn't working. Runnable examples are easier to review/debug than
    out-of-context fragments, because they make obvious the answers to
    questions like the one I just raised.)


    Another obvious alternative would be to turn the markup inside out:
    <it><reg type="hyp" orig="Hyphe|">Hyphe</reg></it>nated

    Or to reconsider the whole thing and use something more like
    <it>Hyphe<hyp/></it>nated
    and then have <hyp/> either render as your visible hyphen or as nothing
    depending on whether people want to see it.

    (If your markup is making life complicated, maybe it's the wrong markup...)

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 6, 2007
    #7
  8. Guest

    On 6 Mar, 22:27, Joseph Kesselman <> wrote:
    > wrote:
    > > But the combination of the <it> and <choose> elements
    > > are not working.

    > ("Not working" isn't a very useful description. It'd help if you could
    > post a minimal example of an input document, and a complete stylesheet
    > that contains just enough logic to demonstrate what you're doing and
    > what isn't working.


    Of course, se bottom of this post

    > Another obvious alternative would be to turn the markup inside out:
    > <it><reg type="hyp" orig="Hyphe|">Hyphe</reg></it>nated
    >
    > Or to reconsider the whole thing and use something more like
    > <it>Hyphe<hyp/></it>nated
    > and then have <hyp/> either render as your visible hyphen or as nothing
    > depending on whether people want to see it.


    Originally, I wanted the <reg type="hyp" orig="Hyphe|
    nated">Hyphenated</reg>
    markup so the hyphenated word would be avaliable for greping and
    database
    searches, etc. But now I realize if I'm gonna stick <it>'s in there
    it
    can't be greped anyway... So maybe I need something like

    <choice type="hyp">
    <orig><it>Hyphe</it>|nated</orig>
    <reg><it>Hyphe</it>nated</reg>
    <plain>Hyphenated</plain>
    </choice>

    And then it starts getting out of hand...

    Anyway, it would be interesting if my example could be made to work.
    It would be useful in other situations as well. So here's the example.
    Nr 1 and 4 works as expected, nr 2 gives no italics, nr 3 gives no
    output at all.

    %<--------------------------------------------------------------------

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="application/xml" href="./hyp.xsl"?>

    <!DOCTYPE TEST>

    <TEST>

    <it><reg type="hyp" orig="Hyphe|nated-1">Hyphenated-1</reg></it>

    <choice type="hyp">
    <orig><it>Hyphe</it>|nated-2</orig>
    <reg><it>Hyphe</it>nated-2</reg>
    </choice>

    <choice type="hyp">
    <it><orig>Hyphe|nated-3</orig></it>
    <it><reg>Hyphenated-3</reg></it>
    </choice>

    <it>
    <choice type="hyp">
    <orig>Hyphe|nated-4</orig>
    <reg>Hyphenated-4</reg>
    </choice>
    </it>

    </TEST>

    %<--------------------------------------------------------------------

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.pnp-software.com/XSLTdoc">

    <xsl:eek:utput method="html"/>

    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>

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

    <xsl:template match="reg[@type='hyp']">
    <xsl:apply-templates select="@orig"/>
    </xsl:template>

    <xsl:template match="reg[@type='hyp']/@orig">
    <xsl:call-template name="html-hyphens"/>
    </xsl:template>

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

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

    <xsl:template match="choice[@type='hyp']">
    <xsl:apply-templates select="./orig"/>
    </xsl:template>

    <xsl:template match="choice[@type='hyp']/orig">
    <xsl:call-template name="html-hyphens"/>
    </xsl:template>

    <xsl:template name="html-hyphens">
    <xsl:param name="w" select="."/>
    <xsl:variable name="car"
    select="substring-before($w,'|')"/>
    <xsl:variable name="cdr"
    select="substring-after($w,'|')"/>
    <xsl:choose>
    <xsl:when test="$cdr">
    <xsl:value-of select="concat($car,'-')"/>
    <br/>
    <xsl:call-template name="html-hyphens">
    <xsl:with-param name="w" select="$cdr"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$w"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>

    %<-------------------------------------------------------------

    /Patrik Nyman
     
    , Mar 7, 2007
    #8
  9. > nr 2 gives no italics, nr 3 gives no output at all.

    OK, let's look at it.

    #2:
    "choice[@type='hyp']" applies templates against its <orig> child.
    That's matched by the template for "choice[@type='hyp']/orig".
    This hands off to named template "html-hyphens" with no parameter.
    The parameter's default value is ., which will be the <orig> element.
    All that's done with the parameter is to take its string value.
    String value of that <orig> is the concatenation of its text
    descendants; the <it> is correctly ignored.


    #3 is even more obvious: You're explicitly processing the <orig> child.
    There is no <orig> child; the child elements are both <it> elements.


    You're getting exactly what you asked for. If it isn't what you wanted,
    slow down and think about exactly what you're trying to recognize at
    each stage and what you want to do with it. XSLT is a programming
    language; it does only and exactly what you tell it to do.

    Coming up with a version which handles all four cases is left as an
    exercise for the student. Hint: If you want to recognize the two options
    and select between them no matter how deeply buried they are in other
    markup, you're going to have to select which subtree to process based
    upon its descendants.

    Personally, I still think you're needlessly complicating the problem.
    Were I in your shoes I'd be fighting to get the markup designed from
    first principles in a form that's actually amenable to straightforward
    processing rather than trying to tolerate sloppy usage. You should be
    able to achieve that while still retaining all the historical
    information, if you take the time to structure your descriptive
    hierarchies correctly.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Mar 8, 2007
    #9
  10. Guest

    On 8 Mar, 02:59, Joe Kesselman <> wrote:
    > > nr 2 gives no italics, nr 3 gives no output at all.

    >
    > OK, let's look at it.
    >


    Thanks for your input. I think your hints will help me solve
    the problem.

    /Patrik
     
    , Mar 8, 2007
    #10
    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. Will

    Conflicting namespaces??

    Will, Aug 13, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,692
    Chris R. Timmons
    Aug 13, 2003
  2. Guadala Harry

    Conflicting Controls (who dunit?)

    Guadala Harry, Sep 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    386
    Ken Dopierala Jr.
    Sep 26, 2004
  3. darrel
    Replies:
    2
    Views:
    1,963
    Guest
    Jan 18, 2005
  4. JKop
    Replies:
    3
    Views:
    477
  5. recover
    Replies:
    2
    Views:
    809
    recover
    Jul 25, 2006
Loading...

Share This Page