Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Help needed to write an XSL - XML to XML transformation

Question posted by: sarah12 (Newbie) on June 30th, 2008 02:28 PM
Hi,

I want to change :

<?xml version="1.0" ?>
<nplbiblio >
<document status="U" creadate="19990410">
<xp>000000001</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<ord>19880901</ord>
<titles>
<title>MORPHOLOGY OF NI/GAAS SURFACES</title>
</titles>
<author>SINHA M P, MAHAPATRA S</author>
<supplier>MIGRATION</supplier>
<attribs>
<attrib name="nr_hostdoc">5</attrib>
<attrib name="pg_from_hostdoc">19-21</attrib>
<attrib name="vol_hostdoc">19</attrib>

</attribs>
<publication>
<irn>
<issn>0026-2692</issn>
</irn>
</document>

<document status="U" creadate="19990410">
<xp>000000002</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<ord>19880901</ord>
<titles>
<title>GRAFT COPOLYMERIZATION OF METHYL METHACRYLATE ON POLY (VINYL CHLORIDE).</title>
</titles>
<author>PREMAMOY GHOSH, ARABINDA SHEKHAR BHATTACHARYYA, SOMNATH MAITRA.</author>
<supplier>MIGRATION</supplier>
<attribs>
<attrib name="pg_from_hostdoc">135 - 148</attrib>
<attrib name="vol_hostdoc">162</attrib>

</attribs>
<publication>
<irn>
<issn>0003-3146</issn>
</irn>
</document>

</nplbiblio>


into :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nplbiblio SYSTEM "npl-entities.dtd">
<nplbiblio xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:npl="http://www.epo.org/npl" xsi:noNamespaceSchemaLocation="npldoc v2.0.xsd" rundate="20080123">
<document status="n" creadate="20080123">
<xp>000000001</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<xp>000000001</xp>
<author>SINHA M P, MAHAPATRA S</author>
<supplier>MIGRATION</supplier>
<title>MORPHOLOGY OF NI/GAAS SURFACES</title>
<isbn/>
<issn>0026-2692</issn>
<first_page>19-21</first_page>
<issue>5</issue>
<volume>19</volume>

</document>
<document status="n" creadate="20080123">
<xp>000000002</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<xp>000000002</xp>
<author>PREMAMOY GHOSH, ARABINDA SHEKHAR BHATTACHARYYA, SOMNATH MAITRA.</author>
<supplier>MIGRATION</supplier>
<title>GRAFT COPOLYMERIZATION OF METHYL METHACRYLATE ON POLY (VINYL CHLORIDE).</title>
<isbn/>
<issn>0003-3146</issn>
<first_page>135-148</first_page>
<volume>162</volume>

</document>
</nplbiblio>


I have written this XLS which works at 95%:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:npl="http://www.epo.org/npl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" doctype-system="npl-entities.dtd"/>

<xsl:key name = "kkk" match = "attribs/attrib" use = "@name" />
<xsl:template match="/">

<nplbiblio>
<xsl:attribute name="xsi:noNamespaceSchemaLocation">npldoc v2.0.xsd</xsl:attribute>
<xsl:attribute name="rundate">20080123</xsl:attribute>

<xsl:for-each select="nplbiblio/document">

<document status="n">
<xsl:attribute name="creadate">20080123</xsl:attribute>
<xsl:copy-of select="xp"/>
<xsl:copy-of select="doctype"/>
<xsl:copy-of select="prdate"/>
<xsl:copy-of select="xp"/>
<xsl:copy-of select="author"/>
<xsl:copy-of select="supplier"/>

<title>
<xsl:value-of select="titles/title"/>
</title>

<isbn>
<xsl:value-of select="publication/irn/isbn"/>
</isbn>

<issn>
<xsl:value-of select="publication/irn/issn"/>
</issn>

<first_page>
<xsl:value-of select = "key('kkk','pg_from_hostdoc')" />
</first_page>

<issue>
<xsl:value-of select = "key('kkk','nr_hostdoc')" />
</issue>

