XSL transform on text to XML with unparsed-text: need more depth

旧街凉风 提交于 2019-12-01 21:36:21

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!