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

 手机用户2502855257 发布于 2023-02-07 17:27

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

StartThing
Size Big
Colour Blue
coords 42, 42
foo bar
EndThing
StartThing
Size Small
Colour Red
coords 29, 51
machin bidule
EndThing

我有以下XSL,我使用XSLT从Parse文本文件修改


    
    

    
    

    
        
            
            
                
                    
                          
            
        
    

    
            
    

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


Big
Blue
42, 42

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


    Big
    Blue
    42, 42

一种解决方案可能是在每个"事物"之后匹配每组线的正则表达式.然后匹配我正在做的子串.还是有其他方法来解析树?

1 个回答
  • 我将使用两个嵌套analyze-string级别,一个外部级别来提取StartThing和之间的所有内容EndThing,然后是一个内部操作系统,它对外部匹配的字符串进行操作.

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

    2023-02-07 17:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有