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]
Other format: [Raw text]

Re: How to map XML to XML according to element values


Roy,

There are a number of ways, more-or-less brutish, to accomplish what you 
want to do. The easiest technique to implement and to maintain would use 
keys. It is not the easiest technique to understand because you have to see 
how keys work. Given that, however, it beats any of the other methods.

You would start by declaring a key that would allow you to retrieve your 
employees by their department code:

<xsl:key name="employees-by-dept" match="Employee" use="Dept"/>

this matches Employee elements, using the value of its Dept element child 
as the key for each.

Then, in your template for Department, in addition to doing what you want 
with its children, you merely going and fetching the matching employees:

<xsl:template match="Department">
   <xsl:copy>
   <!-- this copies the Department node itself since we want it in our 
output -->
     <xsl:copy-of select="*"/>
     <!-- inside the Department, we use the copy-of to copy over the existing
          DeptCode and Name children -->
     <xsl:apply-templates select="key('employees-by-dept', DeptCode)"/>
     <!-- this instruction says to go get the nodes returned by the key, using
          the value in the DeptCode child as the key value, and apply templates
          to them. Naturally, since every Department has a different DeptCode
          child, we'll get a different set of Employees for each different
          Department node. -->
   </xsl:copy>
</xsl:template>

then you just need a template to process your Employee elements:

<xsl:template match="Employee">
   <xsl:copy>
     <xsl:copy-of select="EmpNo | Name"/>
     <!-- this time we copy the EmpNo and Name children, but leave out
          the Dept for the obvious reasons. -->
   </xsl:copy>
</xsl:template>

That should do it. Try running it. If you understand XSLT's template-driven 
("rule-based") processing, and see how the key works, it should all make sense.

Cheers,
Wendell


At 01:25 PM 11/26/01, you wrote:
>I have the following XML,
>
><Company>
>   <Departments>
>     <Department>
>       <DeptCode>D1</DeptCode>
>       <Name>Engineering</Id>
>     </Department>
>     <Department>
>       <DeptCode>D2</DeptCode>
>       <Name>Custom Support</Name>
>     </Department>
>   </Departments>
>   <Employees>
>     <Employee>
>       <EmpNo>E1</EmpNo>
>       <Name>John Smith</Name>
>       <Dept>D1</Dept>
>     </Employee>
>     <Employee>
>       <EmpNo>E2</EmpNo>
>       <Name>Mary Johnson</Name>
>       <Dept>D2</Dept>
>     </Employee>
>     <Employee>
>       <EmpNo>E3</EmpNo>
>       <Name>Tom Baker</Name>
>       <Dept>D1</Dept>
>     </Employee>
>   </Employees>
></Company>
>
>I would like to transform it into another XML, by matching <DeptCode> in 
><Department> to <Dept> in <Employee>.  The output XML should look like:
>
><Company>
>   <Departments>
>     <Department>
>       <DeptCode>D1</DeptCode>
>       <Name>Engineering</Id>
>       <Employee>
>         <EmpNo>E1</EmpNo>
>         <Name>John Smith</Name>
>       </Employee>
>       <Employee>
>         <EmpNo>E3</EmpNo>
>         <Name>Tom Baker</Name>
>       </Employee>
>     </Department>
>     <Department>
>       <DeptCode>D2</DeptCode>
>       <Name>Custom Support</Name>
>       <Employee>
>         <EmpNo>E2</EmpNo>
>         <Name>Mary Johnson</Name>
>       </Employee>
>     </Department>
>   </Departments>
></Company>


======================================================================
Wendell Piez                            mailto:wapiez@mulberrytech.com
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


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