<volume>
<xsl:value-of select = "key('kkk','vol_hostdoc')" />
</volume>

</document>
</xsl:for-each>
</nplbiblio>
</xsl:template>
</xsl:stylesheet>


The only problem with the output is that the values of 'pg_from_hosdoc' , 'nr_hostdoc' and 'vol_hostdoc' are taken from the first record and repeats on the next record :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nplbiblio SYSTEM "npl-entities.dtd">
<nplbiblio xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:npl="http://www.epo.org/npl" xsi:noNamespaceSchemaLocation="npldoc v2.0.xsd" rundate="20080123">
<document status="n" creadate="20080123">
<xp>000000001</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<xp>000000001</xp>
<author>SINHA M P, MAHAPATRA S</author>
<supplier>MIGRATION</supplier>
<title>MORPHOLOGY OF NI/GAAS SURFACES</title>
<isbn/>
<issn>0026-2692</issn>
<first_page>19-21</first_page>
<issue>5</issue>
<volume>19</volume>

</document>
<document status="n" creadate="20080123">
<xp>000000002</xp>
<doctype>JOURN-ART</doctype>
<prdate>19880901</prdate>
<xp>000000002</xp>
<author>PREMAMOY GHOSH, ARABINDA SHEKHAR BHATTACHARYYA, SOMNATH MAITRA.</author>
<supplier>MIGRATION</supplier>
<title>GRAFT COPOLYMERIZATION OF METHYL METHACRYLATE ON POLY (VINYL CHLORIDE).</title>
<isbn/>
<issn>0003-3146</issn>
<first_page>19-21</first_page>
<issue>5</issue>

<volume>19</volume>
</document>
</nplbiblio>
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
jkmyoung's Avatar
jkmyoung
Moderator
765 Posts
July 2nd, 2008
06:48 PM
#2

Re: Help needed to write an XSL - XML to XML transformation
2 Possible ways:
1. Change your key such that it takes something unique form the document element containing the attribs.

eg:
<xsl:key name = "kkk" match = "attribs/attrib" use = "concat(..\preceding-sibling::xp,'-',@name)" />

Other work required when using the keys.
-----

2. Change the xpaths eg:
<first_page>
<xsl:value-of select = "attribs/attrib[@name='pg_from_hostdoc']" />
</first_page>

I think the 2nd way is much easier.

Reply
sarah12's Avatar
sarah12
Newbie
8 Posts
July 8th, 2008
10:15 AM
#3

Re: Help needed to write an XSL - XML to XML transformation
Hi ,

Thanks for your suggestion .

After some long trial and errors, I finally found the right XSL code :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:npl="http://www.epo.org/npl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" doctype-system="npl-entities.dtd"/>

<xsl:template match = "/" >

<nplbiblio>
<xsl:attribute name="xsi:noNamespaceSchemaLocation">npldoc v2.0.xsd</xsl:attribute>
<xsl:attribute name="rundate">20080123</xsl:attribute>

<xsl:for-each select="nplbiblio/document">


<document status="n">
<xsl:attribute name="creadate">20080123</xsl:attribute>
<xsl:copy-of select="xp"/>
<xsl:copy-of select="doctype"/>
<xsl:copy-of select="prdate"/>
<xsl:copy-of select="xp"/>
<xsl:copy-of select="author"/>
<xsl:copy-of select="supplier"/>

<title>
<xsl:value-of select="titles/title"/>
</title>

<isbn>
<xsl:value-of select="publication/irn/isbn"/>
</isbn>

<issn>
<xsl:value-of select="publication/irn/issn"/>
</issn>


<first_page>
<xsl:value-of select="attribs/attrib[@name='pg_from_hostdoc']"/>
</first_page>

<issue>
<xsl:value-of select="attribs/attrib[@name='nr_hostdoc']"/>
</issue>

<volume>
<xsl:value-of select="attribs/attrib[@name='vol_hostdoc']"/>
</volume>


</document>
</xsl:for-each>
</nplbiblio>
</xsl:template>

</xsl:stylesheet>

It works perfect !!

Thanks again

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,081 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top XML Forum Contributors