xslt 压平任何 xml 文件

发布于 2024-11-04 10:56:07 字数 2359 浏览 1 评论 0原文

以以下 XML 为例:

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

是否可以创建一个通用的 xslt,因为它将接受向其抛出的任何元素(无论是否嵌套)并提供格式良好的 html。

像这样的事情:

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

它不必那么复杂,只需将元素的所有名称和值格式化为嵌套列表元素即可。

我不知道从哪里开始使用 xslt

非常感谢任何帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-11-11 10:56:07

我认为,如果您确实开始,然后具体解释您在哪里需要帮助,从您尝试的解决方案到您需要的内容,人们将更能够帮助您。

例如,您可以从一个将模板应用于 "*" (任何子元素)的模板开始,以及另一个匹配 "*" 并使用 local-name 的模板() 输出当前元素的名称(以及所需的标记)。

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).

岁月静好 2024-11-11 10:56:07

一种方式:

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

输出:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文