@@ -13,6 +13,7 @@ import fetch, { Headers, Request, Response } from 'node-fetch';
13
13
import {
14
14
XDocument ,
15
15
XNode ,
16
+ XmlParser ,
16
17
domAppendChild ,
17
18
domAppendTransformedChild ,
18
19
domCreateCDATASection ,
@@ -71,6 +72,7 @@ import { MatchResolver } from '../xpath/match-resolver';
71
72
*/
72
73
export class Xslt {
73
74
xPath : XPath ;
75
+ xmlParser : XmlParser ;
74
76
matchResolver : MatchResolver ;
75
77
options : XsltOptions ;
76
78
decimalFormatSettings : XsltDecimalFormatSettings ;
@@ -89,6 +91,7 @@ export class Xslt {
89
91
}
90
92
) {
91
93
this . xPath = new XPath ( ) ;
94
+ this . xmlParser = new XmlParser ( ) ;
92
95
this . matchResolver = new MatchResolver ( ) ;
93
96
this . options = {
94
97
cData : options . cData === true ,
@@ -397,22 +400,8 @@ export class Xslt {
397
400
case 'import' :
398
401
throw new Error ( `not implemented: ${ template . localName } ` ) ;
399
402
case 'include' :
400
- // We need to test here whether `window.fetch` is available or not.
401
- // If it is a browser environemnt, it should be.
402
- // Otherwise, we will need to import an equivalent library, like 'node-fetch'.
403
- if ( ! global . globalThis . fetch ) {
404
- global . globalThis . fetch = fetch as any ;
405
- global . globalThis . Headers = Headers as any ;
406
- global . globalThis . Request = Request as any ;
407
- global . globalThis . Response = Response as any ;
408
- }
409
-
410
- const fetchTest = await global . globalThis . fetch (
411
- 'https://raw.githubusercontent.com/DesignLiquido/xslt-processor/xsl-include/examples/head.xsl'
412
- ) ;
413
- const fetchResponse = await fetchTest . text ( ) ;
414
- console . log ( fetchResponse ) ;
415
- throw new Error ( `not implemented: ${ template . localName } ` ) ;
403
+ await this . xsltInclude ( context , template , output ) ;
404
+ break ;
416
405
case 'key' :
417
406
throw new Error ( `not implemented: ${ template . localName } ` ) ;
418
407
case 'message' :
@@ -618,6 +607,35 @@ export class Xslt {
618
607
}
619
608
}
620
609
610
+ /**
611
+ * Implements `xsl:include`.
612
+ * @param input The Expression Context.
613
+ * @param template The template.
614
+ * @param output The output.
615
+ */
616
+ protected async xsltInclude ( context : ExprContext , template : XNode , output : XNode ) {
617
+ // We need to test here whether `window.fetch` is available or not.
618
+ // If it is a browser environemnt, it should be.
619
+ // Otherwise, we will need to import an equivalent library, like 'node-fetch'.
620
+ if ( ! global . globalThis . fetch ) {
621
+ global . globalThis . fetch = fetch as any ;
622
+ global . globalThis . Headers = Headers as any ;
623
+ global . globalThis . Request = Request as any ;
624
+ global . globalThis . Response = Response as any ;
625
+ }
626
+
627
+ const hrefAttributeFind = template . childNodes . filter ( n => n . nodeName === 'href' ) ;
628
+ if ( hrefAttributeFind . length <= 0 ) {
629
+ throw new Error ( '<xsl:include> with no href attribute defined.' ) ;
630
+ }
631
+ const hrefAttribute = hrefAttributeFind [ 0 ] ;
632
+
633
+ const fetchTest = await global . globalThis . fetch ( hrefAttribute . nodeValue ) ;
634
+ const fetchResponse = await fetchTest . text ( ) ;
635
+ const includedXslt = this . xmlParser . xmlParse ( fetchResponse ) ;
636
+ await this . xsltChildNodes ( context , includedXslt . childNodes [ 0 ] , output ) ;
637
+ }
638
+
621
639
/**
622
640
* Orders the current node list in the input context according to the
623
641
* sort order specified by xsl:sort child nodes of the current
0 commit comments