Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xml - How to read plain text content with XSLT 1.0

The source file contains

Hello World, this is a nice world

The output desired applying the XSLT to the input file:

<Hello_World message="this is a nice world"/>

I know I can use unparsed-text in XSLT 2.0 easily, but I need do it with XSLT 1.0.

I browsed a while, and I can't find something useful.

Is it posible? I need to use Xalan XSLT processor.

I think this question is challenging.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is not possible for the input document to be plain text because the input to an XSLT 1.0 transformation must be well-formed XML.

Here are some alternative ways to access plain text in an XSLT transformation:

  • Use unparsed-text in XSLT 2.0.
  • Pass the plain text in via top-level parameters (xsl:param).
  • Preprocess the text file to turn it into a well-formed XML document.
  • Generate the XSLT file dynamically, possibly via a meta XSLT transformation, and include the plain text directly in the XSLT source. Then just use a dummy XML input file.
  • Reference the text file as an external entity in a wrapper XML document, and then process the wrapper XML document using XSLT.

Here's an example of the external entity technique:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wrapper [
<!ENTITY textFile SYSTEM "file.txt">
]>
<wrapper>&textFile;</wrapper>

(Note that this last option could be challenging given XSLT 1.0's limited string processing abilities, but for some data, it may be viable.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...