相关疑难解决方法(0)

使用XSLT解析文本文件

我有一个像这样结构的纯文本文件:

!ITEM_NAME
Item value
!ANOTHER_ITEM
Its value
...
Run Code Online (Sandbox Code Playgroud)

是否可以使用类似于XSLT的文件:

<?xml version="1.0" encoding="UTF-8" ?>
<document>
  <ITEM_NAME>Item value</ITEM_NAME>
  <ANOTHER_ITEM>Its value</ANOTHER_ITEM>
  ...
</document>
Run Code Online (Sandbox Code Playgroud)

编辑

对不起,我之前没有明确说过.我试图使用Visual Studio 2005 XSLT引擎完成此转换.我已经尝试了两种提供的解决方案,我确信这是正确的.但Visual Studio 2005不知道未解析的文本功能.

xml xslt text visual-studio-2005

7
推荐指数
2
解决办法
1万
查看次数

使用未解析的文本将文本XSL转换为XML:需要更多深度

我的格式很好(我不想复制所有数据):

StartThing
Size Big
Colour Blue
coords 42, 42
foo bar
EndThing
StartThing
Size Small
Colour Red
coords 29, 51
machin bidule
EndThing
<!-- repeat a few thousand times-->
Run Code Online (Sandbox Code Playgroud)

我有以下XSL,我使用XSLTParse文本文件修改

<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="'unparsed-text.txt'"/>

    <xsl:template name="text2xml">
        <xsl:variable name="text" select="unparsed-text($text-uri, $text-encoding)"/>
        <xsl:analyze-string select="$text" regex="(Size|Colour|coords) (.+)">    
            <xsl:matching-substring>
                <xsl:element name="{(regex-group(1))}">
                    <xsl:value-of select="(regex-group(2))"/>
                </xsl:element>          
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:template>

    <xsl:template match="/">
        <xsl:call-template name="text2xml"/>    
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

它可以很好地将对解析成元素和值.它给了我这个输出:

<?xml version="1.0" encoding="UTF-8"?> …
Run Code Online (Sandbox Code Playgroud)

xslt xslt-2.0

3
推荐指数
1
解决办法
3074
查看次数

标签 统计

xslt ×2

text ×1

visual-studio-2005 ×1

xml ×1

xslt-2.0 ×1