Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 22nd, 2007, 10:49 PM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default need help on XSLT

HI XSLT/XPATH gurus,

i have the following xml:
<acord>
  <vehicles>
    <vehiclenumber>1</vehiclenumber>
   </vehicles>
  <vehicles>
    <vehiclenumber>2</vehiclenumber>
  </vehicles>
  <coverages>
     <covcd>bi</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
  </coverages>
  <coverages>
     <covcd>pd</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
  </coverages>
<coverages>
     <covcd>bi</covcd>
     <vehiclenumber>2</vehiclenumber>
      <amt>100</amt>
  </coverages>
<coverages>
     <covcd>pd</covcd>
     <vehiclenumber>2</vehiclenumber>
      <amt>100</amt>
  </coverages>
<coverages>
     <covcd>um</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
  </coverages>
<coverages>
     <covcd>um</covcd>
     <vehiclenumber>2</vehiclenumber>
      <amt>100</amt>
  </coverages>

</acord>

i need following out put how to achieve this.

<vehicles>
<vehiclenumber>1</vehiclenumber>
<coverages>
 <covcd>um</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
</coverages>
<coverages>
     <covcd>bi</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
  </coverages>
  <coverages>
     <covcd>pd</covcd>
     <vehiclenumber>1</vehiclenumber>
      <amt>100</amt>
  </coverages>
</vehicles>

any help is greatly appreciated.i can have n number of vehicles. n number of coverages for each vehicles.

Thanks,
Misanaka


 
Old March 23rd, 2007, 01:04 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would suggest a template that matches vehicles and within this apply-templates to a coverages element where vehiclenumber is equal to the current vehiclenumber. Finally a template that matches coverages.

--

Joe (Microsoft MVP - XML)
 
Old March 23rd, 2007, 03:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Not quite sure how your output relates to your input but it looks like a classic grouping problem. Use xsl:for-each-group if you're in XSLT 2.0, or Muenchian grouping (much trickier) in 1.0. For Muenchian grouping see your favourite Wrox textbook or http://www.jenitennison.com/xslt/grouping

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 23rd, 2007, 04:23 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That was my first thought but looking at the XML it seems simpler:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
      <vehicles>
        <xsl:apply-templates select="*/vehicles"/>
      </vehicles>
    </xsl:template>

    <xsl:template match="vehicles">
      <xsl:copy-of select="vehiclenumber"/>
      <xsl:apply-templates select="../coverages[vehiclenumber = current()/vehiclenumber]"/>
    </xsl:template>

    <xsl:template match="coverages">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
Personally I think the output is a poor design, it will be difficult to process, especially in XSLT 1.0 as the information is contained somewhat in the position of the elements. Better to surround each vehicle and its coverages with a vehicle element.

--

Joe (Microsoft MVP - XML)
 
Old March 23rd, 2007, 11:24 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks joefawcett

I solved the problem with your help.

Misanaka

 
Old March 23rd, 2007, 11:26 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mhkay

For your suggestion i solved it using for-each loop.and

Misanaka








Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XSLT with XSLT stonis XSLT 3 April 1st, 2008 08:17 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xslt with an xslt outputfile alleycat XSLT 4 February 20th, 2006 09:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.