Home
Über mich
Veröffentlichungen

XML XML-Schema XPath XSL-T XSL-FO XQuery XProc SVG

XPath / XPath-Funktionen / XPath: Sequenz-Funktionen / XPath: unparsed-text

XPath: unparsed-text

XPath: unparsed-text

➪ Eine häufige Aufgabe ist die Auswertung von Textdateien, die im Sinn von XML nicht wohlgeformt sind, etwa CSV, JSON oder schwach strukturierte Dokumente. Die ist in XQuery sowie in XSLT 2.0 problemlos möglich.

Auf dieser Seite:

Signaturen:


unparsed-text($href as xs:string?) as xs:string?
unparsed-text($href as xs:string?, $encoding as xs:string) as xs:string?

Siehe auch:

Lesen einer Textdatei

Nehmen Sie an, Sie haben eine Textdatei, die im Sinn von XML nicht wohlgeformt ist:


'Nikki Nixlos', 'Steffi Selbstlos',38, 57, 76, 95,
114,133,152,
171,
190,'dateiende'

Nun geht es darum, aus dieser Datei sämtliche Zahlen zu addieren. Mit unparsed-text sowie tokenize, hier aufgerufen in XQuery, geht das sehr einfach:


let $datei := unparsed-text('C:/mixedcontent_testdat.txt')
let $zahlen := for $w in (tokenize($datei, ',')[. castable as xs:integer]) 
                        return xs:integer($w)
return sum($zahlen)

Das Ergebnis ist:


1026

Siehe auch parse-xml, parse-xml-fragment.

Konvertierung von CSV nach XML

Eine häufige Aufgabe ist die Konvertierung CSV nach XML. In XSLT 2.0 ist auch die Konvertierung von CSV nach XML problemlos möglich.

Nachdem zuvor die Konvertierung von XML nach CSV besprochen wurde, geht es an dieser Stelle darum, eine (dieselbe) CSV-Datei systematisch mit XSLT 2.0 einzulesen und nach XML zu konvertieren.

Für den Fall, dass Sie das Template anderweit benötigen, habe ich den Aufruf parametrisiert.


<xsl:template name="runcsv2xml">
  <xsl:call-template name="csv2xml">
    <xsl:with-param name="csvfile">file:///C:/wg/Mensch.csv</xsl:with-param>
    <xsl:with-param name="xmlfile">file:///C:/wg/Mensch.xml</xsl:with-param>
    <xsl:with-param name="zeilentrenner" select="'&#xD;&#xa;'" />
  </xsl:call-template>
</xsl:template>
<xsl:template name="csv2xml">
  <xsl:param name="csvfile" as="xs:string" />
  <xsl:param name="xmlfile" as="xs:string" />
  <xsl:param name="zeilentrenner" as="xs:string" />
  <!--liest die gesamte CSV-Datei in die Variable vinput -->
  <xsl:variable name="vinput" select="unparsed-text($csvfile)"/> 
  <!--liest die erste Zeile aus vInput, um die Spaltennamen zu ermitteln-->
  <xsl:variable name="vHeader">
    <xsl:for-each select="tokenize($vinput, $zeilentrenner)[position()=1]">
      <xsl:variable name="nzeile">
        <xsl:copy-of select="replace(., $zeilentrenner, '')"/>
      </xsl:variable>      
      <xsl:for-each select="tokenize($nzeile, ',')">
        <spalte pos="{position()}">
          <xsl:value-of select="normalize-space(replace(., '_', ''))"/>
        </spalte>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>
  <!--Generiert das XML-Dokument-->
  <xsl:result-document 
       href="{$xmlfile}" 
       omit-xml-declaration="no" 
       encoding="ISO-8859-1" method="xml">
    <CSV2XML xmlns="https://www.wilfried-grupe.de">
      <!--liest ab der zweiten Zeile alle weiteren Zeilen aus dem Input -->
      <xsl:for-each 
           select="tokenize($vinput, $zeilentrenner)[position() &gt; 1]">
        <xsl:variable name="nzeile">
          <xsl:value-of select="."/>
        </xsl:variable>        
        <!--generiert pro Zeile ein Default-Element DS-->
        <DS pos="{position()}">
          <xsl:comment>
            <xsl:value-of select="position()"/>
          </xsl:comment>
          <!--generiert pro Feld in dieser Spalte ein Child-Element von DS;
          hierzu werden die in vHeader ermittelten Feldnamen verwendet--> 
          <xsl:for-each select="tokenize($nzeile, ',')">
            <xsl:variable name="vpos" select="position()"/>
            <xsl:element name="{$vHeader/spalte[@pos=$vpos]}">
              <xsl:value-of select="."/>
            </xsl:element>            
          </xsl:for-each>
        </DS>
      </xsl:for-each>
    </CSV2XML>
  </xsl:result-document>
