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


The following should do what you want.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="Company">
        <Company>
            <xsl:apply-templates select="Departments" />
        </Company>
    </xsl:template>

    <xsl:template match="Departments">
        <Departments>
            <xsl:apply-templates />
        </Departments>
    </xsl:template>

    <xsl:template match="Department">
        <Department>
            <DeptCode>
                <xsl:value-of select="DeptCode"/>
            </DeptCode>
            <Name>
                <xsl:value-of select="Name"/>
            </Name>
            <xsl:apply-templates select="/Company/Employees/Employee[Dept =
current()/DeptCode]" />
        </Department>
    </xsl:template>

    <xsl:template match="Employee">
        <Employee>
            <EmpNo>
                <xsl:value-of select="EmpNo"/>
            </EmpNo>
            <Name>
                <xsl:value-of select="Name"/>
            </Name>
        </Employee>
    </xsl:template>
</xsl:stylesheet>

The key is the select attribute for the xsl:apply-tempates tag under the
Department template.  This will select all employee nodes whose Dept node
matches the current Depatment's DeptCode node.

Also, if your files are likely to get large, you may want to look into the
xsl:key functionality to speed up that XPath query.

	Steve

-----Original Message-----
From: Roy_Lu@notes.toyota.com [mailto:Roy_Lu@notes.toyota.com]
Sent: Monday, November 26, 2001 1:25 PM
To: XSL-List@lists.mulberrytech.com
Subject: [xsl] How to map XML to XML according to element values


Hi, I am new to the list and I have searched the archive but couldn't find
an answer.

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>

Can someone give me an advice? Thanks a lot.

Roy



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

 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]