hoping for help with xsl search loop
Here should go questions about transforming XML with XSLT and FOP.
-
- 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!
Any suggestions? Clearly I am an XSL novice. Thanks for your patience.
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>
-
- Posts: 9431
- Joined: Fri Jul 09, 2004 5:18 pm
Re: hoping for help with xsl search loop
Hi,
Did not test this but maybe you can use something like:
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
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>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- 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.
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=" "/><xsl:text> </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=" "/>-->
<!-- 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>
-
- Posts: 9431
- Joined: Fri Jul 09, 2004 5:18 pm
Re: hoping for help with xsl search loop
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
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
<oXygen/> XML Editor
http://www.oxygenxml.com
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service