Frage

Ich habe eine XML wie folgt:

<?xml version="1.0" encoding="utf-8"?>
  <GetSavedReportResponse>
  <ResponseType>Success</ResponseType>
  <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
  <FileSizeBytes>7816</FileSizeBytes>
  <FileDataFormat>XML</FileDataFormat>
  <FileData>
    <Zthes>
      <term>
        <termId>49555</termId>
        <termUpdate>add</termUpdate>
        <termName>Active Personnel</termName>
        <termVocabulary>People Status Global</termVocabulary>
        <termVocabulary>Global People Status</termVocabulary>
        <termCategory>PDA</termCategory>
        <termCategory>PDI</termCategory>
        <termCategory>GLB</termCategory>
        <relation weight="100">
          <termId>49556</termId>
          <relationType>EQ</relationType>
          <termName>term name</termName>
          <termVocabulary>term vocabulary</termVocabulary>
        </relation>
        <relation weight="100">
          <termId>49557</termId>
          <relationType>BT</relationType>
          <termName>General Active Personnel</termName>
          <termVocabulary>People Status Global Updated</termVocabulary>
        </relation>
      </term>
      <term>
        <termId>49556</termId>
        <termUpdate>add</termUpdate>
        <termName>Leave of Absence Personnel</termName>
        <termVocabulary>People Status Global</termVocabulary>
        <termCategory>GLB</termCategory>
        <termCategory>PDI</termCategory>
        <relation weight="100">
          <relationType>BT</relationType>
          <termId>49554</termId>
          <termName>General Non-Active Personnel</termName>
          <termVocabulary>People Status Global</termVocabulary>
        </relation>
      </term>
    </Zthes>
  </FileData>
</GetSavedReportResponse>

Ich muss es in eine flache Datei umwandeln. Dafür habe ich folgendes xsl geschrieben

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" />
  <xsl:template match="Zthes">
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each select="term">
      <xsl:text>"</xsl:text>
      <xsl:text>GL</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="termCategory">
        <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="termVocabulary">
        <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>;</xsl:text>
      <xsl:text>"</xsl:text>
      <xsl:for-each select="relation/termVocabulary">
          <xsl:value-of select="." />
      </xsl:for-each>
      <xsl:text>"</xsl:text>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

also sollte die Ausgabe sein
"HDR"; "Text"; "20120112045620"; "F"
"GL"; "PDA"; "People Status Global"; "Begriff Vokabular"
"GL"; "PDA"; "People Status Global"; "People Status Global Updated"
"GL"; "PDA"; "Global People Status"; "Begriff Vokabular"
"GL"; "PDA"; "Global People Status"; "People Status Global Updated"
"GL"; "PDI"; "People Status Global"; "Begriff Vokabular"
"GL"; "PDI"; "People Status Global"; "People Status Global Updated"
"GL"; "PDI"; "Global People Status"; "Begriff Vokabular"
"GL"; "PDI"; "Global People Status"; "People Status Global Updated"
"GL"; "GLB"; "People Status Global"; "Begriff Vokabular"
"GL"; "GLB"; "Personenstatus global"; "Personenstatus global aktualisiert"
"GL"; "GLB"; "Global People Status"; "Begriff Vokabular"
"GL"; "GLB"; "Global People Status"; "People Status Global Updated"
"FTR"; 12

Mit meinem xsl bekomme ich eine einzelne Zeile:
"GL"; "PDAPDIGLB"; "People Status GlobalGlobal People Status"; "Begriff VokabularPeople Status Global Updated"

Und eine Kopfzeile:
"HDR"; "PIGLSSTD"; "20120112045620"; "F":
sollte am Anfang zusammen mit einer Fußzeile
angehängt werden "FTR";

unten.

War es hilfreich?

Lösung

Sie möchten so etwas :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:defaults>
   <termCat/>
   <termVocab/>
 </my:defaults>

 <xsl:variable name="vDefaults" select="document('')/*/my:defaults"/>

 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="term">
   <xsl:variable name="vTerm" select="."/>

   <xsl:variable name="vRow1" select="'&#xA;&quot;GL&quot;;'"/>

     <xsl:for-each select=
      "termCategory
      |
       $vDefaults/termCat[not($vTerm/termCategory)]">
       <xsl:variable name="vRow2" select=
           "concat($vRow1, $vQ, ., $vQ, ';')"/>

       <xsl:for-each select=
        "$vTerm/termVocabulary
        |
         $vDefaults/termCat[not($vTerm/termVocabulary)]
        ">
         <xsl:variable name="vRow3" select=
           "concat($vRow2, $vQ, ., $vQ, ';')"/>

        <xsl:for-each select=
         "$vTerm/relation/termVocabulary
         |
          $vDefaults/termCat[not($vTerm/relation/termVocabulary)]
         ">
        <xsl:value-of select="concat($vRow3, $vQ, ., $vQ, ';')"/>
      </xsl:for-each>
      </xsl:for-each>
     </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