</xsl:template>

Das Auslesen der CSV-Datei liefert damit folgendes Ergebnis:


<?xml version="1.0" encoding="ISO-8859-1"?>
<CSV2XML xmlns="https://www.wilfried-grupe.de">
    <DS pos="1"><!--1-->
        <id>1</id>
        <name>Holzflos</name>
        <vorname>Hugo</vorname>
        <Gehalt>234.56</Gehalt>
    </DS>
    <DS pos="2"><!--2-->
        <id>4</id>
        <name>Nixlos</name>
        <vorname>Nicole</vorname>
        <Gehalt>1234.56</Gehalt>
    </DS>
    <DS pos="3"><!--3-->
        <id>9</id>
        <name>Sprachlos</name>
        <vorname>Stefan</vorname>
        <Gehalt>5430</Gehalt>
    </DS>
    <DS pos="4"><!--4-->
        <id>2</id>
        <name>Sagblos</name>
        <vorname>Stefan</vorname>
        <Gehalt>321.45</Gehalt>
    </DS>
    <DS pos="5"><!--5-->
        <id>3</id>
        <name>Sorglos</name>
        <vorname>Siggi</vorname>
        <Gehalt>987.58</Gehalt>
    </DS>
    <DS pos="6"><!--6-->
        <id>7</id>
        <name>Herzlos</name>
        <vorname>Heini</vorname>
        <Gehalt>654.21</Gehalt>
    </DS>
    <DS pos="7"><!--7-->
        <id>8</id>
        <name>Rhodos</name>
        <vorname>Rudi</vorname>
        <Gehalt>333.33</Gehalt>
    </DS>
    <DS pos="8"><!--8-->
        <id>15</id>
        <name>Kolos</name>
        <vorname>Karl</vorname>
        <Gehalt>456</Gehalt>
    </DS>
    <DS pos="9"><!--9-->
        <id>16</id>
        <name>Sinnlos</name>
        <vorname>Simone</vorname>
        <Gehalt>876.54</Gehalt>
    </DS>
    <DS pos="10"><!--10-->
        <id>17</id>
        <name>Hirnlos</name>
        <vorname>Horst</vorname>
        <Gehalt>546.77</Gehalt>
    </DS>
    <DS pos="11"><!--11-->
        <id>18</id>
        <name>Wertlos</name>
        <vorname>Werner</vorname>
        <Gehalt>777.77</Gehalt>
    </DS>
    <DS pos="12"><!--12-->
        <id>19</id>
        <name>Lustlos</name>
        <vorname>Ludwig</vorname>
        <Gehalt>357</Gehalt>
    </DS>
    <DS pos="13"><!--13-->
        <id>5</id>
        <name>Wasistlos</name>
        <vorname>Willi</vorname>
        <Gehalt>6789</Gehalt>
    </DS>
    <DS pos="14"><!--14-->
        <id>10</id>
        <name>Ruhelos</name>
        <vorname>Rita</vorname>
        <Gehalt>234</Gehalt>
    </DS>
    <DS pos="15"><!--15-->
        <id>11</id>
        <name>Schlaflos</name>
        <vorname>Susi</vorname>
        <Gehalt>321</Gehalt>
    </DS>
    <DS pos="16"><!--16-->
        <id>12</id>
        <name>Rielos</name>
        <vorname>Lotte</vorname>
        <Gehalt>456</Gehalt>
    </DS>
    <DS pos="17"><!--17-->
        <id>6</id>
        <name>Bodenlos</name>
        <vorname>Betty</vorname>
        <Gehalt>3450</Gehalt>
    </DS>
    <DS pos="18"><!--18-->
        <id>13</id>
        <name>Muehelos</name>
        <vorname>Martin</vorname>
        <Gehalt>222</Gehalt>
    </DS>
    <DS pos="19"><!--19-->
        <id>14</id>
        <name>Leinenlos</name>
        <vorname>Liane</vorname>
        <Gehalt>135</Gehalt>
    </DS>
</CSV2XML>

wg / 17. April 2021



Fragen? Anmerkungen? Tipps?

Bitte nehmen Sie Kontakt zu mir auf.






Vielen Dank für Ihr Interesse an meiner Arbeit.



V.i.S.d.P.: Wilfried Grupe * Klus 6 * 37643 Negenborn

☎ 0151. 750 360 61 * eMail: info10@wilfried-grupe.de

www.wilfried-grupe.de/unparsedtext.html