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 November 11th, 2006, 02:33 AM
Registered User
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML "Selective" translation : Need Help

Dear gurus,


I want to use xslt to translate an xml file into another xml file. But the new xml file will only omit all the elements that name on the old xml. It just choose one element that has the highest value available.

Example:
Original xml

<?xml version="1.0"?>
<Values>



  <PacketType>TWO_DH3</PacketType>
  <PacketType>THREE_DH5</PacketType>
  <PacketType>TWO_DH5</PacketType>
  <PacketType>THREE_DH3</PacketType>
  <PacketType>ONE_DH3</PacketType>
  <PacketType>ONE_DH5</PacketType>

   <otherelement1> some other elements</otherelement1>
   <otherelement2> some other elements</otherelement2>
   <otherelement3> some other elements</otherelement3>

</Value>


######## Desired OUTPUT ###############

<?xml version="1.0"?>
<Values>

  <!--Only the highest PacketType from Input is sellected here in this case THREE_DH5
       -->
  <PacketType>THREE_DH5</PacketType>

   <otherelement1> some other elements</otherelement1>
   <otherelement2> some other elements</otherelement2>
   <otherelement3> some other elements</otherelement3>

</Value>


Could any one please give me some hints on how to make this translation posible.

I am realy appreciated.

Charles Hua
 
Old November 11th, 2006, 05:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Can one assume all the PacketType names begin with the English name of a one-digit number, like ONE, FOUR, NINE? And this is followed by "_DH", then another digit? Because the programming for this depends very much on those assumptions.

I think your best approach is a two-phase transformation. The first phase adds an attribute to the PacketType which contains a sort key, for example translating ONE_DH5 to 15, and the second phase selects the last in sorted order. The translation of ONE_DH5 to 15 can be done by logic such as

<xsl:choose
  <xsl:when tests="startsWith(packetType, 'ONE_')">
    <xsl:text>1</xsl:text>

etc, and you can pick out the final digit with

xsl:value-of select="substring-after(packetType, '_DH')

There are a number of facilities in XSLT 2.0 that simplify the coding, for example you can use regular expressions, and you can also collapse it into a single phase of processing by using a stylesheet function to compute the sort key; but the basic logic is similar.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 11th, 2006, 01:39 PM
Registered User
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for you hint Michael, I appreciated.
 
Old November 11th, 2006, 10:27 PM
Registered User
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Michael,

      Thank you again for the hint you gave earlier. I now can get the result by two-phrase transformation.
However, I found out that two-phrase transformation requires at least two *.xsl files. I have to use 3 xsl files for the example above

So my new question come up. Is there anyway to do this transformation by only one .xsl file?


I want to thank you one !more time, And thank anyone who read this post!

Charles Hua
 
Old November 12th, 2006, 05:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If your processor has an xx:node-set() extension function (the only mainstream processor that doesn't is the Transformiix processor used by Firefox) then you can do a 2-phase transformation within a single stylesheet using multiple modes. The typical pattern is

<xsl:template match="/">
  <xsl:variable name="v1">
    <xsl:apply-templates mode="phase1"/>
  </xsl:variable>
  <xsl:apply-templates select="xx:node-set($v1)" mode="phase2"/>
</xsl:template>

With a 2.0 processor you don't need the extension, just change the second call to

<xsl:apply-templates select="$v1" mode="phase2"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
XML Schema translation w/ included schemas elliottg XSLT 2 May 16th, 2008 04:14 PM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
WordprocessingML 2 XML Translation ROCXY XSLT 3 July 11th, 2007 04:47 AM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM





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