check for existence of xml file in different folde

2019-09-06 16:30发布

I am trying to check if a file exists in any of the preceding folders and if not, write that filename to the output:

So my folder structure looks like this:

FOLDER_A
a.xml
b.xml
c.xml
d.xml

FOLDER_B
c.xml
d.xml
e.xml

FOLDER_C
d.xml
e.xml
f.xml
g.xml

FOLDER_D
e.xml
f.xml
g.xml

Desired output:

<files>
<file>a.xml</file>
<file>b.xml</file>
<file>c.xml</file>
<file>d.xml</file>
<file>e.xml</file>
<file>f.xml</file>
<file>g.xml</file>
</files>

My attempt so far:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" standalone="yes"/>
    <xsl:template match="/">
        <files>
            <xsl:apply-templates/>
        </files>
    </xsl:template>

    <xsl:template match="folders">
        <xsl:for-each select="folder">
            <xsl:variable name="numberOfFolders" select="count(preceding-sibling::folder)"/>
            <xsl:variable name="nameOfPrecedingFolder" select="preceding-sibling::folder/@name"/>
            <xsl:variable name="string" select="iri-to-uri(concat(@name, '/?select=*.xml'))"/>
            <xsl:variable name="input" select="collection($string)"/>

            <xsl:for-each select="$input">
                <xsl:choose>
                    <xsl:when test="$numberOfFolders = 0">
                        <file>
                            <xsl:value-of select="tokenize(document-uri(.), '/')[last()]"/>
                        </file>
                    </xsl:when>
                    <xsl:otherwise>

                        <xsl:variable name="currInput" select="tokenize(document-uri(.), '/')[last()]"/>
                        <xsl:variable name="currFolder" select="tokenize(document-uri(.), '/')[last()-1]"/>

                        <xsl:for-each select="$nameOfPrecedingFolder">
                        <xsl:choose>
                            <xsl:when test="doc-available(iri-to-uri(concat(.,'/',$currInput))) = fn:false()"></xsl:when>
                            <xsl:otherwise><file><xsl:value-of select="$currInput"/></file></xsl:otherwise>
                        </xsl:choose>
                        </xsl:for-each>

                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

The problem that I have is that I dont know how to code the OR-condition. Right now, it checks each folder individually an if the file doesnt exist in that folder, it writes it to the output, regardless of whether it already exists in another previous folder.

How can I get it to consider all of the previous folders and not write the file to the output if that files exists in any one of those folders?

Help and tips highly appreciated!!

标签: xml xslt
0条回答
登录 后发表回答