user3872094
user3872094

Reputation: 3351

loop through files in a directory based on a condition

I've an XML like below. and it is named title.xml.

<entry>
<file name="AMR_1A.xml"></file>
<file name="AMR_1B.xml"></file>
<file name="AMR_1C.xml"></file>
<file name="AMR_1D.xml"></file>
<file name="AMR_1E.xml"></file>
<file name="AMR_1F.xml"></file>
<file name="AMR_1G.xml"></file>
<file name="AMR_1H.xml"></file>
<file name="AMR_2A.xml"></file>
<file name="AMR_2B.xml"></file>
<file name="AMR_2C.xml"></file>
<file name="AMR_2D.xml"></file>
<file name="AMR_2E.xml"></file>
<file name="AMR_2F.xml"></file>
<file name="AMR_2G.xml"></file>
</entry>

and The files placed in a folder where this title.xml file is placed. And I've the below files also

idx_1.xml
idx_2.xml

here i'm trying to do the below.

first open idx_X.xml and using XSLT, i want to loop through files with reference to my title.xml. The condition is the looping should be done only on the files where the number matches the number in filename, i.e. translate all the text in title.xml file and the idx file, and if they match, then it should be looped only through those files, The xslt i tried is as below. But it is not working.

<xsl:for-each select="document('C:\Users\u0138039\Desktop\MY\2014All\XML\title.xml')/entry/file[translate(translate(replace(@name,'[^a-zA-Z]', ''),'.xml',''),'_','') = translate(translate(replace(base-uri(),'[^a-zA-Z]', ''),'.xml',''),'_','')]">
                    <xsl:value-of select="translate(normalize-space(replace(base-uri(document(concat('C:\Users\u0138039\Desktop\MY\2014All\XML\',./@name))//case[//star.page=regex-group(1)]), '^.*/', '')),'.xml','')"/>
                </xsl:for-each>

please let me know how can i fix this.

Thanks

Upvotes: 2

Views: 1823

Answers (1)

Kokkie
Kokkie

Reputation: 556

Below will give you the unique numbers used in your title.xml and translate them as required.
Maybe you can use that to build on further.

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">  
        <xsl:for-each-group select="/entry/file" group-by="replace(@name,'(^.*_)([0-9])([a-zA-Z])(.xml$)', 'idx$2$4')">
            <xsl:value-of select="replace(@name,'(^.*_)([0-9])([a-zA-Z])(.xml$)', 'idx$2$4')"/>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

Also see if you can do without title.xml and use all the files with a name like "idx_**.xml", see below example.

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="collection(iri-to-uri('file:///c:/Temp/XML/?select=idx_[0-9]+.xml'))">
            <xsl:value-of select="entry/file/@name"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions