使用 XSLT 解析文本文件 [英] Parse text file with XSLT

查看:37
本文介绍了使用 XSLT 解析文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下的纯文本文件:

I have a plain text file structured like this:

!ITEM_NAME
Item value
!ANOTHER_ITEM
Its value
...

是否可以使用 XSLT 获取类似于以下内容的文件:

Is it possible to get with XSLT a file similar to:

<?xml version="1.0" encoding="UTF-8" ?>
<document>
  <ITEM_NAME>Item value</ITEM_NAME>
  <ANOTHER_ITEM>Its value</ANOTHER_ITEM>
  ...
</document>

编辑

对不起,我之前没有明确说明.我正在尝试使用 Visual Studio 2005 XSLT 引擎完成这种转换.我已经尝试了提供的两种解决方案,我确信这是正确的.但是 Visual Studio 2005 不知道 unparsed-text 函数.

I am sorry I haven't clearly stated before. I am trying to accomplish this transformation with the Visual Studio 2005 XSLT engine. I have tried both of the provided solutions, and I am sure that are correct. But Visual Studio 2005 doesn't know the unparsed-text function.

推荐答案

如果你可以使用 XSLT 2.0 你可以使用 unparsed-text()...

If you can use XSLT 2.0 you could use unparsed-text()...

文本文件(不要使用文本文件作为 XSLT 的直接输入.)

Text File (Do not use the text file as direct input to the XSLT.)

!ITEM_NAME
Item value
!ANOTHER_ITEM
Its value
!TEST_BANG
Here's a value with !bangs!!!

XSLT 2.0(将此 XSLT 应用于自身(使用样式表作为 XML 输入).您还必须更改文本文件的路径.您可能还必须更改编码.)

XSLT 2.0 (Apply this XSLT to itself (use the stylesheet as the XML input). You'll also have to change the path to your text file. You might have to change the encoding too.)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="text-encoding" as="xs:string" select="'iso-8859-1'"/>
    <xsl:param name="text-uri" as="xs:string" select="'file:///C:/Users/dhaley/Desktop/test.txt'"/>

    <xsl:template name="text2xml">
        <xsl:variable name="text" select="unparsed-text($text-uri, $text-encoding)"/>
        <xsl:analyze-string select="$text" regex="!(.*)\n(.*)">
            <xsl:matching-substring>
                <xsl:element name="{normalize-space(regex-group(1))}">
                    <xsl:value-of select="normalize-space(regex-group(2))"/>
                </xsl:element>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:template>

    <xsl:template match="/">
        <document>
            <xsl:choose>
                <xsl:when test="unparsed-text-available($text-uri, $text-encoding)">
                    <xsl:call-template name="text2xml"/>                                
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="error">
                        <xsl:text>Error reading "</xsl:text>
                        <xsl:value-of select="$text-uri"/>
                        <xsl:text>" (encoding "</xsl:text>
                        <xsl:value-of select="$text-encoding"/>
                        <xsl:text>").</xsl:text>
                    </xsl:variable>
                    <xsl:message><xsl:value-of select="$error"/></xsl:message>
                    <xsl:value-of select="$error"/>
                </xsl:otherwise>
            </xsl:choose>
        </document>
    </xsl:template>
</xsl:stylesheet>

XML 输出

<document>
   <ITEM_NAME>Item value</ITEM_NAME>
   <ANOTHER_ITEM>Its value</ANOTHER_ITEM>
   <TEST_BANG>Here's a value with !bangs!!!</TEST_BANG>
</document>

这篇关于使用 XSLT 解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