Search code examples
xmlxsltxpathtridion

how to extract attributes of an element from an XML using xslt and display in different style


I am new to XSLT. I am working transforming an XML file from one format to another format. I also want to extract values from an element and display the them in bold format.

Sample source XML:

 <Content xmlns="uuid:4522eb85">
      <first xmlns="uuid:4522eb85">Hello World. This is first field</first>
      <second author="XYZ">Hi iam second field</second>
 </Content>

Output format required:

  <root>
     <aaa>Hello World. This is first field</aaa>
     <bbb><author>**XYZ**</author>Hi iam second field</bbb>
  <root>

I am unable to extract attributes from a tag and display with style(say bold).

Please help. Thank you in advance.


Solution

  • This XSLT outputs exactly what you ask for.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="uuid:4522eb85" exclude-result-prefixes="c">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="/c:Content">
            <root>
                <aaa><xsl:value-of select="c:first"/></aaa>
                <bbb><author>**<xsl:value-of select="c:second/@author" />**</author><xsl:value-of select="c:second" /></bbb>
            </root>
        </xsl:template>
    </xsl:stylesheet>
    

    But as Jeremy suggests, you may want to consider first taking an (online) XSLT training if you want to get much further.