Rnet
Rnet

Reputation: 5040

Define a list variable in xslt

In my xsl transform the filename is passed as a parameter to the stylesheet. I want to do a certain set of actions if it is in a certain filelist. Right now I'm doing it this way;

<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>
        .....
        .....

This works, but quickly becomes messy when the specialFiles list grows. Is there a way to declare it like an array, and lookup quickly?

EDIT: This is the code I'm using to transform, I just print everything to stdout

    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));

EDIT 2: I just managed to do this in a slightly different way, I embed an xml fragment inside the stylesheet and use an xpath expression on it in

<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)]">
           ....
           ....

Upvotes: 2

Views: 1838

Answers (1)

Rnet
Rnet

Reputation: 5040

I used a xml fragment embedded within the stylesheet:

<specialFiles>
    <name>abcdef.xml</name>
    <name>sadfk32.xml</name>
</specialFiles>

So a simple xpath on it can be used to verify certain name is in the list or not,

<xsl:when test="document('')/xsl:stylesheet/specialFiles/name= $filename">

Upvotes: 3

Related Questions