Searching files with XSLT

Here should go questions about transforming XML with XSLT and FOP.
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Searching files with XSLT

Post by crult »

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&apos; 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&apos;un vaisseau taillé pour le combat avec 118 canons. A bord, l&apos;eau est rationnée à trois pintes par homme et par jour. Les vers et les charançons infestent les biscuits de mer. L&apos;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&apos;hygiène est absente, le sommeil mauvais. Deux matelots alternent dans un hamac, souvent trempé, à l&apos;entrepont, espace confiné où vivent aussi les moutons embarqués vivants. Le capitaine prend soin de sa chair à canon comme d&apos;un cheptel : il lui faut assez d&apos;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
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Searching files with XSLT

Post by george »

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:

Code: Select all


<Document>
<Article>
<Texte>
<P>Sur l&apos; 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&apos;un vaisseau taillé pour le combat avec 118
canons. A bord, l&apos;eau est rationnée à trois pintes par homme et par jour. Les vers et
les charançons infestent les biscuits de mer. L&apos;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&apos;hygiène est absente, le sommeil mauvais. Deux matelots alternent dans un
hamac, souvent trempé, à l&apos;entrepont, espace confiné où vivent aussi les moutons
embarqués vivants. Le capitaine prend soin de sa chair à canon comme d&apos;un cheptel : il
lui faut assez d&apos;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>
Best Regards,
George
George Cristian Bina
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Re: Searching files with XSLT

Post by crult »

Hi,

i'm very interested in your solution. I have to test it today and i'll give you the results :D

Thank you very much!
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Re: Searching files with XSLT

Post by crult »

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
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Searching files with XSLT

Post by george »

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
George Cristian Bina
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Re: Searching files with XSLT

Post by crult »

Ok, i have now some useful information to continue the job! Thanks for the support.

Have a nice day

Giannis
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Re: Searching files with XSLT

Post by crult »

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!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Searching files with XSLT

Post by george »

You can find that in any XSLT documentation, including the specification.
For example:
not(contains(.,'combat'))

Best Regards,
George
George Cristian Bina
crult
Posts: 20
Joined: Thu Jan 21, 2010 10:21 pm

Re: Searching files with XSLT

Post by crult »

Hello,

i have to see the XSLT documentation, have a nice day, thank you :)
Post Reply