开发者

how to recursively load xml files to one co-responding xslt file?

I have 10 xml files in the same layout and need to transform them by the same xslt file. So my question is: Is there a way that I can take the xml file name as parameter and IN THE XSLT FILE, it load those xml files and transform them recursively?

Cause usually, xml and xslt are tied by giving the xslt name IN THE XML FILE like this. And this is a one to one relationship. So is there some sort of doc function in xslt that can do the other way around: loading xml files IN THE XSLT file in a one to many relationship?

Some example code will b开发者_运维技巧e highly appreciated!Thanks.


If you're using XSLT 2, you could work with an index XML file:

<?xml-stylesheet href="your-stylesheet.xsl" ?>
<index>
  <doc href="doc1.xml" />
  <doc href="doc2.xml" />
  [...]
</index>

and then utilize xsl:result-document to write to your resulting files:

<xsl:template match="/index/doc">
  <xsl:variable name="target" select="concat(@href, '.result.html')" />
  <xsl:result-document href="$target">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head><title>Document</title></head>
      <body>
        <xsl:apply-templates select="document(@href, .)/" />
      </body>
    </html>
  </xsl:result-document>
</xsl:template>


To transform multiple files with same stylesheet, you normally use as input xml a file containing the paths to the files to be transformed.

You then parse the input file and apply templates to multiple files using the document function. See my recent answer to this question. This is the correct XSLT 1.0/2.0 approach.

If you need also to generate a separate output for each file, you use the XSLT 2.0 instruction xs:result-document or the equivalent XSLT 1.0 EXSLT extension. See my recent answer to this question.

So finally you can have:

  • 1 to 1 (single input -> single output)

  • n to 1 (multiple input with document() -> single output)

  • n to n (multiple input with document() -> xsl:result-document or extension and multiple output)

  • 1 to n (single input -> xsl:result-document or extension and multiple output)

Note there is not recursion.


XSLT have document function, which can load external XML files.

See this question: How to add an attribute "type" for default data type of elements in XSLT transformation. In that answer XSD (your XML in your case) loads in XSLT using document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