|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Flatten XML to load into Oracle DB
Hi Max,
It appears that you are flattening your hierarchy with one row for every FROMDATE element (multiple FROMDATE elements can share a single THEKEY parent). The easiest way to achieve this is to start by "reaching in" and selecting these FROMDATE descendants of the root: <xsl:template match="/">
<ROWSET>
<xsl:apply-templates select="//FROMDATE"/>
</ROWSET>
</xsl:template>Having done this, you will need a template matching the FROMDATE elements selected, which generates each row: <xsl:template match="FROMDATE"> <ROW> <xsl:attribute name="num"> <xsl:number level="any"/> <!-- xsl:number is a convenient and robust way of generating the row number --> </xsl:attribute> <!-- ... then, more logic ... --> </ROW> </xsl:template> (You could have <ROW num="{position()}"> here but it would be more fragile in the face of future changes to the stylesheet, if that's an issue.) So far so good -- but how to fill in "more logic"? At this point there are two general approaches. Since your target element structure is very regular, you could achieve this once again by "pulling" the relevant values from your source. This would be easy enough to do inside the same template as: <THEKEY>
<!-- retrieve the needed value from the parent node, THEKEY -->
<xsl:value-of select="../text()"/>
</THEKEY>
<FROMDATE>
<!-- likewise, except from the context node, the matched FROMDATE -->
<xsl:value-of select="text()"/>
</FROMDATE>
<type>
<!-- likewise... -->
<xsl:value-of select="type/text()"/>
</type>
... etc ...Alternatively, a somewhat more sophisticated approach would be more easily maintainable and extensible (maybe you want to continue using this stylesheet with other data structures similar to but not identical with what you have here): continue using templates. In this case, using a mode, as your first effort does, is a good idea. So instead of the literal result elements above you'd do: <xsl:apply-templates select="parent::THEKEY | self::FROMDATE | type | type/*" mode="flatten"/> ...which selects the parent, the FROMDATE node itself, the "type" child node, and its children, to process them using templates in the "flatten" mode. Note that each of these will be processed in turn, in document order, but not nested inside one another (unless logic in the "flatten" templates you invoke here does some nesting) -- thereby achieving your flattening. Templates in this mode can simply write out the elements you need as in: <xsl:template match="THEKEY" mode="flatten">
<xsl:copy>
<xsl:value-of select="text()"/>
</xsl:copy>
</xsl:template>or even, for the simpler cases, <xsl:template match="SOMEGROUP" mode="flatten"> <!-- this node can simply be copied over --> <xsl:copy-of select="."> </xsl:template> (I won't write them all out, but leave that for you to figure.) I hope this isn't too cryptic! If it is, please ask about anything you find confusing. Cheers, Wendell At 01:10 PM 2/10/2004, you wrote:
====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ====================================================================== XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








