Adam Cohen wrote:
I'm rather new to XSL, and was hoping that someone could help me
with the following problem:
I have an XML file that describes various items for a computer store, things
like hard drives, optical drives, network cards, etc. Each item has the
same attribute list, and are grouped under elements such as <hard_disk>,
<video_card>, etc. But in some instances, I have further sub-categorized
items under their respective manufacturer, if a given company makes more
than one item. For example, for video cards, let's say I have brand_s and
brand_t. Brand_s only makes one type of video card, so I put that directly
under the <video_card> element. But brand_t makes two types of video cards,
so I create a new element called <brand_t_vid_card> and below that I have
both of the video cards made by brand_t, such as <video_card_type_1> and
<video_card_type_2>. Now, I want to use an XSL to translate this XML file
into one large HTML file, listing all of the different components, but I run
into a problem when I use a simple <xsl:for-each select="component/*/*/*">
statement, since this will only traverse down 3 levels from the <component>
element, but in some cases (as noted above), I'll need to traverse further
down to the next element. I'm sure there is an easy way to do this, but I
have only limited knowledge of XSL, and I would rather not have to
restructure the entire XML file so that it works with my single for-each
statement. If anyone can make sense of what I'm talking about, I would
greatly appreciate an answer. Below is an example of my XML file to further
make my question more clear. Thanks in advance!
Not the most sensible XML, but you knew that.
If you can say what test you can subject a component/*/*/* element to that
makes it clear whether to use that element itself or its child elements, it
shouldn't be too difficult. Perhaps the presence of the 'manufacturer'
attribute can be the test. Take the union of the ones that have a manufacturer
and the ones that are children of those that don't.
component/*/*/*[(_at_)manufacturer] | component/*/*/*[not(@manufacturer)]/*
or perhaps more simply
component/*/*/descendant::*[(_at_)manufacturer]
- Mike
____________________________________________________________________________
mike j. brown | xml/xslt: http://skew.org/xml/
denver/boulder, colorado, usa | resume: http://skew.org/~mike/resume/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list