使用XSL转换XML - 选择什么结构?

时间:2016-06-12 00:27:17

标签: xml xslt

我有两个XSL文件用于转换我的xml。

File1.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"
            indent="yes"
            encoding="utf-8"
            media-type="text/xml"/>

    <xsl:template match="File">
        <File>

            <Purchaser>
                <Name>
                    <xsl:value-of select="Customer/LastName"/>
                </Name>
            </Purchaser>

            <vehicles>
                <xsl:for-each select="Cars/Car">
                    <vehicle>
                        <vehicleColor>
                            <xsl:value-of select="Color"/>
                        </vehicleColor>
                        <vehicleModel>
                            <xsl:value-of select="Model"/>
                        </vehicleModel>
                        <vehicleYear>
                            <xsl:value-of select="Year"/>
                        </vehicleYear>
                        <vehicleSpeed>
                            <xsl:value-of select="Speed"/>
                        </vehicleSpeed>
                        <Towns>
                            <xsl:for-each select="Towns/Town">
                                <Town>
                                    <xsl:value-of select="."/>
                                </Town>
                            </xsl:for-each>
                        </Towns>
                    </vehicle>
                </xsl:for-each>
            </vehicles>

        </File>
    </xsl:template>

</xsl:stylesheet>

File2.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"
            indent="yes"
            encoding="utf-8"
            media-type="text/xml"/>

    <xsl:template match="File">
        <File>
            <xsl:apply-templates select="Customer"/>
            <xsl:apply-templates select="Cars"/>
        </File>
    </xsl:template>

    <xsl:template match="Customer">
        <Purchaser>
            <Name>
                <xsl:value-of select="LastName"/>
            </Name>
        </Purchaser>
    </xsl:template>

    <xsl:template match="Cars">
        <vehicles>
            <xsl:for-each select="Car">
                <vehicle>
                    <vehicleColor>
                        <xsl:value-of select="Color"/>
                    </vehicleColor>
                    <vehicleModel>
                        <xsl:value-of select="Model"/>
                    </vehicleModel>
                    <vehicleYear>
                        <xsl:value-of select="Year"/>
                    </vehicleYear>
                    <vehicleSpeed>
                        <xsl:value-of select="Speed"/>
                    </vehicleSpeed>
                    <Towns>
                        <xsl:for-each select="Towns/Town">
                            <Town>
                                <xsl:value-of select="."/>
                            </Town>
                        </xsl:for-each>
                    </Towns>
                </vehicle>
            </xsl:for-each>
        </vehicles>
    </xsl:template>

</xsl:stylesheet>

问题1:这两个文件做同样的事情,但我不知道选择什么结构。什么是“最佳”结构?

问题2:在<Name><xsl:value-of select="Customer/LastName"/></Name>我的文件1中,我被迫完整的路径?如何只在select属性中添加“LastName”?

0 个答案:

没有答案