Jump to content
adsingh1

how to add root tag using xslt when xml returning more than one row

Recommended Posts

hi all,

i m very new to xslt. i need a help.i want add a root tag in my xml using xslt where xml file returning more than one rows.

my xslt is demo.xsl

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/*:ExportXML"> <sources> <xsl:for-each-group select="*:record" group-by="*:field[@name=ContestNumber]"> <sourcetype> <sourcelist> </sourcelist> </sourcetype> </xsl:for-each-group> </sources> </xsl:template> <xsl:template match="*:field"> <xsl:element name="{lower-case(@name)}"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="*:record"> <JobPositionPostings> <JobPositionPosting> <xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/> <JobDisplayOptions> <xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/> </JobDisplayOptions> </JobPositionPosting> </JobPositionPostings> </xsl:template> <xsl:template match="*:field[@name=TypeName]"/> <xsl:template match="*:field[@name=TypeName]" mode="title"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>

 

my output is cming like this requisition.xml

 

<?xml version="1.0" encoding="UTF-8"?><JobPositionPostings> <JobPositionPosting> <jobpositionpostingid>1300000F</jobpositionpostingid> <HiringOrg> <Industry> <summarytext>Project Manager</summarytext> </Industry> <Contact> <PersonName/> </Contact> </HiringOrg> <JobDisplayOptions/> <JobPositionInformation> <JobClassifications> <PrimaryJobCategory/> </JobClassifications> <JobPositionDescription/> <JobPositionLocation/> <CompensationDescription> <Pay/> </CompensationDescription> <JobPositionRequirements> <JobExperience/> <JobQualifications/> </JobPositionRequirements> </JobPositionInformation> <HowToApply> <ApplicationMethods> <ByEmail/> <ByWeb/> </ApplicationMethods> </HowToApply> </JobPositionPosting></JobPositionPostings><JobPositionPostings> <JobPositionPosting> <jobpositionpostingid>1300000H</jobpositionpostingid> <HiringOrg> <Industry> <summarytext>Project Manager</summarytext> </Industry> <Contact> <PersonName/> </Contact> </HiringOrg> <JobDisplayOptions/> <JobPositionInformation> <JobClassifications> <PrimaryJobCategory/> </JobClassifications> <JobPositionDescription/> <JobPositionLocation/> <CompensationDescription> <Pay/> </CompensationDescription> <JobPositionRequirements> <JobExperience/> <JobQualifications/> </JobPositionRequirements> </JobPositionInformation> <HowToApply> <ApplicationMethods> <ByEmail/> <ByWeb/> </ApplicationMethods> </HowToApply> </JobPositionPosting></JobPositionPostings><JobPositionPostings> <JobPositionPosting> <jobpositionpostingid>1300000T</jobpositionpostingid> <HiringOrg> <Industry> <summarytext>Project Manager</summarytext> </Industry> <Contact> <PersonName/> </Contact> </HiringOrg> <JobDisplayOptions/> <JobPositionInformation> <JobClassifications> <PrimaryJobCategory/> </JobClassifications> <JobPositionDescription/> <JobPositionLocation/> <CompensationDescription> <Pay/> </CompensationDescription> <JobPositionRequirements> <JobExperience/> <JobQualifications/> </JobPositionRequirements> </JobPositionInformation> <HowToApply> <ApplicationMethods> <ByEmail/> <ByWeb/> </ApplicationMethods> </HowToApply> </JobPositionPosting></JobPositionPostings>

 

i want that Root tag <JobPositionPostings> just as root tag(open and close one time) but its

repeating for every row please suggest me any solution for this.thanks in advance.

 

Share this post


Link to post
Share on other sites

Can you show us a sample of the XML input you want to transform? That way it is easier to make suggestions on how to transform it.

Currently with

 

<xsl:template match="*:record">    <JobPositionPostings>    <JobPositionPosting>

you are mapping each "*:record" element to a "JobPositionPostings" element. If that is not your objective then you need to change the code so that the "JobPositionPostings" element is only created once. Usually you do that by creating it in a template matching a parent or ancestor of "*:record" and doing apply-templates. So assuming the parent element is named "*:record-list" you would use

 

<xsl:template match="*:record-list">  <JobPositionPostings>    <xsl:apply-templates/>  </JobPositionPostings></xsl:template>

Share this post


Link to post
Share on other sites

hi martin thanks for ur reply.

my input xml message is input.xml

 

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getDocumentByKeyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07"><Document xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07"><Attributes><Attribute name="duration">0:00:00.130</Attribute><Attribute name="count">7</Attribute><Attribute name="entity">Requisition</Attribute><Attribute name="mode">XML</Attribute><Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute></Attributes><Content>

<record>
<field name="ContestNumber">1300000F</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">1300000H</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">1300000T</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">13000018</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">000123</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">1300000R</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
<record>
<field name="ContestNumber">13000016</field>
<field name="ManagerRequisitionTitle">Project Manager</field>
</record>
</ExportXML></Content></Document></ns1:getDocumentByKeyResponse></soapenv:Body></soapenv:Envelope>
my expexted output is output.xml
<?xml version="1.0" encoding="UTF-8"?>
<JobPositionPostings>
<JobPositionPosting>
<contestnumber>1300000F</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>1300000H</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>1300000T</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>13000018</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>000123</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>1300000R</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
<JobPositionPosting>
<contestnumber>13000016</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
</JobPositionPostings>
but i am not able to get this in my output </JobPositionPostings> tag is not coming as parent tag
my xsl file is myxsl.xsl
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:field">
<xsl:element name="{lower-case(@name)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:record">
<JobPositionPostings>
<JobPositionPosting>
<xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/>
<JobDisplayOptions>
<xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/>
</JobDisplayOptions>
</JobPositionPosting>
</JobPositionPostings>
</xsl:template>
<xsl:template match="*:field[@name=TypeName]"/>
<xsl:template match="*:field[@name=TypeName]" mode="title">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
please suggest me any solution.thanku in advance
Edited by adsingh1

Share this post


Link to post
Share on other sites

Write a template

 

<xsl:template match="*:ExportXML">  <JobPositionPostings>    <xsl:apply-templates/>  </JobPositionPostings></xsl:template>

and remove the element from the other template, that is change

 

<xsl:template match="*:record">    <JobPositionPostings>    <JobPositionPosting>      <xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/>           <JobDisplayOptions>       <xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/>      </JobDisplayOptions>        </JobPositionPosting>       </JobPositionPostings>      </xsl:template>

to

 

<xsl:template match="*:record">       <JobPositionPosting>      <xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/>           <JobDisplayOptions>       <xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/>      </JobDisplayOptions>        </JobPositionPosting>       </xsl:template>
  • Like 1

Share this post


Link to post
Share on other sites

hi martin thanku for reply.

my xslt is requsition.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="*:field">
<xsl:element name="{lower-case(@name)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*:ExportXML">
<JobPositionPostings>
<xsl:apply-templates/>
</JobPositionPostings>
</xsl:template>
<xsl:template match="*:record">
<JobPositionPosting>
<xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/>
<JobDisplayOptions>
<xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/>
</JobDisplayOptions>
</JobPositionPosting>
</xsl:template>
<xsl:template match="*:field[@name=TypeName]"/>
<xsl:template match="*:field[@name=TypeName]" mode="title">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

 

i tried whatever u suggested but still i am getting output like this.now that <JobPositionPostings> tag is not cming in output.

is there any problem when <ExportXml > tag containg namespace .because it is not behaving like root tag.so only i think <JobPositionPostings>

tag is not present in output.

requisition.xml

 

<JobPositionPosting>
<contestnumber>1300000F</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>1300000H</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>1300000T</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>13000018</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>000123</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>1300000R</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting><JobPositionPosting>
<contestnumber>13000016</contestnumber>
<JobDisplayOptions>
<managerrequisitiontitle>Project Manager</managerrequisitiontitle>
</JobDisplayOptions>
</JobPositionPosting>
even i tried to generate separate file for every <record> tag containing data.in that i can get data in <JobPositionPostings> . using this xslt
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*[starts-with(name(), 'record')]">
<xsl:result-document href="{$pDest}section{position()}.xml">
<JobPositionPostings>
<JobPositionPosting>
<xsl:apply-templates select="*:field[starts-with(@name,'ContestNumber')]"/>
<JobDisplayOptions>
<xsl:apply-templates select="*:field[starts-with(@name,'ManagerRequisitionTitle')]"/>
</JobDisplayOptions>
</JobPositionPosting>
</JobPositionPostings>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
but this also generating only last <record> tag detail.
<JobPositionPostings>
<JobPositionPosting>13000016<JobDisplayOptions>Project Manager</JobDisplayOptions>
</JobPositionPosting>
</JobPositionPostings>
please suggest me some solution either get every record tag detail in seprate file or in one file containing <JobPositionPostings> tag.thanks in advance
Edited by adsingh1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...