是否可以在 XInclude 标签中使用通配符?

发布于 2024-10-08 20:50:08 字数 360 浏览 8 评论 0原文

恐怕这是不可能的,但也没有发现任何地方说不可能。

我想使用通配符在 XML 文档中包含一组文件。像这样:

<?xml version="1.0" encoding="utf-8"?>
<mydocument>
  <!-- ... -->
  <xi:include href="*include.xml"/>
</mydocument>

我知道这不起作用,但我认为它清楚地表达了我的意图。有办法实现这一点吗?

编辑:

我尝试使用xpointer属性,但无法使其工作。

I'm afraid it's not possible, but haven't found anywhere it is said to be impossible either.

I'd like to include a set of files within a XML document using wildcards. Like this:

<?xml version="1.0" encoding="utf-8"?>
<mydocument>
  <!-- ... -->
  <xi:include href="*include.xml"/>
</mydocument>

I know it doesn't work, but I think it clearly expresses my intentions. Is there a way to achieve this?

Edit:

I tried to use xpointer attribute, but couldn't make it work.

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

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

发布评论

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

评论(3

游魂 2024-10-15 20:50:08

是否可以在 XInclude 标记中使用通配符?

不。href 表示 URI,这些没有通配符的概念。

否则,可以通过诸如 href="http://www.google.com/*" 之类的内容来镜像 Google 主页。

提示:文件系统也没有任何通配符的概念。 贝壳可以。当他们解析路径并看到 * 时,他们会为您完成填写空白的繁重工作。底层文件系统永远看不到星号。

Is it possible to use wildcards with XInclude tags?

No. The href denotes a URI, and these do not have a concept of wildcards.

Otherwise it would be possible to mirror the Google homepage by saying something like href="http://www.google.com/*".

Hint: File systems do also not have any concept of wildcards. Shells do. They do the heavy lifting of filling in the blanks for you when they parse a path and see a *. The underlying file system never gets to see the asterisk.

椵侞 2024-10-15 20:50:08

有一个快速选项

覆盖 XmlResolver 来创建通配符感知解析器:

class WildCardResolver : XmlUrlResolver
{
    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        try
        {
            DirectoryInfo di = new DirectoryInfo(baseUri.AbsolutePath); // TODO Check it is valid.
            string combinedFilePath = Path.GetTempFileName();
            using (FileStream combinedStream = new FileStream(combinedFilePath, FileMode.OpenOrCreate))
            {
                foreach (FileInfo fi in di.GetFiles(relativeUri))
                {
                    using (FileStream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
                    {
                        fileStream.CopyTo(combinedStream);
                    }
                }
            }
            Uri absUri = new Uri(combinedFilePath);
            return absUri;
        }
        catch(Exception ex)
        {
            //Log Exception
            return base.ResolveUri(baseUri, relativeUri);
        }
    }
}

需要做很多事情来注意通配符是否适用此外

,BaseURI 可能很棘手,因为假设源 XML 来自

file://c:/ myXMLRepository/myXML.xml

包含 *inc.xml

现在基本 URI 是 file//c:/temp/tmpA0.tmp

祝你好运,

编辑:

还有另一种方法可以覆盖

public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)

,但还会带来其他问题...因为绝对 URI 并不总是有效,并且 XInclusionReader 会尝试并验证它。

There is a quick option

Override XmlResolver to create a Wildcard Aware Resolver:

class WildCardResolver : XmlUrlResolver
{
    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        try
        {
            DirectoryInfo di = new DirectoryInfo(baseUri.AbsolutePath); // TODO Check it is valid.
            string combinedFilePath = Path.GetTempFileName();
            using (FileStream combinedStream = new FileStream(combinedFilePath, FileMode.OpenOrCreate))
            {
                foreach (FileInfo fi in di.GetFiles(relativeUri))
                {
                    using (FileStream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
                    {
                        fileStream.CopyTo(combinedStream);
                    }
                }
            }
            Uri absUri = new Uri(combinedFilePath);
            return absUri;
        }
        catch(Exception ex)
        {
            //Log Exception
            return base.ResolveUri(baseUri, relativeUri);
        }
    }
}

There is a lot to do to notice if Wildcards are applicable at all

Additionally, the BaseURI can be tricky because lets say the Source XML is from

file://c:/myXMLRepository/myXML.xml

includes *inc.xml

now the base URI is file//c:/temp/tmpA0.tmp

.

.

wish you best of luck,

EDIT:

Theres another way to override

public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)

but there are other problem imposed by that... because the Absolute URI will not always be valid, and the XIncludingReader try and verify it.

捂风挽笑 2024-10-15 20:50:08

@Tomalek 绝对就在这里。有多种方法可以完成您想要做的事情,但 XInclude 不是正确的答案。您将需要某种可以为您处理通配符扩展的工具。

您几乎可以肯定可以使用 Norm Walsh 的 XProc 实现 - calabash 之类的东西来做到这一点,但您需要推出自己的等效工具以某种方式包含。

@Tomalek is absolutely right here. There are ways to do what you trying to do but XInclude is not the right answer. You are going to need some sort of tool that can handle wildcard expansion for you.

You can almost certainly do this with something like Norm Walsh's XProc implementation - calabash but you would need to roll your own equivalent to XInclude in some way.

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