使用 XSLT 为标签添加属性 [英] Add attribute to tag with XSLT

查看:40
本文介绍了使用 XSLT 为标签添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个带有 1-n 路径元素的 svg 文档,现在我想更改这些路径元素的颜色.

I have a few svg documents with a 1-n Path elements now i wanted to change the color of those path elements.

我还没有找到方法来做到这一点

i have haven't found a way to do this

Svg 示例文档:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" height="45" width="45" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
<g transform="matrix(1.25,0,0,-1.25,0,45)">
<path d="m9 18h18v-3h-18v3"/>
</g>
</svg>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' version='1.0'>
<xsl:template match='path'>
<xsl:copy>
<xsl:attribute name='fill'>red</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

我需要更改什么才能将填充属性添加/更改为红色?

What do i need to change to make it add/change the fill attribute to red?

推荐答案

我想您误解了 XSLT 的工作原理.它接受一个输入 XML 树并通过解释您的样式表生成一个 树.换句话说,您的样式表定义了如何根据输入的 XML 树从头开始生成一个全新的树.

I think you misunderstand how XSLT works. It takes an input XML tree and produces a new tree by interpreting your stylesheet. In other words, your stylesheet defines how a completely new tree is produced from scratch, based on the input XML tree.

重要的是要了解您不是在修改原始 XML 树.这就像纯函数式语言和命令式语言之间的区别.底线:您不能更改fill 属性为red,您可以生成原始文档的副本,其中fill 属性设置为 red.

It's important to understand that you are not modifying the original XML tree. It's like a difference between a purely functional and imperative language. Bottom line: you can't change the fill attribute to red, you can produce a copy of your original document where the fill attribute is set to red.

也就是说,这或多或少你会这样做:

That said, this is more or less how you would do it:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:svg="http://www.w3.org/2000/svg" version='1.0'>
    <!-- this template is applied by default to all nodes and attributes -->
    <xsl:template match="@*|node()">
        <!-- just copy all my attributes and child nodes, except if there's a better template for some of them -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- this template is applied to an existing fill attribute -->
    <xsl:template match="svg:path/@fill">
        <!-- produce a fill attribute with content "red" -->
        <xsl:attribute name="fill">red</xsl:attribute>
    </xsl:template>

    <!-- this template is applied to a path node that doesn't have a fill attribute -->
    <xsl:template match="svg:path[not(@fill)]">
        <!-- copy me and my attributes and my subnodes, applying templates as necessary, and add a fill attribute set to red -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:attribute name="fill">red</xsl:attribute>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这篇关于使用 XSLT 为标签添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