[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: xsl:transform
Hi Ben,
At 08:19 PM 6/22/01, you wrote: >Hi, > >I would like to transform from one xml into another one >using a 'lookup' xml file.... Assuming your lookup file is in a file 'lookup.xml' in the same subdirectory as your input, try <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="lookup" select="document('lookup.xml')/xref"/> <xsl:template match="/*"> <xsl:variable name="whichfile" select="local-name()"/> <xsl:copy> <xsl:for-each select="*"> <xsl:element name="$lookup/*[local-name()=$whichfile]/abbr[.=local-name(current())]/following-sibling::name"/> <xsl:apply-templates> </xsl:element> </xsl:for-each> </xsl:copy> <xsl:template> </xsl:stylesheet> (Not tested!) Please ask about anything you don't understand here. There's alot of fancy logic buried in the XPath. Since it would require the patience of Jeni to explain, I'll just let you ask about the mysterious parts. Well, okay.... First, a variable is declared, $lookup, which lets us get at the lookup table easily. It'll be the xref node in the lookup table (a good place to reach from). The template matches the element child of the root (the document element), in your examples, either a <file1> or a <file2> element. Its name is bound to the variable $whichfile. Then the element is copied to our result tree. Within this element, we iterate over its element children. These are the <col1> through <col5> elements in your examples. For each of these nodes, we create an element in our output. Its name is determined as follows: $lookup/*[local-name()=$whichfile]/abbr[.=local-name(current())]/following-sibling::name or, in English, from the lookup xref, step to the element child(ren) whose name is the same as $whichfile (remember, the name of the element I'm inside of, either 'file1' or 'file2' as you have it), then to the 'abbr' element child(ren) whose content is the same as the name of the element we're now working with (the col1, col2 or whatever), then to the subsequent 'name' element(s). If your lookup table is correctly constructed, there'll be only one of these. If there are more than one, the name will be the name of the first one. (If this breaks, it's possible that we may have to wrap this whole expression in a string() function: I don't remember.) Within this new element, the content is processed (copying text node content to output by default). Hoping it works, Wendell >Here is the original xml file : > ><?xml version="1.0"?> ><file1> > <col1>01</col1> > <col2>Hello</col2> > <col3>world</col3> > <col4>GTP</col4> > <col5>02 Jul 1999 09:45:05:706</col5> ></file1> > >using this 'lookup' xml file : ><?xml version="1.0"?> ><xref> > <file1> > <abbr>col1</abbr><name>label1</name> > <abbr>col2</abbr><name>label2</name> > <abbr>col3</abbr><name>label3</name> > <abbr>col4</abbr><name>label4</name> > <abbr>col5</abbr><name>label5</name> > </file1> > <file2> > <abbr>col1</abbr><name>header1</name> > <abbr>col2</abbr><name>header2</name> > </file2> ></xref> > > >the xml result is : ><?xml version="1.0"?> ><file1> > <label1>01</label1> > <label2>Hello</label2> > <label3>world</label3> > <label4>GTP</label4> > <label5>02 Jul 1999 09:45:05:706</label5> ></file1> > >If the original xml file : > ><?xml version="1.0"?> ><file2> > <col1>12345</col1> > <col2>Welcome</col2> ></file2> > >using the same 'lookup' xml file, the result is : ><?xml version="1.0"?> ><file2> > <header1>12345</header1> > <header2>Welcome</header2> ></file2> > >Any idea how to ? >Thanks for any suggestions. ====================================================================== 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
|