This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: extrapolating attributes to child nodes


>I want to convert xml as follows:
>From:
><set = 5>
>  <A>
(some deleted)
>  </A>
>  </set=5>
>  To:
>  <directory name="A" global_id="4:5" gid="10" perm="777" />
>  <directory name="B" global_id="5:5" gid="11" perm="444" />
>
>  Where global_id=uid:set, and set. Anything other than set and elements 
>under
>  <attrib> can be safely assumed to be directories. A set is to be listed
as
>  part of global_id in each child node.
>
>  I use the following hardcoded xslt such as <xsl:template match="A">..., 
>but
>  I am seeking more dynamic solution such that it would parse from the root

>node,
>  and construct global_id from the <set> and <uid>.
>

Here's something. By "more dynamic," it doesn't count on the directory
element's names being "A" and "B" but instead searches from the root for any
descendant element with an attrib element as a child. Also, <set = 5></set =
5> doesn't make any sense, so I changed it to <set uid="5"></set>before I
ran this to test it. 

  <xsl:template match="/">
    <xsl:for-each select="//*[attrib]">
      <directory name="{name()}" 
       global_id="{normalize-space(attrib/uid)}:{ancestor::node()/@uid}"
       gid="{normalize-space(attrib/gid)}" 
       perm="{normalize-space(attrib/perm)}"/><xsl:text><!-- carriage return
-->
</xsl:text>
    </xsl:for-each>
  </xsl:template>

Is this what you meant?

Bob DuCharme          www.snee.com/bob           <bob@  
snee.com>  "The elements be kind to thee, and make thy
spirits all of comfort!" Anthony and Cleopatra, III ii


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]