Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:25:02+00:00 2026-05-25T19:25:02+00:00

I am new to XSLT transformations. I am writing some transformations in order to

  • 0

I am new to XSLT transformations. I am writing some transformations in order to refactor code expressed in srcML and I am facing a problem. The XML input I am working with looks like this:

....
<function><type><name>void</name></type> <name>DrawHorizontal</name>
<parameter_list>(
    <param><decl><type><name>t_paper</name></type> <name>p</name></decl></param>,
    <param><decl><type><name>int</name></type> <name>x</name></decl></param>,
    <param><decl><type><name>int</name></type> <name>y</name></decl></param>)
 </parameter_list>
   <block>{.....
     <expr_stmt><expr>
       <name>
         <name>p</name>
         <index>[<expr><name>x</name></expr>]</index>
         <index>[<expr><name>y</name></expr>]</index>
       </name>.
       <name>hor</name>
       <operator>=</operator> 1
     </expr>;</expr_stmt>
  ...
  }</block>
</function>

What I want to do is, for each function section:

1) Identify the name of the parameters of a certain type (p of type t_paper for example)

2) Change the expression statement where the parameters, identified in the previous step, are found ()

The output should look like this:

.....
<function><type><name>void</name></type> <name>DrawHorizontal</name>
<parameter_list>(
    <param><decl><type><name>t_paper</name></type> <name>p</name></decl></param>,
    <param><decl><type><name>int</name></type> <name>x</name></decl></param>,
    <param><decl><type><name>int</name></type> <name>y</name></decl></param>)
 </parameter_list>
   <block>{.....
     <expr_stmt><expr>
       <name>p</name>.
       <name>
         <name>data</name>
         <index>[<expr><name>x</name></expr>]</index>
         <index>[<expr><name>y</name></expr>]</index>
       </name>.
       <name>hor</name>
       <operator>=</operator> 1
     </expr>;</expr_stmt>
  ...
  }</block>
</function>

In summary, I need to transform expr_stmt nodes only where parameters of a certain type are found (t_paper in the example). The rest of the file should be copied as it is.

My problem is that I am not able to apply templates to that specific sections based on the dynamically created conditions (the names of the parameters).

Some NOT working code is the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node() | @*">
   <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
   </xsl:copy>
</xsl:template>

<xsl:template match="//function">
<xsl:choose>
       <xsl:when test="parameter_list/param/decl/type/name='t_paper'">
    <xsl:copy>
     <xsl:call-template name="transform-function" select="node()">
      <xsl:with-param name="appearance" select="parameter_list/param/decl/name" />
     </xsl:call-template>
        </xsl:copy> 
       </xsl:when>
       <xsl:otherwise>
         <function><xsl:apply-templates select="node()"/></function>
   </xsl:otherwise>
</xsl:choose>
 </xsl:template>

 <xsl:template name="transform-function">
<xsl:param name="appearance" />
<xsl:if test="//expr_stmt/expr/name/name=$appearance">
    substitution here
</xsl:if>
 </xsl:template>
</xsl:stylesheet>

I would be very grateful if someone could give me a hint.

Please let me know if you need more details as this is my first question in SO.

Cheers,

D.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:25:02+00:00Added an answer on May 25, 2026 at 7:25 pm

    This simple transformation (no explicit conditionals at all):

    <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:key name="kParamByNameType" match="param"
      use="concat(decl/name, '+', decl/type/name)"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
       "expr/name
             [key('kParamByNameType',
                  concat(name, '+', 't_paper')
                  )
             ]">
      <xsl:copy-of select="name"/>
      <name>
        <name>data</name>
        <xsl:apply-templates select="name/following-sibling::node()"/>
      </name>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <function>
        <type>
            <name>void</name>
        </type>
        <name>DrawHorizontal</name>
        <parameter_list>(     
            <param>
               <decl>
                        <type>
                            <name>t_paper</name>
                        </type>
                        <name>p</name>
           </decl>
            </param>,     
            <param>
                <decl>
                    <type>
                        <name>int</name>
                    </type>
                    <name>x</name>
                </decl>
            </param>,     
            <param>
                <decl>
                    <type>
                        <name>int</name>
                    </type>
                    <name>y</name>
                </decl>
            </param>)  
        </parameter_list>
        <block>{.....      
            <expr_stmt>
                <expr>
                    <name>
                        <name>p</name>
                        <index>[
                            <expr>
                                <name>x</name>
                            </expr>]
                        </index>
                        <index>[
                            <expr>
                                <name>y</name>
                            </expr>]
                        </index></name>.        
                    <name>hor</name>
                    <operator>=</operator> 1      
                </expr>;
            </expr_stmt>   ...   }
        </block>
    </function>
    

    produces the wanted result:

    <function>
       <type>
          <name>void</name>
       </type>
       <name>DrawHorizontal</name>
       <parameter_list>(     
            <param>
             <decl>
                <type>
                   <name>t_paper</name>
                </type>
                <name>p</name>
             </decl>
          </param>,     
            <param>
             <decl>
                <type>
                   <name>int</name>
                </type>
                <name>x</name>
             </decl>
          </param>,     
            <param>
             <decl>
                <type>
                   <name>int</name>
                </type>
                <name>y</name>
             </decl>
          </param>)  
        </parameter_list>
       <block>{.....      
            <expr_stmt>
             <expr>
                <name>p</name>
                <name>
                   <name>data</name>
                   <index>[
                            <expr>
                         <name>x</name>
                      </expr>]
                        </index>
                   <index>[
                            <expr>
                         <name>y</name>
                      </expr>]
                        </index>
                </name>.        
                    <name>hor</name>
                <operator>=</operator> 1      
                </expr>;
            </expr_stmt>   ...   }
        </block>
    </function>
    

    Explanation:

    1. Overriding the identity rule just for parameters that are of the requested type.

    2. Using a key to conveniently define the mapping between a parameter declaration and its (name, type)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on XML to XML transformations through XSLT. I want to extract
I am new to XSLT working on XML to XML transformation. I want to
i am pretty new to XSLT / XML and HTML. I have a XML
Currently I have some legacy ASP.NET 2.0 code that uses the ASP Xml web
I am new to XML and have been trying some simple examples and they
I am processing XML files transformations with XSLT in PHP correctly. Actually I use
I'm transforming xml using the following code. It works fine for one xslt but
I generate some PDF via XSLT transformation (FOP) from XML. The PDF file is
I'm facing a nasty encoding issue when transforming XML via XSLT through PHP. The
I'm kind of new to XSLT, and I've gotten basic transformation done. Next I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.