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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:55:37+00:00 2026-06-14T08:55:37+00:00

I have a command contained with a message and body that need to be

  • 0

I have a command contained with a message and body that need to be duplicated. Like so:

<message>
 <body>
  <command name="1"/>
  <command name="2"/>
 </body>
</message>

What I would like is:

 <message>
 <body>
  <command name="1"/>
 </body>
</message>

<message>
 <body>
   <command name="2"/>
 </body>
</message>

I’m using the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <xsl:variable name="items" select="/message/body/*"/>
    <xsl:for-each select="$items">
        <xsl:result-document href="section{position()}.xml">
            <xsl:copy-of select="../../.[position()]"/>
        </xsl:result-document>
    </xsl:for-each>
</xsl:template>

A few clarifications: the name attribute is not a position attribute so it can vary. The command name is not known and can vary. Commands are nodes themselves and can contain elements.

Based on Dabbler’s code I was able to produce this which successfully parses the following:

<message>
<body>
 <commandA num="1"/>
 <commandB num="5"/>
 <commandC num="25">
    <subCommandT />
 </commandC>
</body>
</message>

into

<message>
 <body>
  <commandA num="1"/>
 </body> 
</message>

<message>
 <body>
  <commandB num="5"/>
 </body>
</message>

<message>
<body>
<commandC num="25">
      <subCommandT />
 </commandC>
</body>
</message>

Here is the code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <xsl:for-each select="/message/body/*">
        <xsl:apply-templates select="/*">
            <xsl:with-param name="CmdId" select="generate-id(.)" tunnel="yes"/>
            <xsl:with-param name="CmdPosition" select="position()" tunnel="yes"/>
        </xsl:apply-templates>
    </xsl:for-each>
</xsl:template>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="message">
    <xsl:param name="CmdId" as="xs:string" tunnel="yes"/>
    <xsl:param name="CmdPosition" as="xs:integer" tunnel="yes"/>
    <xsl:result-document href="section{$CmdPosition}.xml">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:result-document>
</xsl:template>
<xsl:template match="body/*">
    <xsl:param name="CmdId" as="xs:string" tunnel="yes"/>
    <xsl:if test="generate-id(.) = $CmdId">
        <xsl:copy-of select="."/>
    </xsl:if>
</xsl:template>

Thank you Dabbler!

Here is a sample of the original command:

<?xml version="1.0" encoding="UTF-8"?>
<tst:Message xmlns:tst="http://www.someschema.com/v1.4" xmlns:pc="http://www.someotherschema.com/v1.5">
<tst:Body tst:hostId="1" tst:machineId="77.D2" tst:dateTimeSent="2012-07-11T15:30:46">
    <tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:46" tst:commandId="1" tst:sessionType="response" tst:sessionId="28" >
        <tst:commandOptionAck tst:transactionId="12" tst:configurationId="3148"/>
    </tst:commandOption>
    <tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:48" tst:commandId="2" tst:sessionType="response" tst:sessionId="29" >
        <tst:commandOptionAck tst:transactionId="13" tst:configurationId="3149"/>
    </tst:commandOption>
    <tst:commandOption tst:deviceId="1" tst:dateTime="2012-07-11T15:30:50" tst:commandId="3" tst:sessionType="response" tst:sessionId="30" >
        <tst:commandOptionAck tst:transactionId="14" />
    </tst:commandOption>
    <pc:pContext pc:deviceId="1" pc:dateTime="2012-07-11T15:24:17" pc:commandId="4" pc:sessionId="47146">
        <pc:Exit pc:transactionId="3116380"/>
    </pc:pContext>
</tst:Body>

  • 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-14T08:55:38+00:00Added an answer on June 14, 2026 at 8:55 am

    There may be a better way, but this works:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
       <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
       <xsl:template match="/">
          <xsl:for-each select="/message/body/*">
             <xsl:apply-templates select="/*">
                <xsl:with-param name="CmdNum" select="position()" tunnel="yes"/>
             </xsl:apply-templates>
          </xsl:for-each>
       </xsl:template>
    
       <xsl:template match="*|@*">
          <xsl:copy>
             <xsl:apply-templates/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="message">
          <xsl:param name="CmdNum" as="xs:integer" tunnel="yes"/>
    
          <xsl:result-document href="section{$CmdNum}.xml">
             <xsl:copy>
                <xsl:apply-templates/>
             </xsl:copy>
          </xsl:result-document>
       </xsl:template>
    
       <xsl:template match="command">
          <xsl:param name="CmdNum" as="xs:integer" tunnel="yes"/>
    
          <xsl:if test="@name = $CmdNum">
             <xsl:copy-of select="."/>
          </xsl:if>
       </xsl:template>
    </xsl:stylesheet>
    

    The problem with the code as you have it is that the filter is applied to the message element instead of the commandelement.

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

Sidebar

Related Questions

I currently have a .command file for Mac that contains the following: for f
I have a command-line executable from which I would like to be able load
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have a command line tool written in c# (that i have the source
I have a command line program that prints out a report such as: I
I have a command (cmd1) that greps through a log file to filter out
I have a python script start.py that executes well from the command line. There
I have a batch script that triggers an exe (c++ code) with command line
I have a function that accepts a message in String form. The message looks
My first question. Please bear with me! I have an Ant task that need

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.