hoping for help with xsl search loop

Here should go questions about transforming XML with XSLT and FOP.
xsl_student
Posts: 4
Joined: Thu Oct 04, 2018 3:58 am

hoping for help with xsl search loop

Post by xsl_student »

I have a tokenized variable that contains list of filenames from a .txt of a directory listing. I want to look for those filenames in a number of xml files in a number of subdirectories. If the filename is found, I want to output that "filename" was found in "xmlfile".

There are a lot of xml directories and they are not static. Same with xml files. The filenames are not tagged in the xml, so I'm just looking for their plain text occurence in the file.

I'm using an academic version of Oxygen XML so I think I have Saxon through that and I have the standalone Saxon file for running this from the command line.

I've gotten this far, which doesn't work. I know it's broken, but I don't know how to fix it!

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:h="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xs"
version="3.0"
expand-text="yes"
>

<xsl:variable name="filenames_from_directory_listing" as="xs:string" select="unparsed-text('filenames_from_directory_listing.txt')"/>
<xsl:variable name="filenames_to_find" select="tokenize($filenames_from_directory_listing, '\s+')"/>

<xsl:template match="/">
<xsl:for-each select="collection('.?select=*.xml;recurse=yes')"/>
<xsl:variable name="xml_filenames" select="."/>
<xsl:for-each select="$filenames_to_find">
<xsl:if test="(contains($t, .))">
<xsl:message>{document-uri($xml_filenames)} contains {.}</xsl:message>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Any suggestions? Clearly I am an XSL novice. Thanks for your patience.
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: hoping for help with xsl search loop

Post by Radu »

Hi,

Did not test this but maybe you can use something like:

Code: Select all

    <xsl:template match="/">
<xsl:for-each select="collection('.?select=*.xml;recurse=yes')">
<!-- For each loaded XML document -->
<xsl:variable name="xmlDoc" select="."/>
<xsl:for-each select="$filenames_to_find">
<!-- For each file name to find -->
<xsl:variable name="fileNameToFind" select="."/>
<!-- Look in all text nodes from the xml document and see if one of them contains the file name -->
<xsl:if test="($xmlDoc//text()[contains(., $fileNameToFind)])[1]">
<xsl:message>{document-uri($xmlDoc)} contains {.}</xsl:message>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Remember that you can use xsl:messages anywhere in order to know what values certain variables have or what the current context inside the for-each instruction is.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
xsl_student
Posts: 4
Joined: Thu Oct 04, 2018 3:58 am

Re: hoping for help with xsl search loop

Post by xsl_student »

Thanks for your help, Radu.

Your solution validates and runs, but doesn't display any results. I checked the variables along the way and they seem (to me at least) to show the contents of the xml files, and the filenames read from the .txt that I am searching for, but it does not return any results. I know that the filenames I am searching for do exist (I manually checked).

Any further advice appreciated - my example included below.

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:h="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xs"
version="3.0"
expand-text="yes"
>

<xsl:variable name="filenames_from_directory_listing" as="xs:string" select="unparsed-text('filenames_from_directory_listing.txt')"/>
<xsl:variable name="filenames_to_find" select="tokenize($filenames_from_directory_listing, '\s+')"/>

<xsl:template match="/">
<xsl:for-each select="collection('.?select=*.xml;recurse=yes')">
<!-- For each loaded XML document -->
<xsl:variable name="xmlDoc" select="."/>
<xsl:for-each select="$filenames_to_find">
<!--display what $filenames_to_find is for troubleshooting-->
<!--<xsl:value-of select="$filenames_to_find" separator="&#10;"/><xsl:text>&#10;&#10;</xsl:text>-->

<!-- For each file name to find -->
<xsl:variable name="fileNameToFind" select="."/>
<!--display what $fileNameToFind_to_find is for troubleshooting-->
<!--<xsl:value-of select="$fileNameToFind" separator="&#10;"/>-->

<!-- Look in all text nodes from the xml document and see if one of them contains the file name -->
<xsl:if test="($xmlDoc//text()[contains(., $fileNameToFind)])[1]">
<xsl:message>{document-uri($xmlDoc)} contains {.}</xsl:message>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: hoping for help with xsl search loop

Post by Radu »

Hi,

As you are using xsl:messages, they do not return anything in the produced output file, when running the transformation Oxygen presents xsl:messages in a separate tab located at the bottom of the bottom area.
You will have to add extra xsl:messages and try to figure this out for yourself.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply