当使用CSS设置XML样式时,如何引用包含句点或冒号的标签名称? [英] When styling XML with CSS, how to refer to tag names that contain periods or colons?
问题描述
我的样式中包含< a.b>< /a.b>
或< b :c>< / b:c>
。现在当然,如果我写CSS规则像
ab {
}
b:c {
}
它不会工作,因为这些字符将被解释为类和伪类。有没有办法用CSS引用这些标记名称? (我不想使用 XSL ...)
$ b $如上所述,要在其标签名称中引用包含。
的元素,您可以简单地转义it: a\.b
$元素,但是<$ c $> 不能说 元素,因为:
在XML中有一个特殊的含义,作为命名空间分隔符。这意味着你拥有的元素实际上是 b
c www.w3.org/TR/xml-namesrel =nofollow>命名空间,而不是名为 b:c
的元素。 1
也就是说,有两种正确的方法来选择该元素。首先,因为我提到的元素实际上被称为 c
而不是 b:c
,你可以简单地使用: / p>
c
在名称空间冲突的情况下,第二种方法是首先在样式表的开头声明 b
命名空间,使用 @namespace
code> xmlns:b 命名空间引用在文档中的某个位置,否则它将无效):
/ *在XML文档中对应于xmlns:b的任何URL * /
@namespace b'http://ns.example.com/b';
然后使用命名空间前缀选择元素:
b | c
1 我们在这里谈论XML,而不是HTML或任何任何标记语言,其中:
没有任何特殊意义。
I'm styling an XML document that contains tags like <a.b></a.b>
or <b:c></b:c>
. Now of course if I write CSS rules like
a.b {
}
b:c {
}
It won't work because these characters will be interpreted as classes and pseudo-classes, respectively. Is there a way to refer to these tag names with CSS? (I don't want to use XSL...)
As mentioned, to refer to an element with a .
in its tag name you can simply escape it:
a\.b
But the same can't be said for a b:c
element, because :
has a special meaning in XML, as a namespace separator. This means that the element you have is actually a c
element in the b
namespace, not an element called b:c
.1
That said, there are two correct ways to select that element. First, since as I mentioned the element is actually called c
and not b:c
, you can simply use:
c
The second way, in case of namespace collisions, is to first declare the b
namespace in the beginning of your stylesheet using @namespace
corresponding to the one in your XML document (there should be an xmlns:b
namespace reference somewhere in your document, otherwise it won't be valid):
/* Whichever URL corresponds to xmlns:b in your XML document */
@namespace b 'http://ns.example.com/b';
Then use a namespace prefix to select the element:
b|c
1 Remember that we're talking about XML here, not HTML or any arbitrary markup language where :
holds no special meaning.
这篇关于当使用CSS设置XML样式时,如何引用包含句点或冒号的标签名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!