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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:22:42+00:00 2026-06-08T02:22:42+00:00

I am trying to solve this for someone else and have run into an

  • 0

I am trying to solve this for someone else and have run into an issue myself.

I have the XML:

<Process>
  <name>Pro1</name>
  <duration>Dur1</duration>
  <time>Time1</time>
  <name>Pro2</name>
  <duration>Dur2</duration>
  <time>Time2</time>
  <name>Pro3</name>
  <duration>Dur3</duration>
  <time>Time3</time>
  <name>Pro4</name>
  <duration>Dur4</duration>
  <time>Time4</time>
  <name>Pro5</name>
  <duration>Dur5</duration>
  <time>Time5</time>
</Process>

Output:

<Process>
  <Process_Info>
    <name>Pro1</name>
    <duration>Dur1</duration>
    <time>Time1</time>
  </Process_Info>
  <Process_Info>
    <name>Pro2</name>
    <duration>Dur2</duration>
    <time>Time2</time>
  </Process_Info>
  <Process_Info>
    <name>Pro3</name>
    <duration>Dur3</duration>
    <time>Time3</time>
  </Process_Info>
  <Process_Info>
    <name>Pro4</name>
    <duration>Dur4</duration>
    <time>Time4</time>
  </Process_Info>
  <Process_Info>
    <name>Pro5</name>
    <duration>Dur5</duration>
    <time>Time5</time>
  </Process_Info>
</Process>

Using XSLT:

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

  <xsl:variable name ="varProcess" select ="Process"/>

  <xsl:template match="Process">
    <xsl:element name="Process">
      <xsl:for-each select ="name">
        <xsl:variable name ="posName" select ="position()"/>
        <xsl:element name ="Process_Info">
          <xsl:copy-of select ="."/>
          <xsl:copy-of select="$varProcess/duration[$posName]"/>
          <xsl:copy-of select="$varProcess/time[$posName]"/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

However, the <duration> and <time> nodes will not always be present and the <name> is the only guarenteed node. Therefore if one is missing my position() selecting fails.

How can I change the XSLT to allow for it to work even when <duration> and/or <time> does not exist.

My theory would be that you select the two nodes below the current name node and if they are <duration> or <time> they are copied? But not sure how that would implement either.

Example of current output causing issue.

Input:

<Process>
  <name>Pro1</name>
  <duration>Dur1</duration>
  <time>Time1</time>
  <name>Pro2</name>
  <duration>Dur2</duration>
  <time>Time2</time>
  <name>Pro3</name>
  <duration>Dur3</duration>
  <time>Time3</time>
  <name>Pro4</name>
  <time>Time4</time>
  <name>Pro5</name>
  <duration>Dur5</duration>
</Process>

Output:

<Process>
  <Process_Info>
    <name>Pro1</name>
    <duration>Dur1</duration>
    <time>Time1</time>
  </Process_Info>
  <Process_Info>
    <name>Pro2</name>
    <duration>Dur2</duration>
    <time>Time2</time>
  </Process_Info>
  <Process_Info>
    <name>Pro3</name>
    <duration>Dur3</duration>
    <time>Time3</time>
  </Process_Info>
  <Process_Info>
    <name>Pro4</name>
    <duration>Dur5</duration> <!-- Should be in the below process_info -->
    <time>Time4</time>
  </Process_Info>
  <Process_Info>
    <name>Pro5</name>
  </Process_Info>
</Process>
  • 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-08T02:22:44+00:00Added an answer on June 8, 2026 at 2:22 am

    This does the trick, albeit a bit ‘manual’ in approach. There may be more elegant ways to achieve this

    <xsl:template match="Process">
        <xsl:element name="Process">
            <xsl:for-each select ="name">
                <xsl:element name ="Process_Info">
                    <xsl:copy-of select ="."/>
                    <xsl:variable name="firstSib" select="local-name(following-sibling::*[1])" />
                    <xsl:variable name="secondSib" select="local-name(following-sibling::*[2])" />
                    <xsl:choose>
                        <xsl:when test="$firstSib='duration'">
                            <xsl:copy-of select="following-sibling::*[1]"/>
                            <xsl:choose>
                                <xsl:when test="$secondSib='time'">
                                    <xsl:copy-of select="following-sibling::*[2]"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <time>SomeDefaultValueForMissingTime</time>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:when test="$firstSib='time'">
                            <duration>SomeDefaultValueForMissingDuration</duration>
                            <xsl:copy-of select="following-sibling::*[1]"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <duration>SomeDefaultValueForMissingDuration</duration>
                            <time>SomeDefaultValueForMissingTime</time>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
    

    Input :

    <Process>
        <name>Pro1</name>
        <duration>Dur1</duration>
        <time>Time1</time>
        <name>NameMissingDuration</name>
        <time>TimeMissingDuration</time>
        <name>NameMissingTime</name>
        <duration>DurMissingTime</duration>
        <name>NameMissingBoth</name>
        <name>NormalName</name>
        <duration>NormalDuration</duration>
        <time>NormalTime</time>
    </Process>
    

    Output

    <Process>
      <Process_Info>
        <name>Pro1</name>
        <duration>Dur1</duration>
        <time>Time1</time>
      </Process_Info>
      <Process_Info>
        <name>NameMissingDuration</name>
        <duration>SomeDefaultValueForMissingDuration</duration>
        <time>TimeMissingDuration</time>
      </Process_Info>
      <Process_Info>
        <name>NameMissingTime</name>
        <duration>DurMissingTime</duration>
        <time>SomeDefaultValueForMissingTime</time>
      </Process_Info>
      <Process_Info>
        <name>NameMissingBoth</name>
        <duration>SomeDefaultValueForMissingDuration</duration>
        <time>SomeDefaultValueForMissingTime</time>
      </Process_Info>
      <Process_Info>
        <name>NormalName</name>
        <duration>NormalDuration</duration>
        <time>NormalTime</time>
      </Process_Info>
    </Process>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying everything to try and solve this myself, but cannot find
I have been trying to solve this issue for a day now and have
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I've been trying to solve this issue for a long time, and nothing seems
I am trying to solve this problem. I have a series of SELECT statements
Been trying to solve this error for ages now, hoping that someone here can
I have been trying to solve this problem for hours (searched here as well
I've been trying to solve this myself (I was awake all last night banging
I'm trying to avoid reinstalling Eclipse to solve this , so I hope someone
I'm trying to solve this problem all afternoon but with no luck, hopefully someone

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.