使用未解析文本将文本 XSL 转换为 XML:需要更多深度 [英] XSL transform on text to XML with unparsed-text: need more depth

查看:22
本文介绍了使用未解析文本将文本 XSL 转换为 XML:需要更多深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当好的输入(我不想复制所有数据):

My rather well-formed input (I don't want to copy all data):

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-->

我有以下 XSL,我从 使用 XSLT 解析文本文件

I have the below XSL which I modified from Parse text file with XSLT

<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>

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

and it works fine on parsing the pairs into elements and values. It gives me this output:

<?xml version="1.0" encoding="UTF-8"?>
<Size>Big</Size>
<Colour>Blue</Colour>
<coords>42, 42</coords>

但我还想将值包装在 Thing 标签中,以便我的输出如下所示:

But I'd also like to wrap the values in the Thing tag so that my output looks like this:

<Thing>
    <Size>Big</Size>
    <Colour>Blue</Colour>
    <coords>42, 42</coords>
</Thing>

一个解决方案可能是在每个事物"之后匹配每组行的正则表达式.然后像我已经在做的那样匹配子字符串.或者有没有其他方法来解析树?

One solution might be a regex that matches each group of lines after each "thing". Then matches substrings as I'm already doing. Or is there some other way to parse the tree?

推荐答案

我会使用两个嵌套的 analyze-string 级别,一个外部级别来提取 StartThingEndThing,然后是一个内部的,对与外部匹配的字符串进行操作.

I would use two nested analyze-string levels, an outer one to extract everything between StartThing and EndThing, and then an inner one that operates on the strings matched by the outer one.

<xsl:template name="text2xml">
    <xsl:variable name="text" select="unparsed-text($text-uri, $text-encoding)"/>
    <!-- flags="s" allows .*? to match across newlines -->
    <xsl:analyze-string select="$text" regex="StartThing.*?EndThing" flags="s">
        <xsl:matching-substring>
            <Thing>
                <!-- "." here is the matching substring from the outer regex -->
                <xsl:analyze-string select="." 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>
            </Thing>
        </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:template>

这篇关于使用未解析文本将文本 XSL 转换为 XML:需要更多深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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