简体   繁体   中英

XSL not working for my XML after root node

I have a XML file with following structure. I want to read the data from XML file and transform it into another format using XSL file but in some way it is not even reading thru my xml nodes. Can someone please suggest me a way to do it.

XML Document :

 <?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>

My 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>

Your source XML uses a namespace that's different from the one you're using in your stylesheet. The source XML has a default namespace of:

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

But you define:

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

instead of:

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

Make this change and you'll see a difference. You haven't posted the expected result, so I haven't gone into your actual code. Note, however, that you could shorten it significantly by using literal result elements and attribute value templates - for example:

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

can be written simply as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM