Vlax
Vlax

Reputation: 1537

xpath 2.0: filter sequence using another sequence

I'm trying to filter a sequence by using another sequence in a predicate:

Here my xpath:

doc('files.xml')/files/file[path = //reference]

here the xml files:

files.xml

<?xml version="1.0" encoding="UTF-8"?>
<files>
    <file>
        <path>d0002/000000338179.pdf</path>
        <file>
            <path>d0002/000000338179.JPG</path>
        </file>
    </file>
    <file>
        <path>d0002/000000341922.pdf</path>
    </file>
    <file>
        <path>d0002/000000342768.pdf</path>
    </file>
</files>

references

<?xml version="1.0" encoding="UTF-8"?>
<references>
    <reference>d0002/000000338179.pdf</reference>
    <reference>d0002/000000341922.pdf</reference>
</references>

I can't get it to work, any hint is greatly appreciated. Vlax

EDIT

based on the answer from @Jirka I came to a "pure" XPath expression:

for $file in doc('files.xml')/files/file,
$ref in //reference
return $file[path = $ref]/path

Upvotes: 0

Views: 383

Answers (1)

Jirka Š.
Jirka Š.

Reputation: 3428

Probably context is changing there. So I tried use a variable and it seems to be working.

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

    <xsl:variable name="refs" select="//reference" />
    <xsl:variable name="files" select="doc('files.xml')/files/file[path = $refs]" />

    <xsl:template match="/">
        <files>
            <xsl:copy-of select="$files" />     
        </files>
    </xsl:template>

</xsl:stylesheet>

This xslt gives a result

<?xml version="1.0" encoding="UTF-8"?>
<files xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <file>
        <path>d0002/000000338179.pdf</path>
        <file>
            <path>d0002/000000338179.JPG</path>
        </file>
    </file>
    <file>
        <path>d0002/000000341922.pdf</path>
    </file>
</files>

Upvotes: 0

Related Questions