xslt 1.0 - How to make on xml field an attribute of another field? -


i have following xml file:

<generic_etd>   <dc.contributor>nserc</dc.contributor>   <dc.creator>gradstudent</dc.creator>   <dc.contributor>john smith</dc.contributor>   <dc.contributor.role>advisor</dc.contributor.role>   <dc.date>2013-05-07</dc.date>   <dc.format>30 pages</dc.format>   <dc.format>545709 bytes</dc.format>   <dc.contributor>jane smith</dc.contributor>   <dc.contributor.role>committee member</dc.contributor.role> </generic_etd> 

which transformed following using xslt 1.0:

<etd_ms>   <etd_ms:contributor>nserc</etd_ms:contributor>   <etd_ms:creator>gradstudent</etd_ms:creator>   <etd_ms:contributor role="advisor">john smith</etd_ms:contributor>   <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:contributor role="committee member">jane smith</etd_ms:contributor> </etd_ms> 

i can etd_ms substitution having difficulty inserting contributor.role line attribute of contributor line. new @ xslt transforms has me stumped. suggestions?

here code far (i have left out beginning , end tags brevity. want thank navin rawat provided more succinct version of code had):

<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:otherwise>             <xsl:variable name="newtag" select="concat('etd_ms:',substring-after(name(),'.'))"/>             <xsl:choose>                 <xsl:when test="contains($newtag, '.')">                     <xsl:element name="{substring-before($newtag,'.')}">                         <xsl:apply-templates/>                     </xsl:element>                 </xsl:when>                 <xsl:otherwise>                     <xsl:element name="{$newtag}">                         <xsl:apply-templates/>                     </xsl:element>                 </xsl:otherwise>             </xsl:choose>         </xsl:otherwise>     </xsl:choose> </xsl:template> 

thanks.

i managed figure out solution myself. created template call previous xslt file included in original question. here template used:

<xsl:template name="contributor-role">   <xsl:param name="element-tag"/>   <xsl:choose>     <!-- check if next element contributer role -->     <xsl:when test="following-sibling::dc.contributor.role[1]">       <xsl:element name="{$element-tag}">         <!-- add role attribute of current element-->         <xsl:attribute name="role">           <xsl:value-of select="following-sibling::dc.contributor.role[1]"/>         </xsl:attribute>         <xsl:apply-templates/>       </xsl:element>     </xsl:when>     <xsl:otherwise>       <!-- else process element -->       <xsl:element name="{$element-tag}">         <xsl:apply-templates/>       </xsl:element>     </xsl:otherwise>   </xsl:choose> </xsl:template> 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -