help with xpath

Discussion in 'XML' started by T, Jul 15, 2010.

  1. T

    T Guest

    Hi, I'm trying to get the alt text from this bit of docbook xml:
    <inlineequation>
    <inlinemediaobject remap="math">
    <alt>$F$</alt>
    <imageobject>
    <imagedata depth="0px"
    fileref="images/geneug_whatsnew0001.png"
    contentwidth="10px" contentdepth="10px" role="math" />
    </imageobject>
    </inlinemediaobject>
    </inlineequation>

    With this selector:
    <xsl:template match="d:inlineequation/d:inlinemediaobject/
    d:imageobject/d:imagedata[@depth]">
    <xsl:element name="img">
    <xsl:attribute name="alt"><xsl:value-of select="../../alt"/></
    xsl:attribute>
    etc....

    In my Xpath visualizer the path looks right:
    //inlineequation/inlinemediaobject/imageobject/imagedata[@depth]/../../
    alt

    but my stylesheet never renders the alt text. When I use the selector
    in the xsl:template using the [@depth] attribute, is the attribute or
    the element selected?

    Just trying to figure out what is going on.
    thanks,
    --Tim Arnold
     
    T, Jul 15, 2010
    #1
    1. Advertising

  2. T wrote:
    > Hi, I'm trying to get the alt text from this bit of docbook xml:
    > <inlineequation>
    > <inlinemediaobject remap="math">
    > <alt>$F$</alt>
    > <imageobject>
    > <imagedata depth="0px"
    > fileref="images/geneug_whatsnew0001.png"
    > contentwidth="10px" contentdepth="10px" role="math" />
    > </imageobject>
    > </inlinemediaobject>
    > </inlineequation>
    >
    > With this selector:
    > <xsl:template match="d:inlineequation/d:inlinemediaobject/
    > d:imageobject/d:imagedata[@depth]">
    > <xsl:element name="img">
    > <xsl:attribute name="alt"><xsl:value-of select="../../alt"/></
    > xsl:attribute>


    The use of the 'd' prefix on the other elements suggests a namespace
    applies so you want
    ../../d:alt

    Also note that your code is rather verbose, literal result elements and
    attribute value templates are shorter e.g.
    <img alt="{../../d:alt}" />


    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jul 15, 2010
    #2
    1. Advertising

  3. T

    T Guest

    On Jul 15, 11:57 am, Martin Honnen <> wrote:
    > T wrote:
    > > Hi, I'm trying to get the alt text from this bit of docbook xml:
    > > <inlineequation>
    > > <inlinemediaobject remap="math">
    > > <alt>$F$</alt>
    > > <imageobject>
    > >   <imagedata depth="0px"
    > >       fileref="images/geneug_whatsnew0001.png"
    > >       contentwidth="10px" contentdepth="10px" role="math" />
    > > </imageobject>
    > > </inlinemediaobject>
    > > </inlineequation>

    >
    > > With this selector:
    > > <xsl:template match="d:inlineequation/d:inlinemediaobject/
    > > d:imageobject/d:imagedata[@depth]">
    > >   <xsl:element name="img">
    > >     <xsl:attribute name="alt"><xsl:value-of select="../../alt"/></
    > > xsl:attribute>

    >
    > The use of the 'd' prefix on the other elements suggests a namespace
    > applies so you want
    >    ../../d:alt
    >
    > Also note that your code is rather verbose, literal result elements and
    > attribute value templates are shorter e.g.
    >    <img alt="{../../d:alt}" />
    >
    > --
    >
    >         Martin Honnen
    >        http://msmvps.com/blogs/martin_honnen/


    Hi Martin,
    I am getting old and forgetful. Thanks for pointing out I had omitted
    the namespace abbreviation. That was the problem of course...

    Thanks also for the comment about the verbosity. I did not know I
    could use such a simple syntax. The books I have on XSLt show the
    verbose method, which is where I learned it. However, the stylesheet
    fragment I posted is for creating HTML 4.0 instead of XHTML; I think
    that means I must use the verbose method, correct? (since <img ... />
    is illegal in HTML 4.)

    thanks for your help,
    --Tim Arnold
     
    T, Jul 15, 2010
    #3
  4. T wrote:

    > Thanks also for the comment about the verbosity. I did not know I
    > could use such a simple syntax. The books I have on XSLt show the
    > verbose method, which is where I learned it. However, the stylesheet
    > fragment I posted is for creating HTML 4.0 instead of XHTML; I think
    > that means I must use the verbose method, correct? (since <img ... />
    > is illegal in HTML 4.)


    No, the stylesheet creates a result tree which is then optionally
    serialized according to the rules of the output method and that way,
    whether you use
    <xsl:element name="img"/>
    or
    <img/>
    that simply creates an "img" element in the result tree and when that is
    serialized with output method="html" the serialization takes care of
    outputting
    <img>
    according to HTML rules.


    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jul 15, 2010
    #4
  5. T

    T Guest

    On Jul 15, 2:11 pm, Martin Honnen <> wrote:
    > T wrote:
    > > Thanks also for the comment about the verbosity. I did not know I
    > > could use such a simple syntax. The books I have on XSLt show the
    > > verbose method, which is where I learned it.  However, the stylesheet
    > > fragment I posted is for creating HTML 4.0 instead of XHTML; I think
    > > that means I must use the verbose method, correct? (since <img ... />
    > > is illegal in HTML 4.)

    >
    > No, the stylesheet creates a result tree which is then optionally
    > serialized according to the rules of the output method and that way,
    > whether you use
    >    <xsl:element name="img"/>
    > or
    >    <img/>
    > that simply creates an "img" element in the result tree and when that is
    > serialized with output method="html" the serialization takes care of
    > outputting
    >    <img>
    > according to HTML rules.
    >
    > --
    >
    >         Martin Honnen
    >        http://msmvps.com/blogs/martin_honnen/


    Wow, thanks for that information. I'll start changing my coding now--
    it's much easier to understand the way you wrote it.

    thanks again,
    --Tim Arnold
     
    T, Jul 15, 2010
    #5
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,027
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,519
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    570
  4. goog
    Replies:
    0
    Views:
    533
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,698
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page