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 31st, 2009, 10:54 AM
Registered User
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed

Hi I have an xml file that looks like below:

<JobAD Source="Source Name" JobAdID="MT411722" Action="insert/modify" ValidFrom="19/03/2009" ValidTo="20/03/2009" CompanyName="Mrs Jones" CompanyID="204605" CompanyAddress1="The address in here" CompanyAddressPostNumber="LL53 3UT" CompanyAddressPostName="Minera" JobAdPlainText="Job Text in here."/>

<Miles33Jobs>

<Job>
<OrganisationName>Jimmys Shoes</OrganisationName>
<ContactName>Mrs Jones</ContactName>
<ContactTel>01978 777777</ContactTel>
<ContactMobile/>
<ContactFax/>
<ContactEmail>Jimmy?jimmys.com</ContactEmail>
<WebLink/>
<JobTitle>Few Hours to spare?</JobTitle>
<NoOfPosition>1</NoOfPosition>
<Location>WREXHAM</Location>
<JobType>Contract</JobType>
<WorkingHours>Part Time</WorkingHours>
<Salary>£Not available</Salary>
<SalaryFrequency>Per day</SalaryFrequency>

<Sectors>
<Name>Retail</Name>
</Sectors>
<JobRef>MT52861</JobRef>
<ClosingDate>2007-06-27T10:21:41.0324367+01:00</ClosingDate>
<HasClosingDate>false</HasClosingDate>

<Description>
ORIFLAME COSMETICS"Few hours to spare? Looking for extra income? No outlay, excellent commission, free products and incentives. Tel: 01978 757246
</Description>
</Job>
</Miles33Jobs>

++++++++++++++++++++++++++++++++++++++++++++++++++ ++
Some of theitems within the Miles33Jobs xml I need to change to be attributes within the JobAd node (the first node)

Is there a way of doing this?
I need this ASAP and will be hugely grateful for any help.
 
Old March 31st, 2009, 11:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Almost certainly possible but firstly your XML is not well-formed, should jobAD enclose the Miles33Jobs element?

Secondly can you give an example of one or two of the elements you wish to change and how they should appear in the new format?
__________________
Joe
http://joe.fawcett.name/

Last edited by joefawcett; March 31st, 2009 at 11:23 AM..
 
Old April 1st, 2009, 07:13 AM
Registered User
 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Updated

Hi I have managed to get the xml better formed now and it reads:

<JobAD Source="BlahBlah" JobAdID="412252" Action="insert/modify" ValidFrom="01/04/2009" ValidTo="01/04/2009" CompanyName="Nursing Home" CompanyID="329334" CompanyAddress1="The Home" CompanyAddressPostNumber="LT32 4LU" CompanyAddressPostName="Denbigh" JobAdPlainText="
PART TIME / FULL TIME COOK
To work in a friendly & caring environment.
Call Sue on 02232 2727362
">

<Miles33>

<Miles33Jobs>

<Job>
<OrganisationName>Nursing Home</OrganisationName>
<ContactName>Sue </ContactName>
<ContactTel>02232 2727362</ContactTel>
<ContactMobile/>
<ContactFax/>
<ContactEmail/>
<WebLink/>
<JobTitle>Care Assistants</JobTitle>
<NoOfPosition>1</NoOfPosition>
<Location>DENBIGH,Denbighshire</Location>
<JobType>Permanent</JobType>
<WorkingHours>Full Time</WorkingHours>
<Salary>£Not available</Salary>
<SalaryFrequency>annually</SalaryFrequency>

<Sectors>
<Name>Social Care</Name>
</Sectors>
<JobRef/>
<ClosingDate>2007-07-24T16:36:38.2950140+01:00</ClosingDate>
<HasClosingDate>false</HasClosingDate>
<Description>Care Assistants</Description>
</Job>
</Miles33Jobs>
</Miles33>
</JobAD>


What I want to do is take the details held in the various nodes of the Miles33 bit and add them as attributes in the opening JobAd node

ie
take
<Sectors>
<Name>Social Care</Name>
</Sectors>

