gpt4 book ai didi

xslt - 根据换行符拆分字符串并映射到有限数量的元素

转载 作者:行者123 更新时间:2023-12-02 00:36:02 26 4
gpt4 key购买 nike

我有一个问题。

我的输入 XML 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<Text>
<Message>this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
....
....n
</Message>
</Text>

Message中的内容以换行或回车分隔,行数不定。

输出将是:

<?xml version="1.0" encoding="UTF-8" ?> 
<Text>
<Line>this is line 1</Line>
<Line>this is line 2</Line>
<Line>this is line 3</Line>
<Line>this is line 4</Line>
<Line>this is line 5</Line>
<Line>this is line 6</Line>
<Line>this is line 7</Line>
<Line>this is line 8</Line>
<Line>this is line 9</Line>
<Line>this is line 10</Line>
</Text>

我编写了以下 XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:variable name="lineFeed"><xsl:text>&#xA;</xsl:text></xsl:variable>
<xsl:variable name="carriageReturn"><xsl:text>&#xD;</xsl:text></xsl:variable>
<xsl:template match="/">
<Text>
<xsl:if test="Text/Message">
<xsl:choose>
<xsl:when test="contains(Text/Message, $lineFeed)">
<xsl:call-template name="TextWithLineBreaks">
<xsl:with-param name="string" select="Text/Message"/>
<xsl:with-param name="delimiter" select="$lineFeed"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains(Text/Message, $carriageReturn)">
<xsl:call-template name="TextWithLineBreaks">
<xsl:with-param name="string" select="Text/Message"/>
<xsl:with-param name="delimiter" select="$carriageReturn"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<Line>
<xsl:value-of select="Text/Message"/>
</Line>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</Text>
</xsl:template>

<xsl:template name="TextWithLineBreaks">
<xsl:param name="string"/>
<xsl:param name="delimiter"/>
<xsl:variable name="Result">
<xsl:call-template name="extract-bodytext">
<xsl:with-param name="GetString" select="$string"/>
<xsl:with-param name="Separator" select="$delimiter"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$Result"/>
</xsl:template>

<xsl:template name="extract-bodytext">
<xsl:param name="GetString"/>
<xsl:param name="Separator"/>
<xsl:choose>
<xsl:when test="contains($GetString, $Separator)">
<xsl:variable name="firstline" select="substring-before($GetString, $Separator)"/>
<xsl:if test="string-length($firstline) > 0">
<Line>
<xsl:value-of select="substring-before($GetString, $Separator)"/>
</Line>
</xsl:if>
<xsl:call-template name="extract-bodytext">
<xsl:with-param name="GetString">
<xsl:value-of select="substring-after($GetString,$Separator)"/>
</xsl:with-param>
<xsl:with-param name="Separator" select="$lineFeed"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$GetString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

现在我的问题是,对于输出,我只能映射到最大数量 10 。使用上面的 XSL,它将映射到输入中存在的任意行数。

有什么建议吗?

谢谢丁

最佳答案

这是一种方法:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/*">
<Text>
<xsl:apply-templates/>
</Text>
</xsl:template>

<xsl:template match="text()" name="wrapLines">
<xsl:param name="pText" select="."/>
<xsl:param name="pNumLines" select="10"/>

<xsl:if test=
"string-length($pText) and $pNumLines > 0">
<xsl:variable name="vLine" select=
"substring-before(concat($pText,'&#xA;'), '&#xA;')"/>
<Line>
<xsl:value-of select="$vLine"/>
</Line>

<xsl:call-template name="wrapLines">
<xsl:with-param name="pNumLines" select="$pNumLines -1"/>
<xsl:with-param name="pText" select=
"substring-after($pText, '&#xA;')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(包含超过 10 行):

<Text>
<Message>this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
this is line 8
this is line 9
this is line 10
this is line 11
</Message>
</Text>

产生了想要的正确结果:

<Text>
<Line>this is line 1</Line>
<Line> this is line 2</Line>
<Line> this is line 3</Line>
<Line> this is line 4</Line>
<Line> this is line 5</Line>
<Line> this is line 6</Line>
<Line> this is line 7</Line>
<Line> this is line 8</Line>
<Line> this is line 9</Line>
<Line> this is line 10</Line>
</Text>

解决方案 2:

使用FXSL的str-split-to-words模板/函数,可以简单地编写:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>

<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>

<xsl:param name="pmaxLines" select="10"/>

<xsl:template match="/">
<Text>
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="'&#10;&#13;'"/>
</xsl:call-template>
</xsl:variable>

<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*[not(position() > $pmaxLines)]"/>
</Text>
</xsl:template>

<xsl:template match="word">
<Line><xsl:value-of select="."/></Line>
</xsl:template>
</xsl:stylesheet>

当此转换应用于与上述相同的 XML 文档时,就会产生所需的正确结果:

<Text>
<Line>this is line 1</Line>
<Line> this is line 2</Line>
<Line> this is line 3</Line>
<Line> this is line 4</Line>
<Line> this is line 5</Line>
<Line> this is line 6</Line>
<Line> this is line 7</Line>
<Line> this is line 8</Line>
<Line> this is line 9</Line>
<Line> this is line 10</Line>
</Text>

关于xslt - 根据换行符拆分字符串并映射到有限数量的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5200147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com