2016-05-18 3 views
0

Ich habe eine XML-Datei mit folgender Struktur. Ich möchte die Daten aus XML-Datei lesen und in ein anderes Format mit XSL-Datei transformieren, aber in gewisser Weise ist es nicht einmal durch meine XML-Knoten lesen. Kann mir bitte jemand einen Vorschlag machen?XSL funktioniert nicht für meine XML nach dem Wurzelknoten

XML-Dokument:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="spss2eml.xsl"?> 
<outputTree xmlns="http://www.ibm.com/software/analytics/spss/xml/oms" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.ibm.com/software/analytics/spss/xml/oms http://www.ibm.com/software/analytics/spss/xml/oms/spss-output-1.8.xsd"> 
    <command command="Codebook" displayOutlineValues="label" displayOutlineVariables="label" 
     displayTableValues="label" displayTableVariables="label" lang="en" text="Codebook"> 
     <pivotTable subType="Variable Information" text="Respondent_Serial"> 
      <dimension axis="row" text="Attributes"> 
       <group text="Standard Attributes"> 
        <category text="Label"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Serial number"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Type"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Numeric"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Format"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="F10"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Measurement"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Scale"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Role"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Input"/> 
          </category> 
         </dimension> 
        </category> 
       </group> 
      </dimension> 
     </pivotTable> 
     <pivotTable subType="Variable Information" text="Respondent_ID"> 
      <dimension axis="row" text="Attributes"> 
       <group text="Standard Attributes"> 
        <category text="Label"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="ID"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Type"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="String"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Format"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="A150"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Measurement"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Nominal"/> 
          </category> 
         </dimension> 
        </category> 
        <category text="Role"> 
         <dimension axis="column" text="Values"> 
          <category text="Value"> 
           <cell text="Input"/> 
          </category> 
         </dimension> 
        </category> 
       </group> 
      </dimension> 
     </pivotTable> 
    </command> 
    </outputTree> 

Mein XSL-Stylesheet (spss2eml.xsl):

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
     <attributeList> 
      <xsl:for-each select="ns1:outputTree/ns1:command/ns1:pivotTable" 
       xmlns:ns1="http://xml.spss.com/spss/oms"> 
       <xsl:element name="attribute"> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="@text"/> 
        </xsl:attribute> 
        <xsl:element name="attributeName"> 
         <xsl:value-of select="@text"/> 
        </xsl:element> 
        <xsl:element name="attributeDefinition"> 
         <xsl:value-of 
          select="ns1:dimension/ns1:group[@text='Standard Attributes']/ns1:category[@text='Label']/ns1:dimension/ns1:category/ns1:cell/@text" 
         /> 
        </xsl:element> 
        <xsl:element name="storageType"> 
         <xsl:attribute name="typeSystem" 
          >http://www.w3.org/2001/XMLSchema-datatypes</xsl:attribute> 
         <xsl:choose> 
          <xsl:when 
           test="ns1:dimension/ns1:group[@text='Standard Attributes']/ns1:category[@text='Type']/ns1:dimension/ns1:category/ns1:cell[@text='Numeric']" 
           >float</xsl:when> 
          <xsl:otherwise>string</xsl:otherwise> 
         </xsl:choose> 
        </xsl:element> 
        <xsl:element name="measurementScale"> 
         <xsl:choose> 
          <xsl:when 
           test="ns1:dimension/ns1:group[@text='Standard Attributes']/ns1:category[@text='Measurement']/ns1:dimension/ns1:category/ns1:cell[@text='Scale']">Scale        
          </xsl:when> 
          <xsl:when 
           test="ns1:dimension/ns1:group[@text='Standard Attributes']/ns1:category[@text='Measurement']/ns1:dimension/ns1:category/ns1:cell[@text='Nominal']"> 
           <nominal> 
            Nominal 
           </nominal> 
          </xsl:when> 
          <xsl:when 
           test="ns1:dimension/ns1:group[@text='Standard Attributes']/ns1:category[@text='Measurement']/ns1:dimension/ns1:category/ns1:cell[@text='Ordinal']"> 
           <ordinal> 
            Ordinal 
           </ordinal> 
          </xsl:when> 
         </xsl:choose> 
        </xsl:element> 
       </xsl:element> 
      </xsl:for-each> 
     </attributeList> 
    </xsl:template> 
</xsl:stylesheet> 

Antwort

0

Ihr Partner XML verwendet einen Namespace, der von dem einen anders Sie in Ihrem Stylesheet verwenden. Die Quelle XML hat einen Standard-Namespace von:

xmlns="http://www.ibm.com/software/analytics/spss/xml/oms" 

Aber Sie definieren:

xmlns:ns1="http://xml.spss.com/spss/oms" 

statt:

xmlns:ns1="http://www.ibm.com/software/analytics/spss/xml/oms" 

diese Änderung vornehmen, und Sie werden den Unterschied sehen. Sie haben das erwartete Ergebnis nicht veröffentlicht, daher bin ich nicht auf Ihren tatsächlichen Code eingegangen. Beachten Sie jedoch, dass Sie es deutlich durch die Verwendung literal result elements und attribute value templates verkürzen können - zum Beispiel:

<xsl:element name="attribute"> 
    <xsl:attribute name="id"> 
     <xsl:value-of select="@text"/> 
    </xsl:attribute> 
</xsl:element> 

geschrieben werden kann einfach als:

<attribute id="{@text}"/> 
+0

ich das Namespace Problem hatte. Es hat gut funktioniert, nachdem der Namespace korrigiert wurde. Vielen Dank!! –