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 April 29th, 2010, 02:12 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing XML file

I am new to XML file, I am writing an XSLT code for parsing XML file, your replies are highly appreciated
Input File:

PHP Code:
<?xml version="1.0" encoding="UTF-8"?>
<Report TotalPage="3" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="7229801100.xsd">
  <Page id="1">
    <ReportHeaderPanel11>
      <Label70>Active users </Label70>
      <Label71>Security</Label71>
    </ReportHeaderPanel11>
    <ReportHeaderPanel10>
      <reportName>ActiveUserCountAudit</reportName>
      <localizedReportNameLabelHeader>Report Name:</localizedReportNameLabelHeader>
    </ReportHeaderPanel10>
    <ReportHeaderPanel9>
      <createdDate>2010-04-28  11:11:21</createdDate>
      <localizedReportGeneratedLabelHeader>Report Generated:</localizedReportGeneratedLabelHeader>
    </ReportHeaderPanel9>
    <ReportHeaderPanel8>
      <timeZoneOffSet>UTC -07:00</timeZoneOffSet>
      <localizedTimeZoneLabelHeader>Time Zone:</localizedTimeZoneLabelHeader>
    </ReportHeaderPanel8>
    <ReportHeaderPanel7>
      <creator>user111</creator>
      <localizedUserLabelHeader>User:</localizedUserLabelHeader>
    </ReportHeaderPanel7>
    <ReportHeaderPanel6>
      <reportDescription>Report on active users</reportDescription>
      <localizedDesciptionLabelHeader>Description:</localizedDesciptionLabelHeader>
    </ReportHeaderPanel6>
    <ReportHeaderPanel5>
      <Label64>Number of Active Users:</Label64>
      <forSessionUsers_distinctUsers34>2</forSessionUsers_distinctUsers34>
    </ReportHeaderPanel5>
    <ReportHeaderPanel4>
      <Label63>Total Active Sessions:</Label63>
      <TotalRecords2>6</TotalRecords2>
    </ReportHeaderPanel4>
    <ReportHeaderPanel3>
      <Label61>Roles:</Label61>
      <forSessionUsers_roleIds2>Super User</forSessionUsers_roleIds2>
    </ReportHeaderPanel3>

  </Page>
  <Page id="2">
    <GroupPanel></GroupPanel>
    <GroupHeaderPanel6>
      <GroupName>u1</GroupName>
    </GroupHeaderPanel6>
    <GroupHeaderPanel5>
      <Label82>User First Name:</Label82>
      <FirstName>N/A</FirstName>
    </GroupHeaderPanel5>
    <GroupHeaderPanel4>
      <Label81>User Last Name:</Label81>
      <LastName>N/A</LastName>
    </GroupHeaderPanel4>
    <GroupHeaderPanel3>
      <Label80>Roles:</Label80>
      <Roles>Super User</Roles>
    </GroupHeaderPanel3>
    <GroupHeaderPanel2>
      <Label79>Number of Active Sessions:</Label79>
      <countSessions>4</countSessions>
    </GroupHeaderPanel2>
    <GroupHeaderPanel1>
      <Label72>Creation</Label72>
      <Label73>Last Update</Label73>
      <Label74>Origin</Label74>
      <Label75>Client Type</Label75>
      <Label76>Cached Objects</Label76>
      <Label77>Trusted</Label77>
      <Label78>Id</Label78>
    </GroupHeaderPanel1>
    <DetailPanel>
      <CreationDate>2010-04-28  09:05:03</CreationDate>
      <Id>F339E88A</Id>
      <CacheSize>3</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:20</LastUpdateDate>
      <Origin>l3ape</Origin>
      <ClientType>DES</ClientType>
      <Trusted>False</Trusted>
    </DetailPanel>
    <DetailPanel>
      <CreationDate>2010-04-28  11:11:20</CreationDate>
      <Id>7B2EADD3</Id>
      <CacheSize>1</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:23</LastUpdateDate>
      <Origin>db.net</Origin>
      <ClientType>SERVER</ClientType>
      <Trusted>True</Trusted>
    </DetailPanel>
    <DetailPanel>
      <CreationDate>2010-04-28  08:51:17</CreationDate>
      <Id>05F5BE63</Id>
      <CacheSize>3</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:04</LastUpdateDate>
      <Origin>l3ape</Origin>
      <ClientType>DIR</ClientType>
      <Trusted>False</Trusted>
    </DetailPanel>
    <DetailPanel>
      <CreationDate>2010-04-28  11:09:55</CreationDate>
      <Id>69D93B70</Id>
      <CacheSize>5</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:21</LastUpdateDate>
      <Origin>l3ape5</Origin>
      <ClientType>WEB</ClientType>
      <Trusted>False</Trusted>
    </DetailPanel>
    <GroupPanel></GroupPanel>
    <GroupPanel></GroupPanel>
    <GroupHeaderPanel6>
      <GroupName>u2</GroupName>
    </GroupHeaderPanel6>
    <GroupHeaderPanel5>
      <Label82>User First Name:</Label82>
      <FirstName>N/A</FirstName>
    </GroupHeaderPanel5>
    <GroupHeaderPanel4>
      <Label81>User Last Name:</Label81>
      <LastName>N/A</LastName>
    </GroupHeaderPanel4>
    <GroupHeaderPanel3>
      <Label80>Roles:</Label80>
      <Roles>Super User</Roles>
    </GroupHeaderPanel3>
    <GroupHeaderPanel2>
      <Label79>Number of Active Sessions:</Label79>
      <countSessions>2</countSessions>
    </GroupHeaderPanel2>
    <GroupHeaderPanel1>
      <Label72>Creation</Label72>
      <Label73>Last Update</Label73>
      <Label74>Origin</Label74>
      <Label75>Client Type</Label75>
      <Label76>Cached Objects</Label76>
      <Label77>Trusted</Label77>
      <Label78>Id</Label78>
    </GroupHeaderPanel1>

  </Page>
  <Page id="3">
    <GroupPanel></GroupPanel>
    <DetailPanel>
      <CreationDate>2010-04-28  10:59:38</CreationDate>
      <Id>DA03FB5B</Id>
      <CacheSize>3</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:17</LastUpdateDate>
      <Origin>l3ac</Origin>
      <ClientType>DES</ClientType>
      <Trusted>False</Trusted>
    </DetailPanel>
    <DetailPanel>
      <CreationDate>2010-04-28  11:01:19</CreationDate>
      <Id>325CFC97</Id>
      <CacheSize>3</CacheSize>
      <LastUpdateDate>2010-04-28  11:11:01</LastUpdateDate>
      <Origin>l3ac</Origin>
      <ClientType>DIR</ClientType>
      <Trusted>False</Trusted>
    </DetailPanel>
    <GroupPanel></GroupPanel>

  </Page>
