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

  • Home
  • SEARCH
  • 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 8429987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:29:10+00:00 2026-06-10T05:29:10+00:00

In this code example I have 2 challenges one is to set B1 node

  • 0

In this code example I have 2 challenges one is to set B1 node after B node and before node C, D and E and the second challenge is to Add the second KEY node to the /ROOT/E/OTHER/DEAL/KEYS structure.

This XML sample:

        <ROOT>
          <A>some A text</A>
          <B>some B text</B>
          <C>some C text</C>
          <D>some D text</D>
          <E>
            <OTHER>
              <DEAL>
                <KEYS>
                  <KEY>
                    <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
                    <KeyValue>123456|1</KeyValue>
                  </KEY>
                </KEYS>
              </DEAL>
            </OTHER>
          </E>
        </ROOT>

after the transformation:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xsl:strip-space elements="*"/>
        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates  select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <!-- Identifiers are added by the system. Need to pass parms from the calling program -->
        <xsl:template match="ROOT" name="add-B1">
            <xsl:variable name="elements-after" select="C|D|E"/>
            <xsl:copy>
                <xsl:copy-of select="* except $elements-after"/>
                <B1>some B1 text</B1>
                <xsl:copy-of select="$elements-after"/>
            </xsl:copy>
        </xsl:template>

        <!-- KEY is added by the system. Need to pass parms from the calling program -->
        <xsl:template match="ROOT/E/OTHER/DEAL/KEYS" name="add-KEYS">
            <xsl:param name="KeyIdentifierTypeB">654321|1</xsl:param>
            <xsl:copy>
                <xsl:copy-of select="*"/>
                <KEY>
                    <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
                    <KeyValue>
                        <xsl:value-of select="$KeyIdentifierTypeB"/>
                    </KeyValue>
                </KEY>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

become:

        <?xml version="1.0" encoding="UTF-8"?>
        <ROOT>
            <A>some A text</A>
            <B>some B text</B>
            <B1 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">some B1 text</B1>
            <C>some C text</C>
            <D>some D text</D>
            <E>
                <OTHER>
                    <DEAL>
                        <KEYS>
                            <KEY>
                                <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
                                <KeyValue>123456|1</KeyValue>
                            </KEY>
                        </KEYS>
                    </DEAL>
                </OTHER>
            </E>
        </ROOT>

why the second template definition has been completely ignored?

The first code college has been resolved B1 node is set after B node and before node C, D and E or in the other words B1 node is set and nodes that must be places after it are: C, D and E.
The second template match=”ROOT/E/OTHER/DEAL/KEYS” that should satisfy the second challenge part: to Add the second KEY node to the /ROOT/E/OTHER/DEAL/KEYS structure, has been ignored completely.
In addition to this fact, if you comment the first template match over the ROOT node, the second template match=”ROOT/E/OTHER/DEAL/KEYS” will work correctly, it will actually add the additional key, but I don’t know why the first template match always overrides the second one. I try the xsl:template match=”ROOT/E/OTHER/DEAL/KEYS… and the xsl:for-each select=… and xsl:call-template name=”add-KEYS” but nothing helped me out.

I actually understand, that apply-templates, match nodes templates with higher structure with highest priority. Changing template place in the XSLT file will not have any impact, it will not read it in the exact line sequence, instead it will process it by matching priority.
“apply-templates” for each matching template will made a change on the XML structure, implicitly it will made the “for-each” for us, but I don’t how to “advice” the second template that the structure has been changed, and why I should do it, because the second template match is looking for a different XPath location, that btw has never been touched from the first template. Should I apply the template sequencing in my case?… and what is the best practice how to do it?

Expected result:

        <?xml version="1.0" encoding="UTF-8"?>
        <ROOT>
            <A>some A text</A>
            <B>some B text</B>
            <B1>some B1 text</B1>
            <C>some C text</C>
            <D>some D text</D>
            <E>
                <OTHER>
                    <DEAL>
                        <KEYS>
                            <KEY>
                                <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
                                <KeyValue>123456|1</KeyValue>
                            </KEY>
                            <KEY>
                                <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
                                <KeyValue>654321|1</KeyValue>
                            </KEY>
                        </KEYS>
                    </DEAL>
                </OTHER>
            </E>
        </ROOT>
  • 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-06-10T05:29:11+00:00Added an answer on June 10, 2026 at 5:29 am

    The second template match="ROOT/E/OTHER/DEAL/KEYS" that should
    satisfy the second challenge part: to Add the second KEY node to the
    /ROOT/E/OTHER/DEAL/KEYS structure, has been ignored completely.

    The problem is that in the template matching ROOT there isn’t any xsl:apply-templates. A template only gets selected for execution in result of an xsl:apply-templates instruction. In your code there isn’t any xsl:apply-templates, therefore the XSLT processor doesn’t apply templates any more and finishes execution.

    It would be different if you replaced xsl:copy-of with xsl:apply-templates.

    Here is how to do this:

    <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()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="@*|B/preceding-sibling::node()"/>
        <xsl:apply-templates select="B"/>
        <B1>some B1 text</B1>
        <xsl:apply-templates select="B/following-sibling::node()"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="KEY">
      <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
       <KEY>
        <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
        <KeyValue>
         <xsl:value-of select="'654321|1'"/>
        </KeyValue>
       </KEY>
       </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <ROOT>
        <A>some A text</A>
        <B>some B text</B>
        <C>some C text</C>
        <D>some D text</D>
        <E>
            <OTHER>
                <DEAL>
                    <KEYS>
                        <KEY>
                            <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
                            <KeyValue>123456|1</KeyValue>
                        </KEY>
                    </KEYS>
                </DEAL>
            </OTHER>
        </E>
    </ROOT>
    

    the wanted, correct result is produced:

    <ROOT>
       <A>some A text</A>
       <B>some B text</B>
       <B1>some B1 text</B1>
       <C>some C text</C>
       <D>some D text</D>
       <E>
          <OTHER>
             <DEAL>
                <KEYS>
                   <KEY>
                      <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
                      <KeyValue>123456|1</KeyValue>
                      <KEY>
                         <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
                         <KeyValue>654321|1</KeyValue>
                      </KEY>
                   </KEY>
                </KEYS>
             </DEAL>
          </OTHER>
       </E>
    </ROOT>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken this example test before I'll try to take the real one
I have this code (an example that PHP generates for me)... <select id=outsideBlue3> <option
If I have a simple cucumber feature and scenario , like this (example code
For example, I have this code: <table> <tr> <td class=last></td> <td></td> </tr> <tr> <td
For example we have this line chart at Google Code API there is a
For Example if i have my code like this (php): <?php somefunc(1); function somefunc($a)
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Ok, in my code I have for example, this: $('.follow').click(function(){ var myself = $(this);
For this (pseudo code) example I have two tables in MySQL: member { id,
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,

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.