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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:49:31+00:00 2026-05-14T06:49:31+00:00

I have a xml document. I know the index where i need to insert

  • 0

I have a xml document. I know the index where i need to insert the new node. The index that i have is the position considering only the text nodes and ignoring the element tags. Is there a java api to seek the index in a xml knowing the position relative to the text nodes alone and insert a new node in that position?

  • 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-14T06:49:32+00:00Added an answer on May 14, 2026 at 6:49 am

    The following XSLT 2.0 stylesheet is an attempt to extend the solution in the original XSLT 2.0 stylesheet to receive a list of index positions and nodes to be inserted with one transformation:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xsd"
      version="2.0">
    
      <xsl:param name="insert-file" as="xsd:string" select="'insert-data.xml'"/>
    
      <xsl:variable name="main-root" as="document-node()" select="/"/>
    
      <xsl:variable name="insert-data" as="element(data)*">
        <xsl:for-each-group select="doc($insert-file)/insert-data/data" group-by="xsd:integer(@index)">
          <xsl:sort select="current-grouping-key()"/>
          <data index="{current-grouping-key()}" text-id="{generate-id($main-root/descendant::text()[sum((preceding::text(), .)/string-length(.)) ge current-grouping-key()][1])}">
            <xsl:copy-of select="current-group()/node()"/>
          </data>
        </xsl:for-each-group>
      </xsl:variable>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@*, node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="text()[generate-id() = $insert-data/@text-id]">
        <xsl:variable name="preceding-text" as="xsd:integer" select="sum(preceding::text()/string-length(.))"/>
        <xsl:variable name="this" as="text()" select="."/>
        <xsl:variable name="insert-here" as="element(data)+">
          <xsl:for-each select="$insert-data[@text-id = generate-id(current())]">
            <data split-index="{@index - $preceding-text}">
              <xsl:copy-of select="node()"/>
            </data>
          </xsl:for-each>
        </xsl:variable>
    
        <xsl:for-each select="$insert-here">
          <xsl:variable name="pos" as="xsd:integer" select="position()"/>
          <xsl:value-of select="substring(
            $this, 
            if ($pos eq 1) then 1 else xsd:integer($insert-here[$pos - 1]/@split-index), 
            if ($pos ne 1) then xsd:integer(@split-index) - xsd:integer($insert-here[$pos - 1]/@split-index) else xsd:integer(@split-index) - 1)"/>
          <xsl:copy-of select="node()"/>
        </xsl:for-each>
    
        <xsl:value-of select="substring($this, $insert-here[last()]/@split-index)"/>  
      </xsl:template>
    
    </xsl:stylesheet>
    

    This stylesheet expects a file insert-data.xml to contain the data in the following format:

    <insert-data>
      <data index="2"><e/></data>
      <data index="4"><f/></data>
      <data index="6"><g/></data>
      <data index="6"><h/></data>
      <data index="18"><i/></data>
    </insert-data>
    

    So each ‘data’ element contains the nodes to be inserted at the position given by the ‘index’ attribute.

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

Sidebar

Related Questions

I have an xml document object that I need to convert into a string.
I have an XML document: var xml:XML = new XML(<rootNode> <head> <meta name=template content=Default
I have an XML document that I'm trying to style via CSS. A relevant
Say I have a xml document that looks like this <foo> <bar id=9 />
In my Python app, I have an XML document that I'd like to transform
I have a long document in XML from which I need to produce static
I have an XML document with un-namespaced elements, and I want to use XSLT
I have an XML document something like ::: <?xml version=1.0 encoding=utf-8?> <?mso-application progid=Excel.Sheet?> <Workbook
I have an XML document looking similar to this: <items> <item cat=1 owner=14>bla</item> <item
In Flex, I have an xml document such as the following: var xml:XML =

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.