</Report>
Output:

PHP Code:
<?xml version="1.0" encoding="UTF-8"?>
<Report>
        <NAME Name="u1" >
        <DETAILPanel CreationDate="2010-04-28  09:05:03" />
        <DETAILPanel CreationDate="2010-04-28  11:11:20" />
        <DETAILPanel CreationDate="2010-04-28  08:51:17" />
        <DETAILPanel CreationDate="2010-04-28  11:09:55" />
</NAME
<NAME Name="u2" >
        <DETAILPanel CreationDate="2010-04-28  10:59:38" />
        <DETAILPanel CreationDate="2010-04-28  11:01:19" />
   </NAME
</Report>
I tried the following XSLT code, but I am going nowhere with it

PHP Code:
Deleted as it is confusing 
Please give me some hints.

Last edited by mike123; April 29th, 2010 at 05:30 PM..
 
Old April 29th, 2010, 05:17 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm afraid its unclear from your example what the relation between Page, GroupHeaderPanel6 and DetailPanel is.

You are outputting Page elements, but in your required output there are no page elements. And the second set of DetailPanel elements is a different Page element to what you seem to be implying is its 'related' GroupHeaderPanel6 element.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 29th, 2010, 05:36 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply... I have removed the XSLT code (that is confusing).
Hopefully the following is little bit clear

For every occurrence of GroupName [in the example it is u1 and u2]
then get the different elements of DetailPanel under the u1 and u2 group.

So for user u1 it has 4 creation date and u2 it has 2 creation date. [Similarly I need to combine the Id, LastUpdateDate, Origin for each user group]

by the way the input XML file is generated by an application, it can be generated in printable format so it displays the page1, page2 and page3[it can be n number pf pages]I hope this was clear
 
Old April 29th, 2010, 07:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Editing posts after someone replies to them is even more confusing - it makes it impossible to pick up the thread.

Also a bit of terminology: your problem is not with parsing the XML file, but with transforming it. The XML parser provides the input to the transformation; the XSLT code is concerned only with the transformation, not with the parsing.

