xsl-list
[Top] [All Lists]

Re: [xsl] Replacing the element's attribute in sequential order

2014-08-01 08:06:53
Thanks for such a quick reply. Really appreciate your help. But the syntax
which you are suggesting is of XSLT-2.0 but I am using XSLT-1.0 so for me
"collection" and "for" statement says - "Invalid XPath".
Can you suggest something which goes with XSLT-1.0. I am doing all these
things in Eclipse Editor with the help of plugin provided for xml.

Thanks,
BR,
Varun


On Fri, Aug 1, 2014 at 12:31 PM, Marc marc(_dot_)liste(_at_)free(_dot_)fr <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

If you have all the files loaded, perhaps can you use the <xsl:number>
Marc

Le 01/08/2014 11:33, Ihe Onwuka ihe(_dot_)onwuka(_at_)gmail(_dot_)com a écrit 
:

http://www.dpawson.co.uk/xsl/sect2/N1553.html#d2256e169


On Fri, Aug 1, 2014 at 10:29 AM, varun bhatnagar 
varun292006(_at_)gmail(_dot_)com
<mailto:varun292006(_at_)gmail(_dot_)com> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com

<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:

    Hi,

    Thanks a ton for the reply.
    I am using XSLT-1.0 so I don't think for loop will work. I am
    loading the file using python code, lxml library does that for me. I
    pass my files one by one to the method given below and merge the
files.

      def startXSLTMerge(self, mergeFileName, inputFile):
             parser = etree.XMLParser()
             parser.resolvers.add(FileResolver())

             xml_input = etree.parse(open(inputFile,'r'), parser)
             xslt_root = etree.parse(open(mergeGeneric.xsl','r'), parser)
             transform = etree.XSLT(xslt_root)
             result = str(transform(xml_input))
             print str(transform(xml_input))
             f = open('Merged_File.xml', 'w+')
             f.write(str(result))
             f.close()

    I was trying something like this (this is not giving me the correct
    result, but something which I have tried):

    <xsl:template match="/InfoTag/Procedure">
    <xsl:variable name="countDocProcedures">
    <xsl:value-of select="count(/InfoTag/Procedure)"/>
    </xsl:variable>
    <xsl:variable name="countProcedures">
    <xsl:value-of
    select="count(document('NewFile1.xml')/InfoTag/Procedure)"/>
    </xsl:variable>

    <xsl:variable name="totalProcedures">
    <xsl:value-of select="$countDocProcedures + $countProcedures"/>
    </xsl:variable>
    <xsl:call-template name="callProcedure">
    <xsl:with-param name="test" select="$totalProcedures"/>
    </xsl:call-template>
    </xsl:template>
    <xsl:template name="callProcedure">
    <xsl:param name="test"/>
    <xsl:if test="$test &gt; 0">
    <xsl:copy>
    <xsl:if test="/InfoTag/Procedure/downTime">
    <xsl:attribute name="attrProc">
    <xsl:value-of select="$test"></xsl:value-of>
    </xsl:attribute>
    <xsl:attribute name="attrLevel">
    <xsl:value-of select="'##### NOT UNIQUE#####'"></xsl:value-of>
    </xsl:attribute>
    <xsl:copy-of select="downTime"></xsl:copy-of>
    </xsl:if>
    </xsl:copy>
    <xsl:call-template name="callProcedure">
             <xsl:with-param name="test" select="$test - 1"/>
           </xsl:call-template>
    </xsl:if>
    </xsl:template>

    It is getting '10' in test parameter which I have passed. Still
    trying to find out why.
    Could you help me.


    Thanks,
    BR,
    Varun


    On Fri, Aug 1, 2014 at 11:19 AM, Martin Honnen 
martin(_dot_)honnen(_at_)gmx(_dot_)de
    <mailto:martin(_dot_)honnen(_at_)gmx(_dot_)de>
    <xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com

    <mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:

        varun bhatnagar varun292006(_at_)gmail(_dot_)com
        <mailto:varun292006(_at_)gmail(_dot_)com> wrote:

            Hi,

            I have few xml files (depends on user how much he/she wants
            to pass). I
            have to take those files and merge them, so I have created a
            loop and in
            that I am passing these files one by one. For this I have
            written few
            rules to merge. But there is this <Procedure> tag where I am
            stuck. To
            merge this I need to parse each Procedure and have to modify
its
            attributes (attrProc and attrLevel). I have to replace the
            attrProc
            attribute with a sequential value and for me this will set a
            unique
            "Level" for each Procedure tag and later I will process them
            based on
            their level.


            _File1.xml_

            *<?xml version="1.0" encoding="UTF-8"?>*
            *<InfoTag>*
            *<Procedure attrProc="TestProcA" attrLevel="1">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            **
            * <Procedure attrProc="TestProcB" attrLevel="2">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            *</InfoTag>*


            _File2.xml_
            _
            _
            *<?xml version="1.0" encoding="UTF-8"?>*
            *<InfoTag>*
            *<Procedure attrProc="TestProcC" attrLevel="3">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            **
            * <Procedure attrProc="TestProcD" attrLevel="4">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            *</InfoTag>*



            I am trying to fetch an output file which looks like this:

            _Output.xml_

            *<InfoTag>*
            *<Procedure attrProc="1" attrLevel="### NOT UNIQUE ###">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            * <Procedure attrProc="2" attrLevel="### NOT UNIQUE ###">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            *<Procedure attrProc="3" attrLevel="### NOT UNIQUE ###">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure>*
            * <Procedure attrProc="4" attrLevel="### NOT UNIQUE ###">*
            *      <downTime>*
            *        <acceptableDownTime>*
            *          <all/>*
            *        </acceptableDownTime>*
            *        <downTimePeriod time="600000000"/>*
            *      </downTime>*
            *    </Procedure> *
            *</InfoTag>*



            I am thinking of looping it and will count the occurrences
            of it and
            then I am thinking to replace the value. But I am not able
            to do that.
            Can anyone tell me how to achieve this. How can I loop every
            Procedure
            tag and and append the attrProc attribute value in a
            sequential order?


        How do you load the files?

        I would simply use Saxon 9 and collection, as in

        <xsl:template match="/" name="main">
           <InfoTag>
             <xsl:apply-templates
        select="collection('.?select=*__.xml')//Procedure"/>

           </InfoTag>
        </xsl:template>

        <xsl:template match="Procedure">
           <Procedure attrProc="{position()}" attrLevel="### NOT UNIQUE
        ###">
             <xsl:apply-templates/>
           </Procedure>
        </xsl:template>

        maybe with an additional <xsl:sort select="@attrLevel"/> on the
        apply-templates of the collection, depending on which ordering
        you need. If the name of the files are known then you can of
        course also do

        <xsl:template match="/" name="main">
           <InfoTag>
             <xsl:apply-templates select="for $file in
        (doc('file1.xml'), doc('file2.xml')) return $file//Procedure"/>
           </InfoTag>
        </xsl:template>

        <xsl:template match="Procedure">
           <Procedure attrProc="{position()}" attrLevel="### NOT UNIQUE
        ###">
             <xsl:apply-templates/>
           </Procedure>
        </xsl:template>


    XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
    EasyUnsubscribe <-list/1005724> (by email)


XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <-list/763474>
(by email <>)



--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>