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..!!

vish
  • 105
  • 10

1 Answers1

1

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.

Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
  • ,Thank you very much for your response and nice explanation. Did you tried with my provided xml document and did it generates expected output successfully? I am getting an error saying "xmlXPathCompOpEval: function node-set not found XPath error : Unregistered function runtime error: file sean.xsl line 34 element value-of XPath evaluation returned no result." here sean.xsl is your xslt document..I am using xsltproc..Thank you.. – vish Aug 04 '12 at 04:21
  • If you want to use node-set(), which namespace it belongs to depends on your XSLT processor. Try xmlns:exsl="http://exslt.org/common instead of the given microsoft one. – Sean B. Durkin Aug 04 '12 at 06:56
  • ,,I am using xsltproc processor. It still gives an error function node-set not found. What will be the namespace for xsltproc processor?? – vish Aug 04 '12 at 07:20
  • I used xmlns:exsl="http://exslt.org/common" and it generates a xml output similar to my main xml document. I tried by doing some modification, but I couldn't generate my expected output. I am very new to XSLT. Please tell me where I may have missed some thing. As in my output xml, I want to do changes from section2, not from section1. Is there any way to generate output without using node-set() function? Thanks in advance.. – vish Aug 04 '12 at 11:55
  • The stylesheet I gave does do the changes from section 2 and not section 1. This was tested on www.xmlper.com and it works perfectly. I can't tell what is wrong with your stylesheet without seeing it. Please accept my answer (click on the tick symbol), and post a new question with your exact style-sheet, sample input and expected output. – Sean B. Durkin Aug 04 '12 at 12:07
  • Ahh..Yes..It works perfectly in www.xmlper.com and that is my desired output. I guess I have problem in my processor. It doesn't identify xmlns:msxsl="urn:schemas-microsoft-com:xslt". I tried in Saxon also. But it also doesn't work. I will post a new question and please give me answer which can use in xsltproc. Thank you very much for your guidance.. – vish Aug 04 '12 at 12:38