Searching files with XSLT
Here should go questions about transforming XML with XSLT and FOP.
Searching files with XSLT
Hello,
i have some xml files containing newspaper articles. Each xml has different tags but the most important is the tag <Texte> with the article itself (the other tags describe other information like the date, the title etc). There's an exemple of <Texte> (the texte is in french):
<Texte>
<P>Sur l' Hermione, les affûts de canon étaient peints en rouge pour faciliter le nettoyage du sang des hommes après la bataille. La « frégate de douze » était armée de 26 canons de douze (les boulets pèsent 6 kg) et 6 canons de six (boulets de 3 kg). Elle était beaucoup plus légère, rapide et maniable qu'un vaisseau taillé pour le combat avec 118 canons. A bord, l'eau est rationnée à trois pintes par homme et par jour. Les vers et les charançons infestent les biscuits de mer. L'absence de fruits et légumes frais rend le scorbut ravageur. La fièvre typhoïde, la petite vérole et la gangrène sont des maladies fréquentes. L'hygiène est absente, le sommeil mauvais. Deux matelots alternent dans un hamac, souvent trempé, à l'entrepont, espace confiné où vivent aussi les moutons embarqués vivants. Le capitaine prend soin de sa chair à canon comme d'un cheptel : il lui faut assez d'hommes vivants pour livrer combat. A cette époque, le service dans la marine est obligatoire - un an sur trois - dans les provinces maritimes du royaume. </P>
<P/>
</Texte>
So, i have the following problem. Can i search specific words? I want to search among all files, in the tag <Texte>, if exist the words provinces, deux, combat for example. What's the xpath for this? For a single word can i use the following? What about 3 or 4 words?
<xsl:if test="document(.)/Document/Article[1]/Texte = 'combat'">
Thank you for the help
i have some xml files containing newspaper articles. Each xml has different tags but the most important is the tag <Texte> with the article itself (the other tags describe other information like the date, the title etc). There's an exemple of <Texte> (the texte is in french):
<Texte>
<P>Sur l' Hermione, les affûts de canon étaient peints en rouge pour faciliter le nettoyage du sang des hommes après la bataille. La « frégate de douze » était armée de 26 canons de douze (les boulets pèsent 6 kg) et 6 canons de six (boulets de 3 kg). Elle était beaucoup plus légère, rapide et maniable qu'un vaisseau taillé pour le combat avec 118 canons. A bord, l'eau est rationnée à trois pintes par homme et par jour. Les vers et les charançons infestent les biscuits de mer. L'absence de fruits et légumes frais rend le scorbut ravageur. La fièvre typhoïde, la petite vérole et la gangrène sont des maladies fréquentes. L'hygiène est absente, le sommeil mauvais. Deux matelots alternent dans un hamac, souvent trempé, à l'entrepont, espace confiné où vivent aussi les moutons embarqués vivants. Le capitaine prend soin de sa chair à canon comme d'un cheptel : il lui faut assez d'hommes vivants pour livrer combat. A cette époque, le service dans la marine est obligatoire - un an sur trois - dans les provinces maritimes du royaume. </P>
<P/>
</Texte>
So, i have the following problem. Can i search specific words? I want to search among all files, in the tag <Texte>, if exist the words provinces, deux, combat for example. What's the xpath for this? For a single word can i use the following? What about 3 or 4 words?
<xsl:if test="document(.)/Document/Article[1]/Texte = 'combat'">
Thank you for the help
Re: Searching files with XSLT
You can use a predicate to test any number of conditions. Inside that you can use and, or, not to compose your logic. Please see a complete working sample below:
Best Regards,
George
Code: Select all
<Document>
<Article>
<Texte>
<P>Sur l' Hermione, les affûts de canon étaient peints en rouge pour faciliter le
nettoyage du sang des hommes après la bataille. La « frégate de douze » était armée de 26
canons de douze (les boulets pèsent 6 kg) et 6 canons de six (boulets de 3 kg). Elle était
beaucoup plus légère, rapide et maniable qu'un vaisseau taillé pour le combat avec 118
canons. A bord, l'eau est rationnée à trois pintes par homme et par jour. Les vers et
les charançons infestent les biscuits de mer. L'absence de fruits et légumes frais rend
le scorbut ravageur. La fièvre typhoïde, la petite vérole et la gangrène sont des maladies
fréquentes. L'hygiène est absente, le sommeil mauvais. Deux matelots alternent dans un
hamac, souvent trempé, à l'entrepont, espace confiné où vivent aussi les moutons
embarqués vivants. Le capitaine prend soin de sa chair à canon comme d'un cheptel : il
lui faut assez d'hommes vivants pour livrer combat. A cette époque, le service dans la
marine est obligatoire - un an sur trois - dans les provinces maritimes du royaume. </P>
<P/>
</Texte>
</Article>
</Document>
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Texte">
<xsl:if test="/Document/Article[1]/Texte[
contains(.,'combat') and
contains(.,'provinces') and
contains(.,'Deux')
]">
Got it!
</xsl:if>
</xsl:template>
</xsl:stylesheet>
George
George Cristian Bina
Re: Searching files with XSLT
So... that works perfectly, thanks! I have two questions if you can help me:
- The location of the tag <Texte> isn't always the same /Document/Article[1]/Texte in all files. So, can i use //Texte making the research sure?
- I wrote the following scenario in order to copy the Xml files that contain these sepcific words:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="file">
<xsl:if test="document(.)/Document/Article[1]/Texte[
contains(.,'combat') and
contains(.,'provinces') and
contains(.,'Deux')
]">
<xsl:result-document href="SCI/{@name}">
<xsl:copy-of select="document(.)" />
</xsl:result-document>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I make a copy of the whole document with
<xsl:copy-of select="document(.)" /> . But i want also make an export to texte, not for the whole document but only for the tag <Texte> (only its contents). How?
- How can i add an xslt expression that deletes the original file after copying it?
Thank you very much
Giannis
- The location of the tag <Texte> isn't always the same /Document/Article[1]/Texte in all files. So, can i use //Texte making the research sure?
- I wrote the following scenario in order to copy the Xml files that contain these sepcific words:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="file">
<xsl:if test="document(.)/Document/Article[1]/Texte[
contains(.,'combat') and
contains(.,'provinces') and
contains(.,'Deux')
]">
<xsl:result-document href="SCI/{@name}">
<xsl:copy-of select="document(.)" />
</xsl:result-document>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I make a copy of the whole document with
<xsl:copy-of select="document(.)" /> . But i want also make an export to texte, not for the whole document but only for the tag <Texte> (only its contents). How?
- How can i add an xslt expression that deletes the original file after copying it?
Thank you very much
Giannis
Re: Searching files with XSLT
Hi,
You can add more steps after the document function to select whatever you want from that document:
document('test.xml')//someElement[1]
The file system is not part of the XSLT data model so you cannot delete files from XSLT. It may be possible however to use/write an extension function to do that, check your XSLT processor documentation for the available extensions and/or how to create an extension.
Best Regards,
George
You can add more steps after the document function to select whatever you want from that document:
document('test.xml')//someElement[1]
The file system is not part of the XSLT data model so you cannot delete files from XSLT. It may be possible however to use/write an extension function to do that, check your XSLT processor documentation for the available extensions and/or how to create an extension.
Best Regards,
George
George Cristian Bina
Re: Searching files with XSLT
Hi again! What is the syntaxe for not? If i don't want a specific word inside? I tried some things but it doesn't work
<xsl:if test="/Document/Article[1]/Texte[
contains(.,'combat') and
contains(.,'provinces') and
contains(.,'Deux')
]">
where am i supposed to use ''not''? thanks!
<xsl:if test="/Document/Article[1]/Texte[
contains(.,'combat') and
contains(.,'provinces') and
contains(.,'Deux')
]">
where am i supposed to use ''not''? thanks!
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