Java 在xslt中定义列表变量
在我的xsl转换中,文件名作为参数传递给样式表。如果它在某个文件列表中,我想执行一组特定的操作。现在我就是这样做的Java 在xslt中定义列表变量,java,xml,xslt,Java,Xml,Xslt,在我的xsl转换中,文件名作为参数传递给样式表。如果它在某个文件列表中,我想执行一组特定的操作。现在我就是这样做的 <xsl:param name="specialFiles" select="'|a.xml|b.xml|'"/> <xsl:template match="/"> <xsl:choose> <xsl:when test="contains($specialFiles,concat('|',$FILENAME,'|'
<xsl:param name="specialFiles" select="'|a.xml|b.xml|'"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="contains($specialFiles,concat('|',$FILENAME,'|'))" >
<xsl:apply-templates select="abc" />
</xsl:when>
.....
.....
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("1.xsl"));
Transformer transformer = factory.newTransformer(xslt);
File xmlFile = new File(args[0]);
String baseName = xmlFile.getName();
transformer.setParameter("FILENAME", baseName); // pass the basename of the file
transformer.transform(new StreamSource(xmlFile ), new StreamResult(System.out));
编辑2:
我只是设法用一种稍微不同的方式来实现这一点,我在样式表中嵌入了一个xml片段,并在其中使用了一个xpath表达式
<specialFiles>
<name>abcdef.xml</name>
<name>sadfk32.xml</name>
</specialFiles>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="document('')/xsl:stylesheet/specialFiles/name/text()[contains(.,$fileName)]">
....
....
abcdef.xml
sadfk32.xml
....
....
我在样式表中嵌入了一个xml片段:
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("1.xsl"));
Transformer transformer = factory.newTransformer(xslt);
File xmlFile = new File(args[0]);
String baseName = xmlFile.getName();
transformer.setParameter("FILENAME", baseName); // pass the basename of the file
transformer.transform(new StreamSource(xmlFile ), new StreamResult(System.out));
<specialFiles>
<name>abcdef.xml</name>
<name>sadfk32.xml</name>
</specialFiles>
abcdef.xml
sadfk32.xml
因此,可以使用一个简单的xpath来验证某个名称是否在列表中
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("1.xsl"));
Transformer transformer = factory.newTransformer(xslt);
File xmlFile = new File(args[0]);
String baseName = xmlFile.getName();
transformer.setParameter("FILENAME", baseName); // pass the basename of the file
transformer.transform(new StreamSource(xmlFile ), new StreamResult(System.out));
XSLT的哪个版本,使用哪个处理器,以及如何调用它?XSLT中明显的“类似数组”结构是一段XML,包含一系列元素,每个元素对应一个值,但是否以及如何将节点作为参数传递给样式表是处理器特有的。@IanRoberts我使用的是2.0版,使用的是非常简单的java代码。我向样式表传递一个值,然后检查该值是否在列表中。2.0是XSLT的版本,您使用哪个处理器?Saxon?这是您的最新编辑,对文档(“”)/xsl:stylesheet/specialFiles/name=$filename
的测试将更简单、更准确(从您最初的问题来看,您似乎希望文件名
正好是其中一个特殊文件,而不是其中一个文件的子字符串)。
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("1.xsl"));
Transformer transformer = factory.newTransformer(xslt);
File xmlFile = new File(args[0]);
String baseName = xmlFile.getName();
transformer.setParameter("FILENAME", baseName); // pass the basename of the file
transformer.transform(new StreamSource(xmlFile ), new StreamResult(System.out));