xml:base - XML: Extensible Markup Language 编辑

Deprecated

This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

xml:base is like the HTML <base> element, but can specify the base URI per element as well as the entire document.

The base URL of a element can be queried from a script using Node.baseURI.

The base URI of an element is:

  1. the base URI specified by an xml:base attribute on the element, if one exists, otherwise
  2. the base URI of the element's parent element within the document entity or external entity, if one exists, otherwise
  3. the base URI of the document entity or external entity containing the element.

Support was removed from blink (Chrome and Opera) in 2015:

And from Firefox 66 in bug 903372.

Use cases

If you have an inline SVG you use as icons that needs to work in a document with a HTML BaseElement , you can reset the URI on your icon sprite by setting the xml:base.

<html>
  <head>
  <base href="https://mydomain.com">
  <style>
  .link { stroke: #999; stroke-opacity: .6; }
  marker#arrow { fill: black; }
  </style>
</head>
<body>
  <svg width="100%" height="100%" xml:base="">
    <defs>
      <marker id="arrow" viewBox="0 -5 10 10" refX="0" refY="0" markerWidth="20" markerHeight="20" orient="auto">
        <path d="M0,-5L10,0L0,5"></path>
      </marker>
    </defs>
    <line x1="100" y1="100" x2="333" y2="333" marker-start="url(#arrow)" class="link"></line>
  </svg>
</body>
</html>

Imagine a SVG with font references. E.g.

<svg xml:base="https://foobar.s3-eu-west-1.amazonaws.com/uploads/15066845653629"
  width="909" height="1286" viewBox="0 0 909 1286"xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<style type="text/css"><![CDATA[

.s9_999{
font-size: 9.17px;
font-family: ZapfDingbats_ghr;
fill: #161615;
}
]]></style>

  <text
    x="647"
    y="412"
    dx="0"
    class="s9_999"
  >r</text>

<style type="text/css"><![CDATA[

@font-face {
    font-family: ZapfDingbats_ghr;
    src: url("fonts/ZapfDingbats_ghr.woff") format("woff");
}

]]></style>

</svg>

Setting the xml:base on the SVG Element means you can inline the SVG and thereby bypass CORS issue while not changing the base URI for your entire document.

Work-arounds

Specifications

SpecificationStatusComment
XML Base (Second Edition)Recommendation

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:132 次

字数:4738

最后编辑:6 年前

编辑次数:0 次

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