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

Subject: Re: [xsl] Replacing the element's attribute in sequential order
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 1 Aug 2014 09:18:49 -0000
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