Ankündigung

Einklappen
Keine Ankündigung bisher.

Umlaut in XSLT-Transformation (if)

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    Umlaut in XSLT-Transformation (if)

    Hallo openHab-Mitstreiter,

    ich habe seit heute ein Miele@Home-Gerät und möchte dies gerne mittels HTTP-Binding und XSLT-Übersetzung anbinden. Dies klappt auch prinzipiell. Allerdings brauche ich in meiner Bedingung für einige Tags auch Umlaute und hier klappt es dann nicht mehr! Ich habe Versucht den Wert erst als Variable zu speichern und dann den Test gegen die Variable durchzuführen und ein HTML-Escape (ä=ä ) führte auch nicht zum Erfolg.

    Kann mir bitte jemand einen Tipp geben, wie ich Umlaute escapen muss:

    Nicht funktioniert:
    Code:
    [I]<?[/I][COLOR=#0000ff][B]xml version[/B][/COLOR][COLOR=#008000][B]="1.0" [/B][/COLOR][COLOR=#0000ff][B]encoding[/B][/COLOR][COLOR=#008000][B]="utf-8" [/B][/COLOR][I]?>
    [/I]<[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:stylesheet [/B][/COLOR][COLOR=#0000ff][B]version[/B][/COLOR][COLOR=#008000][B]="1.0" [/B][/COLOR][COLOR=#0000ff][B]xmlns:[/B][/COLOR][COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#008000][B]="http://www.w3.org/1999/XSL/Transform"[/B][/COLOR]>
        <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:output [/B][/COLOR][COLOR=#0000ff][B]indent[/B][/COLOR][COLOR=#008000][B]="yes" [/B][/COLOR][COLOR=#0000ff][B]method[/B][/COLOR][COLOR=#008000][B]="xml" [/B][/COLOR][COLOR=#0000ff][B]encoding[/B][/COLOR][COLOR=#008000][B]="UTF-8" [/B][/COLOR][COLOR=#0000ff][B]omit-xml-declaration[/B][/COLOR][COLOR=#008000][B]="yes"[/B][/COLOR]/>
        <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:variable [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="geraet"[/B][/COLOR]>Gerät</[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:variable[/B][/COLOR]>
        <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:template [/B][/COLOR][COLOR=#0000ff][B]match[/B][/COLOR][COLOR=#008000][B]="device/information/key"[/B][/COLOR]>
            <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:if [/B][/COLOR][COLOR=#0000ff][B]test[/B][/COLOR][COLOR=#008000][B]="@name='[/B][/COLOR][COLOR=#008000][B]Gerät'"[/B][/COLOR]> <!-- klappt nicht -->
                <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:value-of [/B][/COLOR][COLOR=#0000ff][B]select[/B][/COLOR][COLOR=#008000][B]="@value"[/B][/COLOR]/>
            </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:if[/B][/COLOR]>
        </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:template[/B][/COLOR]>
    </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:stylesheet[/B][/COLOR]>
    Funktioniert:
    Code:
    [I]<?[/I][COLOR=#0000ff][B]xml version[/B][/COLOR][COLOR=#008000][B]="1.0" [/B][/COLOR][COLOR=#0000ff][B]encoding[/B][/COLOR][COLOR=#008000][B]="utf-8" [/B][/COLOR][I]?>
    [/I]<[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:stylesheet [/B][/COLOR][COLOR=#0000ff][B]version[/B][/COLOR][COLOR=#008000][B]="1.0" [/B][/COLOR][COLOR=#0000ff][B]xmlns:[/B][/COLOR][COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#008000][B]="http://www.w3.org/1999/XSL/Transform"[/B][/COLOR]>
        <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:output [/B][/COLOR][COLOR=#0000ff][B]indent[/B][/COLOR][COLOR=#008000][B]="yes" [/B][/COLOR][COLOR=#0000ff][B]method[/B][/COLOR][COLOR=#008000][B]="xml" [/B][/COLOR][COLOR=#0000ff][B]encoding[/B][/COLOR][COLOR=#008000][B]="UTF-8" [/B][/COLOR][COLOR=#0000ff][B]omit-xml-declaration[/B][/COLOR][COLOR=#008000][B]="yes"[/B][/COLOR]/>
        <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:template [/B][/COLOR][COLOR=#0000ff][B]match[/B][/COLOR][COLOR=#008000][B]="device/information/key"[/B][/COLOR]>
            <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:if [/B][/COLOR][COLOR=#0000ff][B]test[/B][/COLOR][COLOR=#008000][B]="@name='Dauer'"[/B][/COLOR]> <!-- funktioniert! -->
                <[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:value-of [/B][/COLOR][COLOR=#0000ff][B]select[/B][/COLOR][COLOR=#008000][B]="@value"[/B][/COLOR]/>
            </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:if[/B][/COLOR]>
        </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:template[/B][/COLOR]>
    </[COLOR=#660e7a][B]xsl[/B][/COLOR][COLOR=#000080][B]:stylesheet[/B][/COLOR]>
    Antwort des Gateways:
    Code:
    [I]<?[/I][COLOR=#0000ff][B]xml version[/B][/COLOR][COLOR=#008000][B]="1.0" [/B][/COLOR][COLOR=#0000ff][B]encoding[/B][/COLOR][COLOR=#008000][B]="UTF-8"[/B][/COLOR][I]?>
    [/I]<[COLOR=#000080][B]device[/B][/COLOR]>
        <[COLOR=#000080][B]information[/B][/COLOR]>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Gerät" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="Backofen"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Gerätestatus" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="Aus"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Programm" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="0"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Phase" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="0"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="measuredTemperature" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="32768"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="deviceTemperature2" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]=""[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Startzeit" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="0"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Dauer" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="0"[/B][/COLOR]/>
            <[COLOR=#000080][B]key [/B][/COLOR][COLOR=#0000ff][B]name[/B][/COLOR][COLOR=#008000][B]="Endzeit" [/B][/COLOR][COLOR=#0000ff][B]value[/B][/COLOR][COLOR=#008000][B]="0"[/B][/COLOR]/>
        </[COLOR=#000080][B]information[/B][/COLOR]>
        <[COLOR=#000080][B]actions[/B][/COLOR]></[COLOR=#000080][B]actions[/B][/COLOR]>
    </[COLOR=#000080][B]device[/B][/COLOR]>
    Viele Grüße,
    Dirk

    #2
    Hallo Dirk,

    seltsam, zumindest mein Firefox rendert auch dein nicht-funktionierendes Beispiel korrekt und zeigt "Backofen" an. Scheint also ein Problem mit der Java-internen XSLT-Engine zu sein...

    Wenn deine Gateway-Antwort tatsächlich von der Form her statisch ist, könntest du für den Moment auch folgendes nehmen:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
        <xsl:template match="/device/information">
            <xsl:value-of select="key[position() = 1]/@value"/>
        </xsl:template>
    </xsl:stylesheet>
    Damit wird dann eben immer das erste key-Element ausgewählt. Ist nicht schön, aber sollte auf jeden Fall funktionieren...

    Vielleicht hat ja jemand anders eine Idee, warum das mit dem Umlaut nicht funktioniert.

    Gruß,
    Alex

    Kommentar


      #3
      Hallo Dirk,

      versuch mal, in der xsl Datei

      <?xml version="1.0" encoding="utf-8" ?>

      durch

      <?xml version="1.0" encoding="iso-8859-1" ?>

      zu ersetzen.

      Klingt sehr nach Encoding.

      Grüße,
      Flo

      Kommentar


        #4
        Zitat von LexionTT Beitrag anzeigen
        Hallo Dirk,

        versuch mal, in der xsl Datei

        <?xml version="1.0" encoding="utf-8" ?>

        durch

        <?xml version="1.0" encoding="iso-8859-1" ?>

        zu ersetzen.

        Klingt sehr nach Encoding.

        Grüße,
        Flo
        Hallo Flo,

        leider hat die Änderung nichts an dem Problem geändert. Allerdings kommt nun eine Fehlermeldung, die ich nicht so ganz verstehe:

        Code:
        2015-02-19 00:37:47.693 [ERROR] [.o.b.http.internal.HttpBinding] - transformation throws exception [transformation=XSLT(miele_backofen.xsl), response=<?xml version="1.0" encoding="UTF-8"?>
        <device><information><key name="Ger??t" value="Backofen" /><key name="Ger??testatus" value="Aus" /><key name="Programm" value="0" /><key name="Phase" value="0" /><key name="measuredTemperature" value="32768" /><key name="deviceTemperature2" value="" /><key name="Startzeit" value="0" /><key name="Dauer" value="0" /><key name="Endzeit" value="0" /></information><actions></actions></device>
        ]
        org.openhab.core.transform.TransformationException: transformation throws exception
            at org.openhab.core.transform.internal.service.XsltTransformationService.transform(XsltTransformationService.java:86) ~[na:na]
            at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:179) ~[na:na]
            at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) [org.openhab.core_1.6.2.jar:na]
            at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) [org.openhab.core_1.6.2.jar:na]
        Caused by: javax.xml.transform.TransformerConfigurationException: file:/etc/openhab/configurations/transform/miele_backofen.xsl: line 6: Required attribute 'test' is missing.
            at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:994) ~[na:1.8.0]
            at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:766) ~[na:1.8.0]
            at org.openhab.core.transform.internal.service.XsltTransformationService.transform(XsltTransformationService.java:82) ~[na:na]
            ... 3 common frames omitted
        Das Attribut ist ja gesetzt und testet gegen das Gerät...

        Viele Grüße,
        Dirk

        Kommentar


          #5
          Zitat von qwasyx Beitrag anzeigen
          Hallo Dirk,

          seltsam, zumindest mein Firefox rendert auch dein nicht-funktionierendes Beispiel korrekt und zeigt "Backofen" an. Scheint also ein Problem mit der Java-internen XSLT-Engine zu sein...

          Wenn deine Gateway-Antwort tatsächlich von der Form her statisch ist, könntest du für den Moment auch folgendes nehmen:
          Code:
          <?xml version="1.0" encoding="utf-8" ?>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
              <xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
              <xsl:template match="/device/information">
                  <xsl:value-of select="key[position() = 1]/@value"/>
              </xsl:template>
          </xsl:stylesheet>
          Damit wird dann eben immer das erste key-Element ausgewählt. Ist nicht schön, aber sollte auf jeden Fall funktionieren...

          Vielleicht hat ja jemand anders eine Idee, warum das mit dem Umlaut nicht funktioniert.

          Gruß,
          Alex
          Hallo Alex,

          ich gehe momentan davon aus, das die Statusmeldungen von der Struktur her statisch sind. Dazu kommen dann die dynamischen Actions in Spiel, je nach Zustand des Geräts.

          Ich habe jetzt dein Vorschlag aufgegriffen und bekomme nun den Status in Klartext übermittelt.

          Ich werde dann als nächstes mal schauen, wie ich das ganze dann noch in einen Switch konvertieren kann.

          Vielen Dank und viele Grüße,
          Dirk

          Kommentar


            #6
            Zitat von dirk0108 Beitrag anzeigen
            Fehlermeldung, die ich nicht so ganz verstehe:

            <key name="Ger??t" value="Backofen" />
            Eine Fehlermeldung, die so aussieht, verrät uns, dass deine XSLT-Datei nur behauptet, in 8859-1 kodiert zu sein, du sie aber immer noch in UTF-8 abgespeichert hast. Das kann nicht funktionieren.

            Wie sieht denn das "Original" aus, also das XML, gegen das du mit diesem XSLT matchen willst – ist darin ein Encoding angegeben? Stimmt es? Steht der Umlaut da als Byte(s) drin, oder steht da "Ger&auml;t" drin?

            Ich bin aber geneigt, der "Problem mit der Java-internen XSLT-Engine"-Theorie zuzustimmen. Welches Java verwendest du?
            DistKV, Home Assistant, 1wire, KNX, Python, Asterisk, SMD-Lötkolben

            Kommentar


              #7
              Hallo Smurf,

              bei mir ist folgende Java-Version installiert:

              Code:
              java version "1.8.0"
              Java(TM) SE Runtime Environment (build 1.8.0-b132)
              Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)
              Die Antwort des Gateways ist in UTF-8. Probleme machen leider nicht nur die Werte im xslt, sondern auch die ausgelesenen Werte sind nicht korrekt formatiert. Kommt nicht so gut an bei meiner Frau...

              Kommentar


                #8
                Moin,

                falls das noch aktuell ist meine 0,02 € dazu.

                Wenn deine Datei UTF-8 encoded ist, dann musst du nichts escapen
                für Umlaute.
                Wenn ich deine Beispiele copy & paste, dann funktionieren sie.
                Das heisst aber nicht, dass ich dann genau die Dateien habe wie du.

                Du bist nicht zufällig auf dem Mac unterwegs?

                Etwas wilde Idee:

                In Unicode gibt es mehrere Arten, einen Umlaut darzustellen,
                auf dem Bildschirm sind sie nicht zu unterscheiden.

                Die übliche Art: Ein Zeichen 'ä' (wie auch in ISO-8859-1)
                Vom Mac aber schon in Dateinamen gesehen: 'a' + " (diaresis)

                Ich weiss nicht, wie sich dass in XSLT verhält, wenn man darauf matched.

                Wenn du einen Hex-Dumper oder Hex-Editor hast, dann kannst du dir
                die Kodierung dort anschauen.

                Unter linux z.B. hexdump -C.

                Deine miele.xml sieht dann bei mir so aus, "c3 a4" ist "auml" in UTF-8.
                00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
                00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 |.0" encoding="UT|
                00000020 46 2d 38 22 3f 3e 0a 3c 64 65 76 69 63 65 3e 0a |F-8"?>.<device>.|
                00000030 20 20 20 20 3c 69 6e 66 6f 72 6d 61 74 69 6f 6e | <information|
                00000040 3e 0a 20 20 20 20 20 20 20 20 3c 6b 65 79 20 6e |>. <key n|
                00000050 61 6d 65 3d 22 47 65 72 c3 a4 74 22 20 76 61 6c |ame="Ger..t" val|
                00000060 75 65 3d 22 42 61 63 6b 6f 66 65 6e 22 2f 3e 0a |ue="Backofen"/>.|
                00000070 20 20 20 20 20 20 20 20 3c 6b 65 79 20 6e 61 6d | <key nam|
                00000080 65 3d 22 47 65 72 c3 a4 74 65 73 74 61 74 75 73 |e="Ger..testatus|
                00000090 22 20 76 61 6c 75 65 3d 22 41 75 73 22 2f 3e 0a |" value="Aus"/>.|
                000000a0 20 20 20 20 20 20 20 20 3c 6b 65 79 20 6e 61 6d | <key nam|
                000000b0 65 3d 22 50 72 6f 67 72 61 6d 6d 22 20 76 61 6c |e="Programm" val|
                000000c0 75 65 3d 22 30 22 2f 3e 0a 20 20 20 20 20 20 20 |ue="0"/>. |
                000000d0 20 3c 6b 65 79 20 6e 61 6d 65 3d 22 50 68 61 73 | <key name="Phas|
                000000e0 65 22 20 76 61 6c 75 65 3d 22 30 22 2f 3e 0a 20 |e" value="0"/>. |
                000000f0 20 20 20 20 20 20 20 3c 6b 65 79 20 6e 61 6d 65 | <key name|
                00000100 3d 22 6d 65 61 73 75 72 65 64 54 65 6d 70 65 72 |="measuredTemper|
                00000110 61 74 75 72 65 22 20 76 61 6c 75 65 3d 22 33 32 |ature" value="32|
                00000120 37 36 38 22 2f 3e 0a 20 20 20 20 20 20 20 20 3c |768"/>. <|
                00000130 6b 65 79 20 6e 61 6d 65 3d 22 64 65 76 69 63 65 |key name="device|
                00000140 54 65 6d 70 65 72 61 74 75 72 65 32 22 20 76 61 |Temperature2" va|
                00000150 6c 75 65 3d 22 22 2f 3e 0a 20 20 20 20 20 20 20 |lue=""/>. |
                00000160 20 3c 6b 65 79 20 6e 61 6d 65 3d 22 53 74 61 72 | <key name="Star|
                00000170 74 7a 65 69 74 22 20 76 61 6c 75 65 3d 22 30 22 |tzeit" value="0"|
                00000180 2f 3e 0a 20 20 20 20 20 20 20 20 3c 6b 65 79 20 |/>. <key |
                00000190 6e 61 6d 65 3d 22 44 61 75 65 72 22 20 76 61 6c |name="Dauer" val|
                000001a0 75 65 3d 22 30 22 2f 3e 0a 20 20 20 20 20 20 20 |ue="0"/>. |
                000001b0 20 3c 6b 65 79 20 6e 61 6d 65 3d 22 45 6e 64 7a | <key name="Endz|
                000001c0 65 69 74 22 20 76 61 6c 75 65 3d 22 30 22 2f 3e |eit" value="0"/>|
                000001d0 0a 20 20 20 20 3c 2f 69 6e 66 6f 72 6d 61 74 69 |. </informati|
                000001e0 6f 6e 3e 0a 20 20 20 20 3c 61 63 74 69 6f 6e 73 |on>. <actions|
                000001f0 3e 3c 2f 61 63 74 69 6f 6e 73 3e 0a 3c 2f 64 65 |></actions>.</de|
                00000200 76 69 63 65 3e 0a |vice>.|

                Kommentar

                Lädt...
                X