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

Subject: Re: [xsl] Replacing the element's attribute in sequential order
From: "varun bhatnagar varun292006@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 1 Aug 2014 09:29:46 -0000
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.honnen@xxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> varun bhatnagar varun292006@xxxxxxxxx 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>

Current Thread