wenn diese Umwandlung auf das bereitgestellte XML-Dokument angewendet wird :

<GetSavedReportResponse>
    <ResponseType>Success</ResponseType>
    <FileModifiedDateTime>2012-01-03T17:05:04</FileModifiedDateTime>
    <FileSizeBytes>7816</FileSizeBytes>
    <FileDataFormat>XML</FileDataFormat>
    <FileData>
        <Zthes>
            <term>
                <termId>49555</termId>
                <termUpdate>add</termUpdate>
                <termName>Active Personnel</termName>
                <termVocabulary>People Status Global</termVocabulary>
                <termVocabulary>Global People Status</termVocabulary>
                <termCategory>PDA</termCategory>
                <termCategory>PDI</termCategory>
                <termCategory>GLB</termCategory>
                <relation weight="100">
                    <termId>49556</termId>
                    <relationType>EQ</relationType>
                    <termName>term name</termName>
                    <termVocabulary>term vocabulary</termVocabulary>
                </relation>
                <relation weight="100">
                    <termId>49557</termId>
                    <relationType>BT</relationType>
                    <termName>General Active Personnel</termName>
                    <termVocabulary>People Status Global Updated</termVocabulary>
                </relation>
            </term>
            <term>
                <termId>49556</termId>
                <termUpdate>add</termUpdate>
                <termName>Leave of Absence Personnel</termName>
                <termVocabulary>People Status Global</termVocabulary>
                <termCategory>GLB</termCategory>
                <termCategory>PDI</termCategory>
                <relation weight="100">
                    <relationType>BT</relationType>
                    <termId>49554</termId>
                    <termName>General Non-Active Personnel</termName>
                    <termVocabulary>People Status Global</termVocabulary>
                </relation>
            </term>
        </Zthes>
    </FileData>
</GetSavedReportResponse>

das gewünschte, korrekte Ergebnis wird erzeugt :

"GL";"PDA";"People Status Global";"term vocabulary";
"GL";"PDA";"People Status Global";"People Status Global Updated";
"GL";"PDA";"Global People Status";"term vocabulary";
"GL";"PDA";"Global People Status";"People Status Global Updated";
"GL";"PDI";"People Status Global";"term vocabulary";
"GL";"PDI";"People Status Global";"People Status Global Updated";
"GL";"PDI";"Global People Status";"term vocabulary";
"GL";"PDI";"Global People Status";"People Status Global Updated";
"GL";"GLB";"People Status Global";"term vocabulary";
"GL";"GLB";"People Status Global";"People Status Global Updated";
"GL";"GLB";"Global People Status";"term vocabulary";
"GL";"GLB";"Global People Status";"People Status Global Updated";
"GL";"GLB";"People Status Global";"People Status Global";
"GL";"PDI";"People Status Global";"People Status Global";

Erläuterung : Sie möchten die Ausgabe nur ausführen, wenn eine vollständige Zeile erstellt wurde - nicht davor.

Update : Das OP funktioniert in einer Umgebung, in der die Funktion document() deaktiviert ist.Er möchte auch eine Kopf- und Fußzeile.

In diesem Fall kann jetzt eine leicht modifizierte Transformation (unter Verwendung der exslt:node-set()-Erweiterungsfunktion) verwendet werden:

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

     <xsl:variable name="vrtfDefaults">
       <termCat/>
       <termVocab/>
     </xsl:variable>

     <xsl:variable name="vDefaults" select=
      "ext:node-set($vrtfDefaults)"/>

     <xsl:variable name="vQ">"</xsl:variable>

     <xsl:template match="Zthes">
      <xsl:text>HDR";"PIGLSSTD";"20120112045620";"F":</xsl:text>

        <xsl:apply-templates/>

      <xsl:text>&#xA;FTR</xsl:text>
     </xsl:template>

     <xsl:template match="term">
       <xsl:variable name="vTerm" select="."/>

       <xsl:variable name="vRow1" select="'&#xA;&quot;GL&quot;;'"/>

         <xsl:for-each select=
          "termCategory
          |
           $vDefaults/termCat[not($vTerm/termCategory)]">
           <xsl:variable name="vRow2" select=
               "concat($vRow1, $vQ, ., $vQ, ';')"/>

           <xsl:for-each select=
            "$vTerm/termVocabulary
            |
             $vDefaults/termCat[not($vTerm/termVocabulary)]
            ">
             <xsl:variable name="vRow3" select=
               "concat($vRow2, $vQ, ., $vQ, ';')"/>

            <xsl:for-each select=
             "$vTerm/relation/termVocabulary
             |
              $vDefaults/termCat[not($vTerm/relation/termVocabulary)]
             ">
            <xsl:value-of select="concat($vRow3, $vQ, ., $vQ, ';')"/>
          </xsl:for-each>
          </xsl:for-each>
         </xsl:for-each>
     </xsl:template>

     <xsl:template match="text()"/>
</xsl:stylesheet>

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top