XSLT - 作为一系列节点的参数 [英] XSLT - Parameter as a series of nodes

查看:56
本文介绍了XSLT - 作为一系列节点的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将多个文件的名称传入 XSLT 并使用 document($myFile) 处理这些文件.我正在尝试使用撒克逊引擎在命令行上传递参数,但它不断抛出错误.

My goal is to pass in the names of multiple files into an XSLT and process the files using document($myFile). I'm trying to pass the parameter through on the command line using the saxon engine and it keeps throwing errors.

我知道我可以写出一个清单文件,在其中进行处理,然后在完成后将其删除.但这似乎是很多额外的工作,可能会进一步减慢速度.

I know I could write out a manifest file, process that in, and then delete it when I'm finished. But that just seems to be a lot of extra work that would potentially slow things down even more.

当参数像这样硬编码时,XSLT 工作......

The XSLT works when the parameter hard coded like this...

<xsl:param name="PnewArticles" as="element()*">
    <file-name>XMLFile.XML</file-name>
    <file-name>XMLFile2.XML</file-name>
</xsl:param>

从命令行分配看起来像这样:

To assign from the command line looks like this:

XSLT -s:Source.XML -o:outfileTest.xml -xsl:"test.xsl" newArticles='<file-name>XMLFile.XML</file-name>'

<!-- xslt param changed to this: -->
<xsl:param name="newArticles"/>

但是,它似乎将其作为字符串值读取.打印出来时看起来像这样(当然,失败了):

However, it appears to be reading it as a string value. When printed it comes out looking like this (which, of course, fails):

'&lt;file-name&gt;XMLFile.XML&lt;/file-name&gt;'

我在命令行(单/双)上尝试了各种引号组合,但无济于事.还尝试添加 as="element()*" 与硬编码示例一样 - 但随后它抱怨得很厉害......

I've tried various combinations of quotes on the command line (single/double), but to no avail. Also tried adding in the as="element()*" as with the hard coded example - but then it complains mightily...

  XPTY0004: Required item type of value of variable $newArticles is node(); suplied value has item type xs:untypedAtomic

有什么想法吗?看起来这应该是可能的.

Any ideas? It seems like this should be possible.

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:oasis="//OASIS//DTD XML Exchange Table Model 19990315//EN"
xmlns:mml="http://www.w3.org/1998/Math/MathML"     xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
exclude-result-prefixes="mml oasis dc content xsi">

<xsl:output method="xml" encoding="utf8"/>
<xsl:param name="newArticles"/>

<!-- When these are used they work -- the extra letter in front is just to silence -->
<xsl:param name="PnewArticles" as="element()*">
    <file-name>XMLFile.XML</file-name>
</xsl:param>
<xsl:variable name="VnewArticles" as="element()*">
    <file-name>XMLFile.XML</file-name>
</xsl:variable>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="skipDays">
    <xsl:copy-of select="."/>
    <myParam>
        <xsl:value-of select="$newArticles"/>
    </myParam>
    <xsl:apply-templates select="document($newArticles)" mode="addArticle"/>
</xsl:template>

<xsl:template match="front" mode="addArticle">
    <item>
        <xsl:text>NEW XML, Vol. </xsl:text>
        <xsl:value-of select="volume"/>
        <xsl:text>, No. </xsl:text>
        <xsl:value-of select="issue"/>
    </item>
</xsl:template>

<xsl:template match="body" mode="addArticle"/>
</xsl:stylesheet>

源文件

<?xml version="1.0"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<language>en-us</language>
<skipDays>
 <day>Saturday</day>
 <day>Sunday</day>
</skipDays>
</channel>
</rss>

XML 文件

<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE article>
<article xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dtd-version="2.2" xml:lang="EN" article-type="abstract">
  <front>
    <volume>17</volume>
    <issue>1</issue>
  </front>
  <body>
    <sec>
      <title>This is my Title</title>
      <p>This is a Paragraph</p>
    </sec>
  </body>
 </article>

推荐答案

如果您确实想在命令行上提供词法 XML,则必须在样式表中使用 saxon:parse() 或XPath 3.0 函数 parse-xml(),两者都需要 Saxon-PE 或更高版本.但对我来说,这似乎是一件奇怪的事情.

If you really want to supply lexical XML on the command line, you will have to parse it within the stylesheet using a call on saxon:parse() or the XPath 3.0 function parse-xml(), both of which need Saxon-PE or higher. But to me it seems an odd thing to do.

我认为最明显的解决方案是提供一个包含文件名列表的字符串值参数,用冒号或分号之类的东西分隔,然后在样式表中使用 tokenize() 来分离单个文件名,然后可以传递给 document().实际上 document() 接受一个 URI 列表,所以你可以直接做 document(tokenize($param, ';')).

I would have thought the most obvious solution is to supply a string-valued parameter containing the list of filenames, separated by something like colon or semicolon, and then use tokenize() within the stylesheet to separate out the individual filenames, which can then be passed to document(). In fact document() accepts a list of URIs, so you could directly do document(tokenize($param, ';')).

这篇关于XSLT - 作为一系列节点的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