Is it possible to use wildcards with XInclude tags?

 ̄綄美尐妖づ 提交于 2019-12-11 11:25:05

问题


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.


回答1:


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.




回答2:


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.




回答3:


@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.



来源:https://stackoverflow.com/questions/4488704/is-it-possible-to-use-wildcards-with-xinclude-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!