How to apply XSLT on XML with just XInclude
Asked Answered
L

2

5

I have this XML file:

<?xml version="1.0"?>
<xi:include href="http://www.w3schools.com/dom/books.xml" 
            xmlns:xi="http://www.w3.org/2003/XInclude"/>

and I expected that it should result in referenced remote XML file http://www.w3schools.com/dom/books.xml at the time of processing.

For that purpose I created this XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml"/>
    <xsl:template match="*">
        <xsl:copy-of select="//book/title"/>
    </xsl:template>
</xsl:stylesheet>

which after XSL transform, I expected to get XML output with title nodes from referenced XML file.

However it didn't happen, transformation just produced an empty file. I suspect that XInclude instruction wasn't performed.

So how can I apply XSLT on Xincluded XML file, if possible?

Lenardlenci answered 17/7, 2012 at 15:23 Comment(5)
See answer here: stackoverflow.com/questions/11491387Pains
#11491887 Most XML processors will not process XInclude for you.Pains
Your answer (XSLT 2) worked! Can you please tell me where I can learn more about this approach, as I just did copy/paste without higher knowledge? ThanksLenardlenci
Try these books: stackoverflow.com/questions/3442827/good-books-on-xsltPains
OK thanks, I meant about walk-through in your xsl example, not general xsl, but maybe you just made that up by yourselfLenardlenci
P
8

In comments, the OP has asked for a walk-through of my referenced answer at Copy xml document's images in different source locations into single output directory , so here it is.

This template..

<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:document(@href)" />
</xsl:template>

... matches the xi:include elements that meet all these requirements:

  1. Has a href attribute
  2. It refers to an xml document (as opposed to a text document)
  3. And that document can be found and is openable.

When these conditions are met, open the XML document and process it just as if it was in place here, instead of the xi:include node.

This template ...

<xsl:template match="xi:include[@href][@parse='text'][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:unparsed-text(@href,@encoding)" />
</xsl:template>

... does a similar thing for pure text includes. Note the default value for @parse is 'xml'. Note we can even change character encoding. Our main document could be in UTF-8, but the included document may well be UTF-16LE for example.

And finally this template...

<xsl:template match="xi:include[@href][@parse=('text','xml') or not(@parse)][not(fn:unparsed-text-available(@href))][xi:fallback]">
 <xsl:apply-templates select="xi:fallback/text()" />
</xsl:template>

... handles the cases where we can open the document (maybe the file reference is broken), and the xi:include node gives us some fall-back content.

Pains answered 18/7, 2012 at 2:8 Comment(7)
this uses XSLT 2.0 and unfortunately xsltproc only supports XSLT 1.0Thorianite
@Thorianite And how is that relevant to any thing? It is not as if the OP had a question about xsltproc.Pains
it makes it significantly less portableThorianite
@Thorianite If you look closely at the question, you see that even there, the OP had version="2.0". We always answer XSLT questions with respect to the XSLT version specified by the OP, and this particular question is in no way related to xsltproc. You are of course welcome to add your own solution, only using XSLT 1.0 constructs (difficult, in my opinion).Shier
@fommil: Wrong. The question was an XSLT 2.0 question. Even if it wasn't, xsltproc is not a "significant" fraction of the market. On almost every platform (even browsers), XSLT 2.0 or even XSLT 3.0 is now available.Pains
so what can you use in UNIX scripts to apply XSLT?Thorianite
@Thorianite post it as a question. Comment feed is not intended for q & a. I am sure you will get a helpful answer.Pains
O
0

XInclude processing, like XSD validation, is something that happens if you ask for it, and not otherwise. The way you ask for it depends on the environment you are in. For example, the Xerces parser has options to do XInclude processing.

Onepiece answered 17/7, 2012 at 16:33 Comment(1)
Ah, I see... I use the trial version of XMLSpy to get the feeling of XML processing. For XSL it's set to built-in processor, but for XInclude processing I can't seem to figure available option if there is anyLenardlenci

© 2022 - 2024 — McMap. All rights reserved.