473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XPath - How to query for <dd> elements ?

Imagine the XHTML "definition list" structure

<dl>
<dt>A</dt>
<dd>a</dd>

<dt>B</dt>
<dd>b1</dd>
<dd>b2</dd>

<dt>C</dt>

<dt>D</dt>
[ ...]
</dl>
Given the element containing "A" as the context node, how can I retrieve
the elements for "a", similarly "B" for "b1,b2" etc. ?

Using ./preceding-sibling::* [1] [self::dd]
I can retrieve the elements for "A" ("a") and "C" (an empty set)
However I can't see any way to get "b1" & "b2", just the first "b1"
Thanks for any advice

May 3 '06 #1
3 3538
The simple-but-ugly way is to explicitly scan through the following
siblings until you hit the next <dt> or run out, using a recursive named
template.

Personally, I'd try to convince whoever was authoring the input document
to abandon the HTML markup on input (even if that's what you generate on
output) and use something with more structure to it, such as:

<deflist>
<entry><term>...</term><def>....</def><def>....</def></entry>
</deflist>

Then it's trivial; the definitions are the <def> children of the
<term>'s parent.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
May 3 '06 #2
Use
following-sibling::dd[preceding-sibling::dt[1]=current()]
get the following dd element whose first preceding dt is the surrent dt
element.
For instance the folllowing stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<result>
<xsl:apply-templates select="//dt"/>
</result>
</xsl:template>

<xsl:template match="dt">
<for value="{.}">
<xsl:for-each

select="following-sibling::dd[preceding-sibling::dt[1]=current()]">
<got value="{.}"/>
</xsl:for-each>
</for>
</xsl:template>
</xsl:stylesheet>

will give you

<?xml version="1.0" encoding="utf-8"?>
<result>
<for value="A">
<got value="a"/>
</for>
<for value="B">
<got value="b1"/>
<got value="b2"/>
</for>
<for value="C"/>
<for value="D"/>
</result>

Note that the above assumes no namespace for the source elements, if
you use the XHTML namespace then you need to qualify the name tests
with the appropriate prefix, that is instead of dd for instance you
should have xhtml:dd and declare the XHTML namespace mapped to xhtml
prefix.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

May 4 '06 #3
George Bina wrote:
following-sibling::dd[preceding-sibling::dt[1]=current()]
get the following dd element whose first preceding dt is the current dt
element.


.... Yep, that should work, and it's certainly more compact.

Performance of this vs. other solutions is going to depend on the exact
characteristics of your XSLT processor, and so is hard to predict
reliably. I'm not sure whether the one I've been using would recognize
the optimization opportunities in that expression or not; I should check.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
May 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: The Plankmeister | last post by:
Hi.. I'm trying to achieve a newline effect within a <dt> block, but am having problems. Here is the basic code: <dl> <dt> <a href="admin_menu.html" class="small">Click here to modify the
9
by: Akseli Mäki | last post by:
Hi, the subject say quite a lot. I have about the following code(4.01 transitional): <p>blaa blaa blaa <ul> <li><a href="foo.html">foo</a></li> <li><a href="bar.html">bar</a></li> </ul>
2
by: AES/newspost | last post by:
In a markup like <dd> <br> some other stuff <br> </dd> the first break seems to add a blank line before the other stuff, but the second doesn't add a blank line after the other stuff. The...
4
by: Andre Majorel | last post by:
I'm trying to make a compact <dl> class à la troff .TP where you have Term Definition of the term. Longer Term Definition of the longer term. i.e. if the <dt> text is shorter than the...
1
by: enrico sabbadin | last post by:
hi, while playing with the SAML specs I found that one must insert a node like this if the call succeded <status xmlns:z=mynamspace" ... <z:statuscode result="z:Sucess"/> ... How do I...
13
by: onetitfemme | last post by:
I think browsers do it pretty much by default. How can you avoid that? It just doesn't look right/pretty for short definitions and also how can you not indent the whole <dd> so that in stead of...
13
by: Mark | last post by:
Dear folks, In Javascript, is it possible to get all id names within, say, a <div></divpair? Like the array of "document.images", I mean. The reason I ask, is that I have a calender whose...
11
by: trackside | last post by:
I'm using the great dd.dragdrop Library for many parts of the page, including loading in a user selected image via the "swapImage()" function. var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i; ...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.