开发者

xslt flatten any xml file

Given the following XML as an example:

<?xml version="1.0" encoding="UTF-8"?>
<Message>
<MessageID>1</MessageID>
<MessageType>0</MessageType>
<UniqueRef>12</UniqueRef>
<CreatedBy>fooo.bar</CreatedBy>
<Product>
    <Name>Food Mixer</Name>
    <Origin>London</Origin>
    <CreatedBy>foo.bar</CreatedBy>
</Product>
<ProductExtendedProperties>
    <CreateDate>23/10/2010</CreateDate>
    <CreatedBy>foo.bar</CreatedBy>
</ProductExtendedProperties>
<Items>
    <Item>
        <Title>Food Mixer</Title>
        <CreatedBy>my.customer</CreatedBy>
    </Item>
</Items>

Is it possible to create an xslt which is generic in that it will take whatever element is thrown at it, nested or not and provide nicely formatted html.

Something like this:

<html>
<body>
    <fieldset>
        <legend>Message</legend>
        <div>
            <p>MessageID: 1</p>
            <p>MessageType: 0</p>
            <p>UniqueRef: 0</p>
            <p>CreatedBy: foo.bar</p>

            <div>
                <p>Product:</p>
                <ul>
           开发者_运维技巧         <li>Name: Food Mixer</li>
                    <li>Origin: London</li>
                    <li>CreatedBy: foo.bar</li>
                </ul>
            </div>
            <div>
                <p>ProductExtendedProperties:</p>
                <ul>
                    <li>CreateDate: 23/10/2010</li>
                    <li>CreatedBy: foo.bar</li>
                </ul>
            </div>
            <div>
                <p>
                    Items
                </p>
                <div>
                    <p>Item 1:</p>
                    <ul>
                        <li>Title: Food Mixer</li>
                        <li>CreatedBy: my.customer</li>
                    </ul>
                </div>


            </div>
        </div>
    </fieldset>
</body>

it doesnt have to be as complex as that, just something which formats into nested list elements all names and values of elements really.

I have no clue where to start with the xslt

Any help much appreciated thanks


I think people will be a lot more able to help you if you do get a start, and then explain specifically where you need help getting from your attempted solution to what you need.

E.g. you could start with a template that applies templates to "*" (any child element), and another template that matches "*" and uses local-name() to output the name of the current element (along with desired markup).


One way:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml"/>
    <xsl:template match="/*" priority="1">
        <html>
            <body>
                <fieldset>
                    <legend>
                        <xsl:value-of select="name()"/>
                    </legend>
                    <div>
                        <xsl:apply-templates/>
                    </div>
                </fieldset>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="/*/*[not(*)]" priority="1">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="*[*]">
        <div>
            <p>
                <xsl:value-of select="concat(name(),':')"/>
            </p>
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    <xsl:template match="*[not(*)][position()!=1]"/>
    <xsl:template match="*[not(*)][1]">
        <ul>
            <xsl:apply-templates select="../*" mode="makeLi"/>
        </ul>
    </xsl:template>
    <xsl:template match="*" mode="makeLi">
        <li>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="concat(name(..),': ',.)"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<html>
    <body>
        <fieldset>
            <legend>Message</legend>
            <div>
                <p>MessageID: 1</p>
                <p>MessageType: 0</p>
                <p>UniqueRef: 12</p>
                <p>CreatedBy: fooo.bar</p>
                <div>
                    <p>Product:</p>
                    <ul>
                        <li>Name: Food Mixer</li>
                        <li>Origin: London</li>
                        <li>CreatedBy: foo.bar</li>
                    </ul>
                </div>
                <div>
                    <p>ProductExtendedProperties:</p>
                    <ul>
                        <li>CreateDate: 23/10/2010</li>
                        <li>CreatedBy: foo.bar</li>
                    </ul>
                </div>
                <div>
                    <p>Items:</p>
                    <div>
                        <p>Item:</p>
                        <ul>
                            <li>Title: Food Mixer</li>
                            <li>CreatedBy: my.customer</li>
                        </ul>
                    </div>
                </div>
            </div>
        </fieldset>
    </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