我有点纠结于如何最好地处理以下XML示例:
<Story>
<Content para="div"><local>This is some (normal) text to start with.</local></Content>
<Content para="div"><local>Connect something (</local></Content>
<Content para="div"><local><refnr value="58236"/></local></Content>
<Content para="div"><local>) to something else (</local></Content>
<Content para="div"><local><refnr value="58237"/></local></Content>
<Content para="div"><local>), and make sure it's connected to this (</local></Content>
<Content para="div"><local><refnr value="58239"/></local></Content>
<Content para="div"><local>).</local></Content>
<Content para="div"><local>If that's ok do the same with this (</local></Content>
<Content para="div"><local><refnr value="58238"/></local></Content>
<Content para="div"><local>) also.</local></Content>
<Content para="div"><local>This is some normal text.</local></Content>
<Content para="div"><local>This also.</local></Content>
</Story>
我想要得到的输出如下:
<Story>
<Content para="div"><local>This is some (normal) text to start with.</local></Content>
<Content para="div"><local>Connect something (<refnr value="58236"/>) to something else (<refnr value="58237"/>), and make sure it's connected to this (<refnr value="58239"/>).</local></Content>
<Content para="div"><local>If that's ok do the same with this (<refnr value="58238"/>) also.</local></Content>
<Content para="div"><local>This is some normal text.</local></Content>
<Content para="div"><local>This also.</local></Content>
</Story>
或者,要对其进行"编码":任何以大括号结尾的[Content para=div][local]节点都需要与以下[Content para=div][local]节点合并,直到节点包含最后一个右括号和句子的结尾(由句点标识)。我能够做一些事情,但它变得过于复杂和缓慢,没有所有想要的结果。使用xslt2有什么建议吗?
这个XSLT2.0转换(可以很容易地转换为XSLT1.0):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<Story>
<xsl:apply-templates select="Content[1]/local"/>
</Story>
</xsl:template>
<xsl:template match="Content/local[not(ends-with(., '('))]">
<Content para="div"><local><xsl:apply-templates/></local></Content>
<xsl:apply-templates select="../following-sibling::Content[1]"/>
</xsl:template>
<xsl:template match="Content/local[ends-with(., '(')]">
<Content para="div"><local><xsl:apply-templates/>
<xsl:apply-templates mode="inGroup" select="../following-sibling::Content[1]"/>
</local></Content>
<xsl:apply-templates select=
"../following-sibling::Content
[local[starts-with(.,')') and ends-with(.,'.')]][1]
/following-sibling::Content[1]"/>
</xsl:template>
<xsl:template match="Content/local" mode="inGroup">
<xsl:apply-templates/>
<xsl:apply-templates mode="#current"
select="../following-sibling::Content[1]"/>
</xsl:template>
<xsl:template mode="inGroup" match="Content/local[starts-with(.,')') and ends-with(.,'.')]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="refnr"><xsl:copy-of select="."/></xsl:template>
</xsl:stylesheet>
应用于所提供的XML文档时:
<Story>
<Content para="div"><local>This is some (normal) text to start with.</local></Content>
<Content para="div"><local>Connect something (</local></Content>
<Content para="div"><local><refnr value="58236"/></local></Content>
<Content para="div"><local>) to something else (</local></Content>
<Content para="div"><local><refnr value="58237"/></local></Content>
<Content para="div"><local>), and make sure it's connected to this (</local></Content>
<Content para="div"><local><refnr value="58239"/></local></Content>
<Content para="div"><local>).</local></Content>
<Content para="div"><local>If that's ok do the same with this (</local></Content>
<Content para="div"><local><refnr value="58238"/></local></Content>
<Content para="div"><local>) also.</local></Content>
<Content para="div"><local>This is some normal text.</local></Content>
<Content para="div"><local>This also.</local></Content>
</Story>
生成所需的正确结果:
<Story>
<Content para="div">
<local>This is some (normal) text to start with.</local>
</Content>
<Content para="div">
<local>Connect something (<refnr value="58236"/>) to something else (<refnr value="58237"/>), and make sure it's connected to this (<refnr value="58239"/>).</local>
</Content>
<Content para="div">
<local>If that's ok do the same with this (<refnr value="58238"/>) also.</local>
</Content>
<Content para="div">
<local>This is some normal text.</local>
</Content>
<Content para="div">
<local>This also.</local>
</Content>
</Story
我认为您需要嵌套两种方法:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="Story">
<xsl:copy>
<xsl:for-each-group select="Content" group-adjacent="contains(., '(') or contains(., ')') or boolean(self::Content[local[refnr]])">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:for-each-group select="current-group()" group-ending-with="Content[local[ends-with(., '.')]]">
<Content para="div">
<local>
<xsl:copy-of select="current-group()/local/node()"/>
</local>
</Content>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
有了这个样式表和您的示例输入,我就得到了您发布的结果。然而,我不确定我使用的条件对于实际输入是否太简单,例如,报告是否可以有包含refnr
元素的"正常"文本。
[编辑]使用中的matches
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="Story">
<xsl:copy>
<xsl:for-each-group select="Content"
group-adjacent="not(matches(., '(.*)'))
or boolean(self::Content[local[refnr]])">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:for-each-group select="current-group()" group-ending-with="Content[local[ends-with(., '.')]]">
<Content para="div">
<local>
<xsl:copy-of select="current-group()/local/node()"/>
</local>
</Content>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
编辑后的输入样本会根据需要进行转换。当然,一旦你有了<Content para="div"><local>Connect (foo) something (</local></Content>
,这种方法也太简单了。