Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > help with xpath

Reply
Thread Tools

help with xpath

 
 
T
Guest
Posts: n/a
 
      07-15-2010
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-15-2010
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/
 
Reply With Quote
 
 
 
 
T
Guest
Posts: n/a
 
      07-15-2010
On Jul 15, 11:57*am, Martin Honnen <(E-Mail Removed)> 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
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      07-15-2010
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/
 
Reply With Quote
 
T
Guest
Posts: n/a
 
      07-15-2010
On Jul 15, 2:11*pm, Martin Honnen <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments