Domanda

Can you iterate over a flat file with XSL?

I'd like to

ls>myfile

for each line of myfile
   apply-template
È stato utile?

Soluzione

With XSLT 2.0 (for which you need to use an XSLT 2.0 processor like Saxon 9 or AltovaXML or XmlPrime) you can use unparsed-text to read in a plain text file. Then you can process that for instance by using tokenize:

<xsl:template name="main">
   <xsl:for-each select="tokenize(unparsed-text('text1.txt', '\n')">
      <line>
         <xsl:value-of select="."/>
      </line>
  </xsl:for-each>
</xsl:template>

With XSLT 3.0 I think you can even apply-templates to atomic values like strings but I haven't used that so I am not trying to post code doing that. I only mention it because part of your post mentiones apply-templates.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top