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 842631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:59:39+00:00 2026-05-15T05:59:39+00:00

I’m having trouble inserting a block of text when a certain condition is met

  • 0

I’m having trouble inserting a block of text when a certain condition is met into an xml document using XSLT. Suppose I have the following xml:

<oldStep Name="Step1">
    <oldItems>
        <oldItem ItemName="item1">
        </oldItem>
        <!-- want to add an extra <Item> here if Step Name == "Step1" -->
        <oldItem ItemName="Step1item">
        </oldItem>
    </oldItems>
</oldStep>
<oldStep Name="Step2">
    <oldItems>
       ...
    </oldItems>
</oldStep>

Amongst the conversion of the old node names into new ones, I want to add an extra block of text (or a manually constructed node) whenever oldStep Name is equal to a certain value (in this case, Step1). I tried using the following XSLT, but it ended up with a weird behaviour in adding the block of text to every single node (sometimes even not under a node) in the xml once its matched. Also, I’m having trouble skipping the node so the node can be inserted within Items, not directly under oldStep:

<xsl:template match="oldItems">
    <xsl:element name="Item">
        <xsl:if test="../@Name = 'Step1'>
            <xsl:call-template name = "identity"></xsl:call-template>
        </xsl:if>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

<xsl:template match="node()" name="identity">
    <xsl:element name="Item">
        <xsl:attribute name="ItemName">Step1item</xsl:attribute>
        </xsl:apply-templates />
    </xsl:element>
</xsl:template>

I get the feeling that I’ve gotten the conditions wrong, but googling didn’t really help (search string too vague). What am I missing in the xsl? Or, is there a better approach to this problem?

Thanks.

  • 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-15T05:59:40+00:00Added an answer on May 15, 2026 at 5:59 am

    Your description is a little hard to follow. Here’s how I interpret your question:

    Give me a transformer that copies everything by default, except:

    • it converts oldItem elements into Item elements
    • it adds an extra item to the oldItems element under “step 1”, as the last element

    And here’s a solution:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <!-- by default, copy everything -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <!-- special handling for old items -->
      <xsl:template match="oldItems">
        <!-- transform all old items first -->
        <xsl:apply-templates select="@*|node()"/>
        <xsl:if test="../@Name = 'Step1'">
          <xsl:call-template name="add-new-step1-stuff"/>
        </xsl:if>
      </xsl:template>
      <!-- add new step 1 stuff -->
      <xsl:template name="add-new-step1-stuff">
        <xsl:element name="Item">
          <xsl:attribute name="ItemName">Step1item</xsl:attribute>
        </xsl:element>
      </xsl:template>
      <!-- converts each oldItem to Item -->
      <xsl:template match="oldItem"> 
        <xsl:element name="Item">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    which, when applied to:

    <?xml version="1.0" encoding="utf-8"?>
    <oldSteps>
      <oldStep Name="Step1">
        <oldItems>
          <oldItem ItemName="item1"></oldItem>
        </oldItems>
      </oldStep>
      <oldStep Name="Step2">
        <oldItems>
          <oldItem ItemName="item2"></oldItem>
          <oldItem ItemName="item3"></oldItem>
        </oldItems>
      </oldStep>
    </oldSteps>
    

    (not exactly what you gave, but what I think you were going for)

    yields:

    <?xml version="1.0"?>
    <oldSteps>
      <oldStep Name="Step1">
    
          <Item ItemName="item1"/>
        <Item ItemName="Step1item"/>
      </oldStep>
      <oldStep Name="Step2">
    
          <Item ItemName="item2"/>
          <Item ItemName="item3"/>
    
      </oldStep>
    </oldSteps>
    

    which needs some whitespace cleanup, but I think is the structure you’re looking for.

    If you wanted block text instead of an extra item, you can simply rewrite the add-new-step1-stuff template like this:

    <!-- add new step 1 stuff -->
    <xsl:template name="add-new-step1-stuff">
      <xsl:text>
        Here's some block text that goes at the end.
      </xsl:text>
    </xsl:template>
    

    Key differences between my stylesheet and yours:

    • Your identity (copy-by-default) transform applies to every node in your document and produces an Item, within which you’re copying the rest of the contents – which is why you’re seeing Items all over the place! In mine, the identity transform and the addition of an element are separate. Note that the identity transform does not need to be called explicitly if you add apply-templates at the appropriate places.
    • Your transformations are missing the copying of attributes, which I imagine you want to keep instead.
    • Your transformation will add an Item after every oldItem in oldItems underneath Step 1, which I’m also imagining is not what you want.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.