The <Page> hierarchy in your input seems to cut across the logical structure, and serves only to confuse matters. I would do a first phase of transformation that gets rid of it. If you choose, you could get rid of a lot of the other unwanted detail at the same time. Part of the unwanted detail is the numeric part of the element name - this seems to convey no useful information and is only getting in the way. If you have any control over the design of this XML, it could be vastly improved - whoever designed it really doesn't understand XML at all well.

You then have a flat XML structure in which the division into groups is not explicit in the XML hierarchy. Adding hierarchic structure to flat data is best tackled with the XSLT 2.0 <xsl:for-each-group> instruction: in this case, something like <xsl:for-each-group select="*" group-starting-with="*[GroupName]"> to start a new group when you find a GroupHeaderPanel with a GroupName child.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 29th, 2010, 08:07 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. I don't have the control over the XML design, but as you suggested I can remove some of the tags[like <Page>].
I am using XSLT 1.0, so couldn't test the code you suggested,

I was trying something like this, could you help me in correcting the code [I am not getting how to use the for-each and copy-of ....]

PHP Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <DetailPanel>                
            <xsl:attribute name="Name">
                <xsl:copy-of select="//Report/Page/GroupHeaderPanel6/GroupName/text()"></xsl:copy-of>
            </xsl:attribute>                  
            
      
    <xsl:attribute name="CreationDate">
            <xsl:copy-of select="//Report/Page/DetailPanel/CreationDate/text()"></xsl:copy-of>            
            </xsl:attribute>     

        </DetailPanel>
    </xsl:template>


</xsl:stylesheet>
 
Old April 30th, 2010, 08:04 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample XSLT 1.0 stylesheet that produces the output you described:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:key name="k1" match="DetailPanel" use="generate-id(preceding::GroupHeaderPanel6[1])"/>
  
  <xsl:template match="/">
    <Report>
      <xsl:apply-templates select="//GroupHeaderPanel6"/>
    </Report>
  </xsl:template>
  
  <xsl:template match="GroupHeaderPanel6">
    <NAME Name="{GroupName}">
      <xsl:apply-templates select="key('k1', generate-id())"/>
    </NAME>
  </xsl:template>
  
  <xsl:template match="DetailPanel">
    <DETAILPanel CreationDate="{CreationDate}"/>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 30th, 2010, 02:23 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the code, I am getting all the dates under first user.

Current output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Report>
    <NAME Name="u1">
        <DETAILPanel CreationDate="2010-04-28  09:05:03" />
        <DETAILPanel CreationDate="2010-04-28  11:11:20" />
        <DETAILPanel CreationDate="2010-04-28  08:51:17" />
        <DETAILPanel CreationDate="2010-04-28  11:09:55" />
        <DETAILPanel CreationDate="2010-04-28  10:59:38" />
        <DETAILPanel CreationDate="2010-04-28  11:01:19" />
    </NAME>
    <NAME Name="u2" />
</Report>
 
Old April 30th, 2010, 03:43 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well then you either aren't running Martin's code as written, or your input XML has changed, because I get the exact output you wanted using his XSLT against your XML.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 30th, 2010, 06:13 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for testing the code.
I was using Eclipse tool there I am get the output as I shown and when I use IBM Infosphere tool (thats where I would be using the XSLT code) I get the correct output[I need to check the exact version ..]

One final question.

If I want to generate an output file with Roles as a part of the output do I have to declare a new key name?
<Report>
<NAME Name="u1" >
<Role Role="Super User"/>
<DetailPanel ......
.....
</Report>

I appreciate all your help.
 
Old May 1st, 2010, 04:31 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It depends. If the rule is that there is always a GroupHeaderPanel3 containing the role after the GroupHeaderPanel6 then you could do the following:

Code:
<xsl:template match="GroupHeaderPanel6">
    <NAME Name="{GroupName}">
      <Role Role="{following-sibling::GroupHeaderPanel3[1]/Roles}"/>
      <xsl:apply-templates select="key('k1', generate-id())"/>
    </NAME>
  </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML::Simple parsing xml geek.shrek Perl 1 August 19th, 2009 10:32 PM
parsing flat xml file using asp.net 2.0 mikecarmody XML 5 April 28th, 2008 02:55 AM
Help Parsing XML File Sojan80 XML 7 January 7th, 2008 06:09 PM
Help with parsing XML file (substitutions) yanzickp XSLT 1 July 25th, 2007 04:32 PM
Parsing xml file ?? bikash XML 3 September 5th, 2003 06:23 AM





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