and put the name into an attribute in the JobAd node
<JobAd SectorName="Social Care" ....... etc etc

Thanks for any help you can offer.
 
Old April 1st, 2009, 08:03 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Next time please make sure you post the source code of your XML document and not what a browser like IE or Firefox might render as that way you do not post well-formed samples.
So assuming your XML is actually as follows:
Code:
<JobAD Source="BlahBlah" JobAdID="412252" Action="insert/modify" ValidFrom="01/04/2009" ValidTo="01/04/2009" CompanyName="Nursing Home" CompanyID="329334" CompanyAddress1="The Home" CompanyAddressPostNumber="LT32 4LU" CompanyAddressPostName="Denbigh" JobAdPlainText="
PART TIME / FULL TIME COOK
To work in a friendly &amp; caring environment.
Call Sue on 02232 2727362
">

<Miles33>

<Miles33Jobs>

<Job>
<OrganisationName>Nursing Home</OrganisationName>
<ContactName>Sue </ContactName>
<ContactTel>02232 2727362</ContactTel>
<ContactMobile/>
<ContactFax/>
<ContactEmail/>
<WebLink/>
<JobTitle>Care Assistants</JobTitle>
<NoOfPosition>1</NoOfPosition>
<Location>DENBIGH,Denbighshire</Location>
<JobType>Permanent</JobType>
<WorkingHours>Full Time</WorkingHours>
<Salary>£Not available</Salary>
<SalaryFrequency>annually</SalaryFrequency>

<Sectors>
<Name>Social Care</Name>
</Sectors>
<JobRef/>
<ClosingDate>2007-07-24T16:36:38.2950140+01:00</ClosingDate>
<HasClosingDate>false</HasClosingDate>
<Description>Care Assistants</Description>
</Job>
</Miles33Jobs>
</Miles33>
</JobAD>
then the following XSLT 2.0 stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="JobAD">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="Miles33/Miles33Jobs/Job/*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Miles33/Miles33Jobs/Job/*[not(*)]">
    <xsl:attribute name="{name()}" select="."/>
  </xsl:template>
  
  <xsl:template match="Miles33/Miles33Jobs/Job/*[*]">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  
  <xsl:template match="Miles33/Miles33Jobs/Job/*/*">
    <xsl:attribute name="{concat(name(..), name())}" select="."/>
  </xsl:template>

</xsl:stylesheet>
when run with Saxon 9.1 produces the following result:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<JobAD Source="BlahBlah" JobAdID="412252" Action="insert/modify"
       ValidFrom="01/04/2009"
       ValidTo="01/04/2009"
       CompanyName="Nursing Home"
       CompanyID="329334"
       CompanyAddress1="The Home"
       CompanyAddressPostNumber="LT32 4LU"
       CompanyAddressPostName="Denbigh"
       JobAdPlainText=" PART TIME / FULL TIME COOK To work in a friendly &amp; caring environment. Call Sue on 02232 2727362 "
       OrganisationName="Nursing Home"
       ContactName="Sue "
       ContactTel="02232 2727362"
       ContactMobile=""
       ContactFax=""
       ContactEmail=""
       WebLink=""
       JobTitle="Care Assistants"
       NoOfPosition="1"
       Location="DENBIGH,Denbighshire"
       JobType="Permanent"
       WorkingHours="Full Time"
       Salary="£Not available"
       SalaryFrequency="annually"
       SectorsName="Social Care"
       JobRef=""
       ClosingDate="2007-07-24T16:36:38.2950140+01:00"
       HasClosingDate="false"
       Description="Care Assistants"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed sumit_00 ASP.NET 2.0 Basics 2 August 20th, 2007 08:59 AM
Help needed fastmike Pro PHP 1 February 15th, 2007 03:25 AM
Help Needed cyberabhi Classic ASP Professional 1 March 21st, 2006 06:33 PM
help needed here arzu C# 1 February 17th, 2005 09:10 AM
Help is needed! satts Access 3 February 14th, 2005 03:04 PM





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