根据关键字/分隔符将元素中的文本拆分为多个元素



my source xml

<event>
<description>Special Notice - 20190305</description>
<note>[Subject]: This is the subject of the event
[Purpose]: This is the purpose of the event
[Evaluation]: This is an evaluation of the event
[Strategy]: This is the strategy for the event</note>
</event>
<event>
<description>Notice</description>
<note>This is a notice</note>
</event>

结果应该是什么样子的

<instance>
<title>
<text>Purpose</text>
</title>
<data>This is the purpose of the event</data>
</instance>
<instance>
<title>
<text>Subject</text>
</title>
<data>This is the subject of the event</data>
</instance>
<instance>
<title>
<text>Purpose</text>
</title>
<data>This is the purpose of the event</data>
</instance>
<instance>
<title>
<text>Notice</text>
</title>
<data>This is a notice</data>
</instance>
etc.

我对 xslt 很陌生,并且在练习期间被困在某件事上 - 我知道我想做什么,但我在弄清楚从哪里开始时遇到了问题。我希望将包含主题、目的、评估和策略的注释元素中的文本拆分为每个实例的单独注释。会有其他内容的注释,但我在这里的问题专门针对这些特定的注释。

源 xml 中的每个注释元素都应该将句子的部分括在方括号中作为目标 xml 中的标题;冒号后面的任何内容也放在数据元素下。我的挑战是弄清楚如何解析 note 元素下的内容并正确传递每一行。我想使用带有某种正则表达式的 for-each 来抓取每个方括号中包含的任何内容,但不确定这是否可能?也许标记化?然后我想到使用子字符串之前和子字符串之后分别传递给标题和数据元素。

编辑:只是添加一些背景,因为丹尼尔建议使用分析字符串。如上面粗体所示,有些事件元素不需要拆分。我在源和目标 xml 中添加了一个示例。对于这些描述和注释,应分别转到文本和数据。

正如我在回复 Daniel 时提到的,我的想法是,从描述和注释到文本和数据,可以在非匹配子字符串中获取 value-of。

编辑2:这是我如何考虑@DanielHaley做这件事的一个例子。正如我之前给您的回复中所述,我通过我的较大文档(我没有发布整个文档,因为它很长并且对问题来说是多余的(使用 for-each 来循环浏览事件和其他元素在公共父元素下。

<xsl:for-each select="event">
<xsl:choose>
<xsl:when test="contains(description,'Special')">
<xsl:analyze-string select="note" regex="[([^]]+)]:s*([^[]*)">
<xsl:matching-substring>
<title><text><xsl:value-of select="normalize-space(regex-group(1))"/></text></title>
<data><xsl:value-of select="normalize-space(regex-group(2))"/></data>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<title><text><xsl:value-of select="description"></text></title>
<data><xsl:value-of select="note"></data>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>

我可能会使用 xsl:analyze-string...

XML 输入(对于测试正则表达式来说有点混乱(

<doc>
<event>
<description>Special Notice - 20190305</description>
<note>[Subject]: This is the subject of the event
[Purpose]: This is the purpose 
of the event [Evaluation]: This is an evaluation of the event
[Strategy]:
This is the strategy for the event</note>
</event>
<event>
<description>Notice</description>
<note>This is a notice</note>
</event>
</doc>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="event[matches(note,'[[^]]+]')]">
<xsl:analyze-string select="note" regex="[([^]]+)]:s*([^[]*)">
<xsl:matching-substring>
<instance>
<title>
<text>
<xsl:value-of select="normalize-space(regex-group(1))"/>
</text>
</title>
<data>
<xsl:value-of select="normalize-space(regex-group(2))"/>
</data>
</instance>
</xsl:matching-substring>
<xsl:non-matching-substring>
<!--This shouldn't trigger. If it does, you'll need to figure out
how you want to handle the differences with the existing pattern.-->
<xsl:message terminate="yes" 
select="concat('Non-matching substring: ''',.,'''')"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="event">
<instance>
<title>
<text>
<xsl:value-of select="description"/>
</text>
</title>
<data>
<xsl:value-of select="note"/>
</data>
</instance>
</xsl:template>
</xsl:stylesheet>

XML 输出

<doc>
<instance>
<title>
<text>Subject</text>
</title>
<data>This is the subject of the event</data>
</instance>
<instance>
<title>
<text>Purpose</text>
</title>
<data>This is the purpose of the event</data>
</instance>
<instance>
<title>
<text>Evaluation</text>
</title>
<data>This is an evaluation of the event</data>
</instance>
<instance>
<title>
<text>Strategy</text>
</title>
<data>This is the strategy for the event</data>
</instance>
<instance>
<title>
<text>Notice</text>
</title>
<data>This is a notice</data>
</instance>
</doc>

小提琴:http://xsltfiddle.liberty-development.net/94AbWAA

最新更新