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 9th, 2016, 09:19 AM
Authorized User
 
Join Date: Nov 2015
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
Default Group XML message based on a particular sequence

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<Languages>
<Language>
<Language>German</Language>
<Proficiency>Native</Proficiency>
</Language>
<Language>
<Language>English</Language>
<Proficiency>Data Migration</Proficiency>
</Language>
<Language>
<Language>English</Language>
<Proficiency>Business</Proficiency>
</Language>
<Language>
<Language>French</Language>
<Proficiency>Conversational</Proficiency>
</Language>
</Languages>

Output XML

<?xml version="1.0" encoding="UTF-8"?>
<Languages>
<Language>
<Language>German</Language>
<Proficiency>Native</Proficiency>
</Language>
<Language>
<Language>English</Language>
<Proficiency>Business</Proficiency>
</Language>
<Language>
<Language>French</Language>
<Proficiency>Conversational</Proficiency>
</Language>
<Language>
<Language>English</Language>
<Proficiency>Data Migration</Proficiency>
</Language>
</Languages>

Need to form the xml such as the sequence of XML tags comes like Native,Business,Conversational and then Data Migration.

I was using the below code but its not working as expected

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="xs:sequence[parent::xs:complexType/@name='Languages']">
<xsl:copy>
<xsl:apply-templates select="xs:element[@ref='Native']"/>
<xsl:apply-templates select="xs:element[@ref='Business']"/>
<xsl:apply-templates select="xs:element[@ref='Conversational']"/>
<xsl:apply-templates select="xs:element[@ref='Data Migration']"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
 
Old March 9th, 2016, 10:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Not sure if the schema based approach is something that would work, but basically your template is not matching, so everything is going through the identity template.

The following gives the correct output.
Code:
 <xsl:template match="Languages">
      <xsl:copy>
         <xsl:apply-templates select="*[Proficiency='Native']"/>
         <xsl:apply-templates select="*[Proficiency='Business']"/>
         <xsl:apply-templates select="*[Proficiency='Conversational']"/>
         <xsl:apply-templates select="*[Proficiency='Data Migration']"/>
      </xsl:copy>
   </xsl:template>
You could change Proficiency to * to match on any child node irrespective of name.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 9th, 2016, 10:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Code:
<xsl:template match="xs:sequence[parent::xs:complexType/@name='Languages']">
This is completely muddled, I don't know where you got this idea from. You are processing the instance document, with element names like Language and Profiency. Element names like xs:sequence and xs:complexType are found only in the schema.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Group XML message based on 3 keys Lipsita XSLT 6 March 10th, 2016 05:12 AM
Transform Attribute based xml to element based xml pallone XSLT 6 February 2nd, 2016 07:59 AM
Help me to select group based on maximum value CCPuser XSLT 3 August 31st, 2012 03:09 PM
select the group of rows based on the three XML fields and with these conditions. CCPuser XSLT 3 October 22nd, 2010 09:11 AM
select the group of rows based on the three XML fields and with conditions. CCPuser BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 1 October 22nd, 2010 09:06 AM





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