Bonjour
Je me retrouve confronté a un probléme dans un script python :

Alors que j'arrive sans difficultés a Parser du xml dans une partie de mon code avec un Namespace...
Une autre recherche avec les mêmes balises et les mêmes Namespaces, plus bas dans mon code, eux ne fonctionnent pas !!!

Precision : j'utilise le module etree de LXML
Les Namespace utilisés sont :
http://www.madcapsoftware.com/Schemas/MadCap.xsd
http://www.w3.org/1999/xhtml
Le premier POUR LE CAS QUI FONCTIONNE
Les deux POUR LE CAS QUI NE FONCTIONNE PAS

Voici mon code qui fonctionne:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
treeSnp = etree.parse(file_to_read)
 
for itemSnp in treeSnp.findall('.//{http://www.madcapsoftware.com/Schemas/MadCap.xsd}snippetText'): 
    # Recovering snippets links from snippets files
    snp_to_read = pathProject + "Content/" + get_path(itemSnp.get("src")) 
 
    print("In snippet for snp text links : " + snp_to_read)
 
    if os.path.isfile(snp_to_read)
        print("In main : snp to create from htm : " + pathMiniProject + "Content/" + get_path(itemSnp.get("src")) + "\n")
        list_snp_to_create.append(itemSnp.get("src"))
 
    else:
        print("Le fichier " + snp_to_read + " n'existe pas.\n")
Voici le code qui ne fonctionne pas :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
treeLayout = etree.parse(layout_to_read)
for itemSnp in treeLayout.findall('.//{http://www.madcapsoftware.com/Schemas/MadCap.xsd}snippetBlock'): 
 
    if itemSnp.get("xhtml:src") != None:
        snp_to_read = pathProject + "Content/" + get_path(str(itemSnp.get("xhtml:src"))) 
        print("In layout for snp block links : " + snp_to_read + "\n")
 
        if os.path.isfile(snp_to_read):
            print("In layout : snp to create from layout : " + pathMiniProject + "Content/Ressources/Snippets/" + get_path(str(itemSnp.get("xhtml:src"))) + "\n")
            snp_to_create = pathMiniProject + "Content/Ressources/Snippets/" + str(itemSnp.get("xhtml:src"))
 
        else:
            print("\nLe fichier " + snp_to_read + " n'existe pas.")
Le premier code fonctionnel dans mon cas Parse un fichier Xml, pour lequel le Namespace est déclaré en debut de fichier
Pour le second c'est la meme procédure (Parsing d'un Xml), mais le Namespace n'est pas déclaré au début mais a l'interieur de la balise "snippetBlock"... Serais-ce le probléme ???


Si vous avez une idée, merci pour votre aide.
Cdt
Jean-Pierre