User 4.5.5
User 4.5.5

Reputation: 1321

xslt required to transformed elements with 'name' attribute

my xml: The input xml to be transformed

                 <content>    
                     <conditionalText name="Masked_Account_Number">
                          <text>
                             Hi 2<dynamicVariable name="asked_Account"/> is needed.
                         </text>
                     </conditionalText>

                     <dynamicInclude name="xyz" />
                </content>

So I have to use above xml,identify all the tags containing 'name' as attribute,extract value of it and create tag 'name' and under it , itshould have 'a' tag the format is as follows :

transformed output for all tags containing 'name' as their attribute in input xml should be as:

                             <name>
                                <a href="#" id="dynamicvariable" 
                                   name="value of name attribute"                   
                                   xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
                                </a>
                          </name>

and only for input xml file's 'dynamicVariable' Tag it should only create 'a' tag as

transformed output for 'dynamicVariable' tag of input xml should look like:

                      <a href="#" id="dynamicvariable" 
                                name="value of name attribute"                   
                            xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
                      </a>

so my output xml after transformation should look like

           <content1>
                     <conditionalText>
                         <text>
                           Hi 2
                           <a href="#" id="dynamicvariable" 
                               name="asked_Account"                   
                               xmlns="http://www.w3.org/1999/xhtml">[asked_Account]
                           </a>is needed
                        </text>

                         <name>
                           <a href="#" id="dynamicvariable"                                 
                             name="Masked_Account_Number"                   
                             xmlns="http://www.w3.org/1999/xhtml">[Masked_Account_Number]
                           </a>
                        </name>
                  </conditionalText>

                  <dynamicInclude>
                      <name>
                         <a href="#" id="dynamicvariable" name="xyz"                   
                            xmlns="http://www.w3.org/1999/xhtml">[xyz]
                         </a>
                     </name>
                  </dynamicIncude>
          </content1>

XSLT tried is given below: I have took two templates.First template will identify all the tags containing 'name' as their attribute and extract value of it and creates in format as

                                 <name>
                                  <a href="#" id="dynamicvariable" 
                                    name="value of name attribute"                   
                                   xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
                                  </a>
                                </name>

And the second template will override the first template and it will identify only tag with name 'dynamicVariable' and extracts its 'name' attribute value and creates tag with format as

                            <a 
                              href="#" id="dynamicvariable" 
                              name="value of name attribute"                   
                             xmlns="http://www.w3.org/1999/xhtml">[value of name attribute]
                            </a>

so my final xslt is as below:

                  <?xml version="1.0" encoding="utf-8"?>
           <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
             <xsl:output method="xml" indent="yes"/>

               <xsl:template match="content/*">
                    <content1>
                      <xsl:apply-templates />
                    </content1>
              </xsl:template>

           <xsl:template  match= "*/@name" >
                   <name>
                      <a href ='#'  id="dynamicvariable" xmlns="http://www.w3.org/1999/xhtml" >
                          <xsl:copy-of select="@*"/>
                              [<xsl:value-of select="@name"/>]
                      </a>
                  </name>

                 <xsl:for-each select="child::*">
                    <xsl:if test="name()='text'">
                          <text>
                            <xsl:apply-templates />
                           </text>
                     </xsl:if>
                  </xsl:for-each>
      </xsl:template>

                <xsl:template  match= "*[name()='dynamicVariable']" >
                  <a href ='#'  id="dynamicvariable" xmlns="http://www.w3.org/1999/xhtml" >
                     <xsl:copy-of select="@*"/>
                       [<xsl:value-of select="@name"/>]
                  </a>
                </xsl:template>

        </xsl:stylesheet>

but didnt get the required transformed xml . Can anyone help me out . and the transformed xml file having conditionalText element should have then as subchilds in the same sequence as specified in the transformed xml.

Upvotes: 3

Views: 872

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This transformation:

<xsl:stylesheet version="1.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="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*[not(name()='name')]"/>
       <xsl:apply-templates select="@name"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*/@name">
   <name>
     <a href="#" id="dynamicvariable"
        name="{.}"
        xmlns="http://www.w3.org/1999/xhtml">[<xsl:value-of select="."/>]
     </a>
   </name>
 </xsl:template>

 <xsl:template match="dynamicVariable">
   <a href="#" id="dynamicvariable"
      name="{@name}"
      xmlns="http://www.w3.org/1999/xhtml">[<xsl:value-of select="@name"/>]
   </a>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<content>
  <conditionalText name="Masked_Account_Number">
    <text>
      Hi 2<dynamicVariable name="asked_Account"/> is needed.
    </text>
  </conditionalText>

  <dynamicInclude name="xyz" />
</content>

produces the wanted (the renaming of content is left as an exercise to the reader), correct result:

<content>
   <conditionalText>
      <text>
      Hi 2<a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="asked_Account">[asked_Account]
   </a> is needed.
    </text>
      <name>
         <a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="Masked_Account_Number">[Masked_Account_Number]
     </a>
      </name>
   </conditionalText>
   <dynamicInclude>
      <name>
         <a xmlns="http://www.w3.org/1999/xhtml" href="#" id="dynamicvariable" name="xyz">[xyz]
     </a>
      </name>
   </dynamicInclude>
</content>

Upvotes: 1

Related Questions