Apply XSL to External XML

前端 未结 5 816
南旧
南旧 2020-12-28 08:54

Currently I use:


To link XSL to XML.

If my xml was here: www.externald

相关标签:
5条回答
  • 2020-12-28 09:18

    You can't achieve this with "pure" xml+xslt(*). Some external code will need to identify the xml and the xslt which should transform it.

    Since you seem to be transforming XML, I'm going to guess you're doing this in the webbrowser.

    You can do this using javascript, as demonstrated on w3schools. However, cross-domain restrictions still apply if you're doing javascript-based requests (i.e. AJAX/XHR): if the originating server doesn't set the appropriate CORS headers to allow cross-domain javascript access, you'll need to proxy that xml request via your own server.

    (*): Mad's answer uses entity references to cleverly embed external xml in a containing document. It's definitely worth a try, but be aware that this kind of entity-based inclusion has been used in several information-disclosure leaks, which is why it's often not enabled in the xml parser (in particular, this shouldn't be able to subvert CORS in a browser). You'll just have to try it in your situation.

    0 讨论(0)
  • 2020-12-28 09:20

    If you're trying to run the XSLT inside .NET, you can easily use the XslCompiledTransform class in .NET to achieve this.

    If you're trying to run this on e.g. the command line, there's a bunch of tools you can use to apply a XSLT file to a given XML file - typically however one that's on your local harddisk.

    See e.g. Oleg Tkachenko's web site for info on NXSLT and this other XSLT tools, or see this CodeProject article for a Windows shell extension to apply a XSLT to a given XML file (on your local harddisk).

    Hope this helps a bit.

    Marc

    0 讨论(0)
  • 2020-12-28 09:27

    You could write a local xml file as wrapper:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="XSL.xsl" ?>
    <wrapper Source="http://www.externaldomain.com/rss.xml"/>
    

    And extend your stylesheet so that it understands the wrapper:

    <xsl:template match="wrapper">
      <xsl:apply-templates select="document(./@Source)"/>
    </xsl:template>
    

    I haven't tested it with XML files over http but it works with local XML files that I don't want to change to include a xml-stylesheet processing instruction. It works mit Firefox, Opera and IE (7, I haven't tried other versions)

    0 讨论(0)
  • 2020-12-28 09:33

    You can create a local XML file that includes the XML content of the remote XML file through an entity reference.

    The example below will give you the content of the remote XML file inside of a wrapper document element.

    Then you can include a stylesheet processing instruction on your local XML file.

    However, since the local file has a wrapper document element, you might need to point to a "wrapper XSLT" that uses xsl:import to import the original XSL.xsl and apply-templates starting with the content inside the wrapper element.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE wrapper [
    <!ENTITY content SYSTEM "http://stackoverflow.com/feeds">
    ]>
    <?xml-stylesheet type="text/xsl" href="XSL.xsl" ?>
    <wrapper>
        &content;
    </wrapper>
    
    0 讨论(0)
  • 2020-12-28 09:35

    One solution: Get the external XML into an XMLDocument object and then insert a node that contains include statement of xsl. Include xsl can be taken from http://adityabajaj.com/weblog/include-xsl-in-x-ml/.

    0 讨论(0)
提交回复
热议问题