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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:01:55+00:00 2026-06-09T08:01:55+00:00

I have xml document like below, <chapter xmlns:xi=http://www.w3.org/2001/XInclude xml:id=chapter1> <title>First chapter</title> <section xml:id=section1> <imageobject>

  • 0

I have xml document like below,

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
<title>First chapter</title>
<section xml:id="section1">
                <imageobject>
                    <image fileref="images/image1.jpg"/>
                </imageobject>
                <imageobject>
                    <image fileref="images/image2.jpg"/>
                </imageobject>
</section>
    <section xml:id="section2" xml:base="../other/section1.xml">  
                    <imageobject>
                        <image fileref="images/image1.jpg"/>
                    </imageobject>
                    <imageobject>
                        <image fileref="images/image2.jpg"/>
                    </imageobject>

<section xml:id="section3" xml:base="../some-other/more/section3.xml">
                    <imageobject>
                        <image fileref="images/image1.jpg"/>
                    </imageobject>
    </section>
    </section>
    <section xml:id="section4" xml:base="../some-other/section4.xml">
                    <imageobject>
                        <image fileref="images/image2.jpg"/>
                    </imageobject>
    </section>
 </chapter>

Be cause of same image name is repeated in different sections, I am renaming all image names other than first section using a java class. Then I can generate a list of renamed image names.

Now I want to reflect those changes in the above xml file also. As example when I rename “image1.jpg” to “aaa.jpg” in section2, I need to reflect that change in the my initial xml by generating a new xml with new renamed image names.

For that purpose, I am using Ant script which uses XSLT 1.0 and take my first xml and renamed image list as inputs and generate a new xml document with new fileref values. How I can make that XSLT and use it in my Ant script.

Here is my new renamed images list.

<Imagedata>
  <section>
    <sectionID>section2</sectionID>
    <relativepath>images/aaa.jpg</relativepath>
    <relativepath>images/bbb.jpg</relativepath>
  </section>
  <section>
    <sectionID>section3</sectionID>
    <relativepath>images/ccc.jpg</relativepath>
  </section>
   <section>
    <sectionID>section4</sectionID>
    <relativepath>images/ddd.jpg</relativepath>
  </section>
</Imagedata>

And my new final xml will be some thing like,

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
    <title>First chapter</title>
    <section xml:id="section1">
                    <imageobject>
                        <image fileref="images/image1.jpg"/>
                    </imageobject>
                    <imageobject>
                        <image fileref="images/image2.jpg"/>
                    </imageobject>
    </section>
        <section xml:id="section2" xml:base="../other/section1.xml">  
                        <imageobject>
                            <image fileref="images/aaa.jpg"/>
                        </imageobject>
                        <imageobject>
                            <image fileref="images/bbb.jpg"/>
                        </imageobject>

    <section xml:id="section3" xml:base="../some-other/more/section3.xml">
                        <imageobject>
                            <image fileref="images/ccc.jpg"/>
                        </imageobject>
        </section>
        </section>
        <section xml:id="section4" xml:base="../some-other/section4.xml">
                        <imageobject>
                            <image fileref="images/ddd.jpg"/>
                        </imageobject>
        </section>
     </chapter>

Thank you..!!

  • 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-09T08:01:58+00:00Added an answer on June 9, 2026 at 8:01 am

    Just for a quick demonstration, I have put the Imagedata document into a variable inside the stylesheet show below. In real use, you will pass in a parameter which is the URI of the Image data document, like so …

    <xsl:param name="ImageDataURI" />
    <xsl:variable name="ImageData" select="document($ImageDataURI)" />
    

    That aside, this XSLT 1.0 style-sheet…

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:variable name="ImageData">
    <Imagedata>
      <section>
        <sectionID>section2</sectionID>
        <relativepath>images/aaa.jpg</relativepath>
        <relativepath>images/bbb.jpg</relativepath>
      </section>
      <section>
        <sectionID>section3</sectionID>
        <relativepath>images/ccc.jpg</relativepath>
      </section>
       <section>
        <sectionID>section4</sectionID>
        <relativepath>images/ddd.jpg</relativepath>
      </section>
    </Imagedata>
    </xsl:variable>
    
    <xsl:template match="@*|node()">
      <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="image/@fileref">
      <xsl:attribute name="fileref">
        <xsl:value-of select="
        (msxsl:node-set($ImageData)/Imagedata/section/
        sectionID[.=current()/../../../self::section/@xml:id]/
        following-sibling::relativepath[
         count( current()/../../preceding-sibling::imageobject) + 1
          ] | .)[1]"/>
      </xsl:attribute>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    …will transform your supplied input document to the expected output document.

    Explanation

    Consider the value-of expression in the last template. Starting from our matched fileref attribute, we navigate up until we get to section and get its id. This is given by the expression …

    current()/../../../self::section/@xml:id
    

    Then we take our look-up data and find the sectionID s. This is given by expression…

    msxsl:node-set($ImageData)/Imagedata/section/sectionID
    

    We need to cross-link these sections by section name. We achieve this by applying a predicate to the sectionID that its value must be the section value of the focus item. The predicate is here…

    [.=current()/../../../self::section/@xml:id]
    

    Now we have found the right lookup-section, we need to index into it by our current position of the focus item within the document-section. We calculate our position by counting the preceding siblings and adding one, like thus …

    count( current()/../../preceding-sibling::imageobject) + 1
    

    And thus the look-up replacement node, if it exists, is given by…

    msxsl:node-set($ImageData)/Imagedata/section/
    sectionID[.=current()/../../../self::section/@xml:id]/
    following-sibling::relativepath[
     count( current()/../../preceding-sibling::imageobject) + 1
    

    That’s all well and good when a replacement is in order, but there are cases where there is no co-responding lookup. In this case our attribute needs to retain its original value. We achieve this by an expression of the form …

    ( $something | .)[1]
    

    If $something doesn’t exist (meaning it;s value is the empty sequence), then the above expression simply returns the focus item. If it does exist, then either the first item of $something will be returned or the focus item. Normally the union operator concatenates two sequences, de-dups and sorts in document order. But the two operands come from different documents, so there is no sorting nor de-duping going on. Thus the expression returns the replacement node when it exists, or if not then the focus item.

    • 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 that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I have XML shaped like the following: <?xml version=1.0 encoding=UTF-8?> <feed xmlns=http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearch/1.1/ xmlns:docs=http://schemas.google.com/docs/2007
I have an XML template document: <TriadMessage xmlns=http://www.myco.com/02/11/2008/V1/TriadMessage.xsd xmlns:triad=http://www.myco.com/02/11/2008/V1/TriadTypes.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance > <TriadRouteInfo> <triad:RoutingCorrelationId>new value
I have XML as below which I am loading into XDocument <ConversionSpecification xmlns=http://tempuri.org/X12ParserSpecification.xsd TransactionSetIdentifierCode=837>
I have a XML document like shown below: <root> <level1> <level2> <level3> ..... </level3>
I have an xml document like the below one that I am trying to
I have an xml document that looks something like below (resources.xml), with a corresponding
I have some xml XML Document with duplicate tag names like below :: <ROOT>
I have a xml document like this : <Node1 attrib1=abc> <node1_1> <node1_1_1 attrib2 =
Hi I have an xml document like this <root> <cert id=1> </cert> <cert id=2>

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.