首页 \ 问答 \ 使用xslt将调色板文件转换为html内容(Transform palette file to html content with xslt)

使用xslt将调色板文件转换为html内容(Transform palette file to html content with xslt)

我想转换这种类型的XML调色板文件:

<?xml version="1.0"?>
<?xml-stylesheet href="Palette.xsl" type="text/xsl"?>
<Palette Name="MyPalette">
  <PaletteEntry Color="#D0427B" Color2="#D0427B" />
  <PaletteEntry Color="#D55588" Color2="#D55588" />
  <PaletteEntry Color="#DA6895" Color2="#DA6895" />
</Palette>

使用此XSL转换:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Palette">
<html>
    <head>
        <title>Palette {@Name}</title>
        <!--link rel="stylesheet" href="Palette.css" type="text/css"/-->
    </head>
    <body>
        <div class="palette">
            <xsl:apply-templates/>
        </div>
    </body>
</html>
</xsl:template>

<xsl:template match="PaletteEntry">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: {@Color}; border: 2px solid {@Color2};">
        <xsl:apply-templates select="@*"/>
    </div>
</xsl:template>

<xsl:template match="@Color">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="@Color2">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

输出中的问题是,颜色属性在属性中时会很好地呈现,但不会在内容中进行翻译。 这是HTML输出:

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Palette {@Name}</title>
</head>

<body>
  <div class="palette">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;" color="495359" color2="#495359"></div>
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;" color="5B646A" color2="#5B646A"></div>
  </div>
</body>

</html>

如何将颜色值复制到内容?

预期产量:

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Palette "MyPalette"</title>
</head>

<body>
  <div class="palette">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;">#495359<br/>#495359</div>
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;">#5B646A<br/>#5B646A</div>
  </div>
</body>

</html>


I want to transform an XML palette file of this type:

<?xml version="1.0"?>
<?xml-stylesheet href="Palette.xsl" type="text/xsl"?>
<Palette Name="MyPalette">
  <PaletteEntry Color="#D0427B" Color2="#D0427B" />
  <PaletteEntry Color="#D55588" Color2="#D55588" />
  <PaletteEntry Color="#DA6895" Color2="#DA6895" />
</Palette>

using this XSL transformation:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Palette">
<html>
    <head>
        <title>Palette {@Name}</title>
        <!--link rel="stylesheet" href="Palette.css" type="text/css"/-->
    </head>
    <body>
        <div class="palette">
            <xsl:apply-templates/>
        </div>
    </body>
</html>
</xsl:template>

<xsl:template match="PaletteEntry">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: {@Color}; border: 2px solid {@Color2};">
        <xsl:apply-templates select="@*"/>
    </div>
</xsl:template>

<xsl:template match="@Color">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="@Color2">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

The problem in the output is that the color attributes are rendered well when they are in an attribute but are not translated in content. Here is the HTML output:

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Palette {@Name}</title>
</head>

<body>
  <div class="palette">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;" color="495359" color2="#495359"></div>
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;" color="5B646A" color2="#5B646A"></div>
  </div>
</body>

</html>

How can I copy color values to content?

Expected output:

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Palette "MyPalette"</title>
</head>

<body>
  <div class="palette">
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;">#495359<br/>#495359</div>
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;">#5B646A<br/>#5B646A</div>
  </div>
</body>

</html>


原文:https://stackoverflow.com/questions/43179574
更新时间:2023-04-18 13:04

最满意答案

你需要部分更新。 PUT调用update()和PATCH调用partial_update()。 唯一的区别是partial = True(PATCH)或partial = False(PUT)。 您可以在视图中初始化序列化程序时自行设置部分。

您可以像在Django REST框架代码(mixins.py)中那样使用PUT执行部分更新:

def update(self, request, *args, **kwargs):
    kwargs['partial'] = True
    return super(YourCustomView, self).update(request, *args, **kwargs)

You need partial update. PUT calls update() and PATCH calls partial_update(). The only difference is partial=True (PATCH) or partial=False (PUT). And you can set partial yourself when initializing the serializer in your views.

You can do it like in Django REST framework code (mixins.py), to perform partial updates with PUT:

def update(self, request, *args, **kwargs):
    kwargs['partial'] = True
    return super(YourCustomView, self).update(request, *args, **kwargs)

相关问答

更多
  • 打印的两个URL显示正在发生的事情。 您正在发布到没有最终斜杠的URL,但是您具有默认的APPEND_SLASH设置,因此Django会重定向到带有最后斜杠的URL。 重定向总是GET。 确保使用斜杠发布到URL。 The two URLs that are printed show exactly what is happening. You are posting to a URL without a final slash, but you have the default APPEND_SLASH ...
  • TL; DR: request.method在实际使用中永远None是None ,但对于您的特定情况,您正在查看错误的内容。 通用HttpRequest django/http/__init__.py : class HttpRequest(object): ... def __init__(self): ... self.method = None ... 当一个普通的HttpRequest被实例化时,它的方法是None 。 但是, WSG ...
  • 该表单看起来是由add_coin视图处理的,因此您应该更改表单操作以发布给它。
    然后我期望看到 test1 GET 当你对add_coin视图执行初始GET请求时,以及 test1 POST 当您使用POST请求提交表单时。 The form appears to be handled by the add_coin view, so you should chan ...
  • 在views.py中添加一个这样的新类: class SurveyAPIView(APIView): def post(self, request, format=None): serializer = SurveySerializer(request.data) if serializer.is_valid(): instance = serializer.save() return Response(seriali ...
  • 此错误由跨站点请求伪造保护 (CSRF)中间件显示,其目的是防止恶意网站在没有您的用户知情的情况下发送数据。 简而言之:您需要将{% csrf_token %}添加到模板中的元素中。 到底有什么好处呢? 假设你把它放在一个网页上,有人决定用虚假数据向你发送垃圾邮件,并设法注入任何人进入页面时提交表单的Javascript代码。 (或者更糟糕的是,如果您的表单允许用户执行破坏性操作,例如删除他的帐户。)CSFR会停止这一点,因为它确保只有作为对HTML表单的响应发送的请求才能通过。 除此之外:有更 ...
  • 您不能只调用return dashboard() 。 您必须启动重定向 - 告诉浏览器加载/dashboard : return flask.redirect(flask.url_for('dashboard')) You cannot just call return dashboard(). You must initiate redirect - tell the browser to load /dashboard: return flask.redirect(flask.url_for('das ...
  • 我用ajax进行注册还有很长的路要走。 所以下面的代码可以达成交易。
    {% csrf_token %} {{form}} {% if error %}

    {{error}}

    {% endif %}
  • 你需要部分更新。 PUT调用update()和PATCH调用partial_update()。 唯一的区别是partial = True(PATCH)或partial = False(PUT)。 您可以在视图中初始化序列化程序时自行设置部分。 您可以像在Django REST框架代码(mixins.py)中那样使用PUT执行部分更新: def update(self, request, *args, **kwargs): kwargs['partial'] = True return sup ...
  • 要创建处理不同类型请求的单个API端点,应该重写实现定制逻辑的方法。 默认情况下predefined functions for each http methods GET,PUT,POST,PATCH and DELETE都有predefined functions for each http methods GET,PUT,POST,PATCH and DELETE 。 举例来说,请参阅下面的代码块。 网址 url(r'^api-endpoint/', apiView.as_view(), namesp ...
  • 你可以试试这个