tags:

views:

399

answers:

4

I'm working with a system (Maximo) that generates a text file. I need to remove just the first line of the file. The way to do that is using XSLT.

Any idea?

A: 

The way to do that is not using XSLT.

XSLT can produce text files, but it cannot process text files. It can only process well-formed XML.

Ben Blank
The way to do that is not using XSLT. -- Agreed but right now it's the only viable option.
NitroxDM
@NitroxDM — That's what each of the answers here is trying to say — *if XSLT is the **only** tool you have, it cannot be done*. You **must** involve some other tool to accomplish your goal, and if you can involve another tool at all, then you have an avenue to use something more appropriate than XSLT.
Ben Blank
@Ben Blank - An XSLT solution IS TOTALLY possible. See my answer.
Mads Hansen
A: 

XSLT will only take a valid XML file as input, not a general text file. It can output text, though.

(I use XSLT to generate C code, for example.)

0x6adb015
XSLT 2.0 can process non-XML files using the unparsed-text() function and XSLT 1.0 can be tricked into working with plain text by using it as an entity reference.
Mads Hansen
A: 

If your XSLT processor supports any-to-any transformation(binary xforms via FFDs - Flat File Descriptors), there is a possibility of doing this. You can wrap your text in a node and then operate on that node using a regular XSLT template to output whatever is after the first carriage return.

Thiyagaraj
Could one wrap the contents of the text file into a node using XSLT?
NitroxDM
@NitroxDM — Catch-22. If a file needs to be processed before you can use it with XSLT, you cannot use XSLT to do that processing.
Ben Blank
Depending on your processor/platform - WebSphere DataPower (an SOA Appliance) supports that - https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14107417
Thiyagaraj
+3  A: 

Yes, you can accomplish what you want in XSLT!

It would probably be easier to do so in XSLT 2.0, if that is an option for you. Michael Kay answered a similar question on the XSL mailing list in 2005.

Paraphrasing his answer, with small examples:

In XSLT 2.0,: you can use the unparsed-text() function to read the file, tokenize() to split it into lines (and just ignore the first line).

<xsl:for-each select="tokenize(unparsed-text($in), '\r?\n')">
 ...
</xsl:for-each>

In XSLT 1.0: you can read a flat text file by pretending that it's an XML external entity, and referencing it from an XML document that causes the entity to be expanded.

<!DOCTYPE foo [
<!ENTITY bar SYSTEM "bar.txt">
]>
<foo>
&bar;
</foo>
Mads Hansen
Very cool trick with the entity reference! I hope it works for him. :-)
Ben Blank