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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:54:04+00:00 2026-06-13T09:54:04+00:00

I have the following XML file. <Record Name=My_Record <Fields StartingBit=0 Size=3 Name=Field_1> </Fields> <Fields

  • 0

I have the following XML file.

<Record 
  Name="My_Record"
  <Fields
    StartingBit="0"
    Size="3"
    Name="Field_1">
  </Fields>
  <Fields 
    StartingBit="1"
    Size="5"
    Name="Field_2">
  </Fields>
  <Fields
    StartingBit="2"
    Size="8"
    Name="Field_3">
  </Fields>
  <Fields
    StartingBit="3"
    Size="4"
    Name="Field_4"
  </Fields>
</Record>

And I would like to use XSLT to properly update the @StartingBit attribute from the previous node’s @StartingBit + @Size – this will be the current node’s @StartingBit value. The resulting XML should be as follows:

<Record 
  Name="My_Record"
  <Fields
    StartingBit="0"
    Size="3"
    Name="Field_1">
  </Fields>
  <Fields 
    StartingBit="3"
    Size="5"
    Name="Field_2">
  </Fields>
  <Fields
    StartingBit="8"
    Size="8"
    Name="Field_3">
  </Fields>
  <Fields
    StartingBit="16"
    Size="4"
    Name="Field_4"
  </Fields>
</Record>

So far, my latest attempts to my XSLT is as follows:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match ="Fields/@StartingBit">
    <xsl:value-of select ="(preceding-sibling::Fields[1]/@StartingBit + preceding-sibling::Fields[1]/@Size)"/>
  </xsl:template>
</xsl:stylesheet>

The above transform does not generate what I would like – basically @StartingBit does not change. I am not proficicent in node navigation to get the results I like – can someone assist in my transform? Thank you in advance.

  • Lorentz
  • 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-13T09:54:06+00:00Added an answer on June 13, 2026 at 9:54 am

    When this simple XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
      <xsl:output omit-xml-declaration="no" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:variable name="vStartingBit" select="/*/Fields[1]/@StartingBit" />
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Fields[position() &gt; 1]">
        <xsl:copy>
          <xsl:attribute name="StartingBit">
            <xsl:value-of
              select="$vStartingBit + sum(preceding-sibling::Fields/@Size)" />
          </xsl:attribute>
          <xsl:apply-templates select="@*[not(name() = 'StartingBit')]" />
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    …is applied to the original XML (corrected to be well-formed):

    <?xml version="1.0" encoding="utf-8"?>
    <Record Name="My_Record">
      <Fields StartingBit="0" Size="3" Name="Field_1" />
      <Fields StartingBit="1" Size="5" Name="Field_2" />
      <Fields StartingBit="2" Size="8" Name="Field_3" />
      <Fields StartingBit="3" Size="4" Name="Field_4" />
    </Record>
    

    …the expected result is produced:

    <?xml version="1.0"?>
    <Record Name="My_Record">
      <Fields StartingBit="0" Size="3" Name="Field_1" />
      <Fields StartingBit="3" Size="5" Name="Field_2" />
      <Fields StartingBit="8" Size="8" Name="Field_3" />
      <Fields StartingBit="16" Size="4" Name="Field_4" />
    </Record>
    

    Explanation:

    • The first template is the Identity Template. Its purpose is to copy all nodes and attributes from the source document to the result document as-is.
    • One template overrides the Identity Template. It’s purpose is to add the original starting bit to the correct @Size attributes (those that precede the current <Fields> element), which forms the value of each subsequent <Fields> element’s @StartingBit attribute.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML File: <persons> <person name=shawn> <age>34</age> <hair style=spikes>red</hair> </person> <person
I have the following text in xml file: <Config Builder=LP Wizard> <Libraries> <Library Name=XCAMSource/>
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <scripts> <ScriptName> <name> My
i have following xml file: <package> <reader> <id>r1007</id> <name>Robert</name> <email>robert@yahoo.com</email> </reader> <writer> <id>w1920</id> <name>Young</name>
Lets assume a xml file named data.xml with following content: <root> <record> <id>1</id> <name>test
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have the following XML file:- <?xml version=1.0 encoding=UTF-8?> <viewentries> <viewentry position=1> <entrydata columnnumber=0>
I have the following xml file: <xfa:data> <form1> <Page1> <Page2> <contractInfo> ... </contractInfo> <paymentInfo>
I have the following XML layout file, HELPME.XML <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent
I have the following invalid XML file: <?xml version=1.0 encoding=utf-8 ?> <Page num=1 crop_box=0,

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.