ukodus
Goto Top

XPath Namespace - Child Nodes anzeigen

Hallo,

meine XML sieht folgend aus:

<?xml version="1.0"?>  
<!DOCTYPE profile>
<profile xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns">  
  <deploy_image>
    <image_installation config:type="boolean">false</image_installation>  
  </deploy_image>
  <general>
    <ask-list config:type="list"/>  
    <mode/>
    <mouse/>
    <proposals config:type="list"/>  
    <signature-handling/>
    <storage/>
  </general>
  <timezone>
    <hwclock>UTC</hwclock>
    <timezone>Asia/Anadyr</timezone>
  </timezone>
</profile>

Jetzt wollte ich in Linux mit xmllint alle Element-Namen (deploy_image, image_installation, general,...) rekursiv auslesen & in eine Datenbank schreiben. (Muss Shell-Script sein!)

Jetzt hab ich nur ein Problem mit XPath.
Pfade wie /profile/deploy_image gehen nicht, vermutlich liegt es am namespace.

Nur über so grässliche Xpath-Codes wie: child::*/child::* kann ich 3 Nodes sehen. Was ich auch schon probiert habe: child::*[local-name() = 'profile']
-> Aber child-Nodes kann ich damit auch nicht anschauen face-sad

Daher meine Frage: Wie kann ich mir von einem beliebigen Pfad alle Child-Nodes anzeigen.

child::*/child::timezone oder child::*/timezone::child klappt leider nicht

Content-Key: 296010

Url: https://administrator.de/contentid/296010

Printed on: April 24, 2024 at 11:04 o'clock

Mitglied: 126919
126919 Feb 12, 2016 at 09:31:37 (UTC)
Goto Top
Mitglied: 114757
114757 Feb 12, 2016 updated at 11:37:07 (UTC)
Goto Top
xmllint --shell demo.xml
set ns x=http://www.suse.com/1.0/yast2ns
xpath /x:profile//*
Ausgabe:
Object is a Node Set :
Set contains 12 nodes:
1  ELEMENT deploy_image
2  ELEMENT image_installation
    ATTRIBUTE type
      TEXT
        content=boolean
3  ELEMENT general
4  ELEMENT ask-list
    ATTRIBUTE type
      TEXT
        content=list
5  ELEMENT mode
6  ELEMENT mouse
7  ELEMENT proposals
    ATTRIBUTE type
      TEXT
        content=list
8  ELEMENT signature-handling
9  ELEMENT storage
10  ELEMENT timezone
11  ELEMENT hwclock
12  ELEMENT timezone
Geht aber mit Perl oder Python-Skript in der Bash wesentlich komfortabler.
#!/usr/bin/perl 
use XML::LibXML; 
my $dom = XML::LibXML->new->parse_file('demo.xml');  

sub parsenodes {
        my $node = shift;
        if ($node->nodeType == 1){ 
                print "Element: ",$node->nodeName,"\n";  
        }
        for my $child ($node->childNodes) {
                parsenodes($child);
        }
}
parsenodes($dom->documentElement);
Gruß jodel32