Reputation: 1021
I am rather new when it comes to doing xslt transforms. I am trying to convert the following xml code (using xslt 1.0):
<generic_etd>
<dc.contributor>NSERC</dc.contributor>
<dc.creator>gradstudent</dc.creator>
<dc.date>2013-05-07</dc.date>
<dc.format>30 pages</dc.format>
<dc.format>545709 bytes</dc.format>
<thesis.degree.name>Theses (M.Eng.)</thesis.degree.name>
<thesis.degree.level>masters</thesis.degree.level>
<thesis.degree.discipline>Dept. of Mechanical Engineering<thesis.degree.discipline>
<thesis.degree.grantor>Concordia University</thesis.degree.grantor>
</generic_etd>
into the following format:
<etd_ms:thesis>
<etd_ms:contributor>NSERC</etd_ms:contributor>
<etd_ms:creator>gradstudent</etd_ms:creator>
<etd_ms:date>2013-05-07</etd_ms:date>
<etd_ms:format>30 pages</etd_ms:format>
<etd_ms:format>545709 bytes</etd_ms:format>
<etd_ms:degree>
<etd_ms:name>Theses (M.Eng.)</etd_ms:name>
<etd_ms:level>masters</etd_ms:level>
<etd_ms:discipline>Dept. of Mechanical Engineering</etd_ms:discipline>
<etd_ms:grantor>Concordia University</etd_ms:grantor>
</etd_ms:degree>
</etd_ms:thesis>
The problem I am having is two fold. I can create everything except the degree portion of xml just fine. However when I try to add the degree portion with the appropriate nesting it is not working for me. I have looked on line for some clues but the xsl:key call doesn't seem to be the correct thing to use. Any ideas how to accomplish this? I suspect it must be relatively straight forward if you know what you are doing (unlike me).
Thanks.
Upvotes: 0
Views: 61
Reputation: 3138
This generated XSLT on version 1.0 is able to get your desired output:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:etd_ms="dsfdsfsdf">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:copy-of select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="name()='generic_etd'">
<etd_ms:thesis>
<xsl:apply-templates/>
</etd_ms:thesis>
</xsl:when>
<xsl:when test="contains(name(),'thesis.degree.')"/>
<xsl:otherwise>
<xsl:element name="{concat('etd_ms:',substring-after(name(),'.'))}">
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="new" match="*[contains(name(),'thesis.degree.')][1]">
<etd_ms:degree>
<xsl:for-each select="//*[contains(name(),'thesis.degree.')]">
<xsl:element name="{concat('etd_ms:',substring-after(name(),'thesis.degree.'))}">
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</etd_ms:degree>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0