OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: XML/XSL help

[ Lists Home | Date Index | Thread Index ]
  • From: "G. Ken Holman" <gkholman@CraneSoftwrights.com>
  • To: "'xml-dev@lists.xml.org'" <xml-dev@lists.xml.org>
  • Date: Tue, 01 Aug 2000 20:27:57 -0400

At 00/08/01 19:54 -0400, Ravi Ramamirtham wrote:
>I am trying to generate an XML fragment from multivalued LDAP attributes. My
>data(in DSML form) looks like:
>
>         <attr name="xyz">
>             <value>1</value>
>             <value>2</value>
>             </attr>
>
>             <attr name="abc">
>             <value>10</value>
>             <value>20</value>
>         </attr>
>
>The XML I want should look something like:
>
>         <data>
>             <xyz>1</xyz>
>             <abc>10</abc>
>         </data>
>
>         <data>
>             <xyz>2</xyz>
>             <abc>20</abc>
>         </data>
>I am having difficulties writing XSL to transform the DSML to XML.

XSL, XSLT and XPath questions would be better posted to the following list:

   http://www.mulberrytech.com/xsl/xsl-list

There are a number of subscribers who would enthusiastically respond to 
such questions.

>Any
>help/pointers/suggestions would be much appreciated.

I think the most appropriate solution uses a recursively-called template, 
since I'm guessing there may be mis-matched counts of <value> children of 
<attr> elements so looping as long as there are *any* children at a 
particular index value would produce the result you need.

I am unfamiliar with the LDAP requirements you are trying to fulfill, so 
I'm not sure about the integrity of matched numbers of value children.

I've attached the shape of a solution below ... it keeps looping through 
the data set as long as there are values to be output.  It will work 
regardless of whether there are matched counts of children or not.

I hope this helps.

................. Ken


T:\ftemp>type ravi.xml
<?xml version="1.0"?>
<info>
         <attr name="xyz">
             <value>1</value>
             <value>2</value>
             </attr>

             <attr name="abc">
             <value>10</value>
             <value>20</value>
         </attr>
</info>
T:\ftemp>type ravi.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/info"> <!--package results in doc. elem.-->
   <result>
     <xsl:call-template name="loop"/>
   </result>
</xsl:template>

<xsl:template name="loop">    <!--recursively-called construct-->
   <xsl:param name="index" select="1"/>
        <!--only put out correlated values if there is an entry-->
   <xsl:if test="attr/value[$index]">   <!--at least one exists-->
     <data>           <!-- put out each value for a given entry-->
       <xsl:for-each select="attr[value[$index]]">
         <xsl:element name="{@name}">           <!--named value-->
           <xsl:value-of select="value[$index]"/>
         </xsl:element>
       </xsl:for-each>
     </data>
     <xsl:call-template name="loop">     <!--check for any more-->
       <xsl:with-param name="index" select="$index + 1"/>
     </xsl:call-template>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt ravi.xml ravi.xsl ravi.out

T:\ftemp>type ravi.out
<?xml version="1.0" encoding="utf-8"?>
<result>
<data>
<xyz>1</xyz>
<abc>10</abc>
</data>
<data>
<xyz>2</xyz>
<abc>20</abc>
</data>
</result>


--
G. Ken Holman                    mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Web site: XSL/XML/DSSSL/SGML services, training, libraries, products.
Book: Practical Transformation Using XSLT and XPath ISBN1-894049-05-5
Next instructor-led training:    2000-09-19/20,2000-10-03,2000-10-04,
-              2000-10-05,2000-10-19,2000-11-12,2000-11-13,2001-01-27





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS