[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: Re: How to map XML to XML according to element values
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 26 Nov 2001 15:43:02 -0500
xml to xml map elements
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@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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.