编辑:我有一个解决方案,但我相信有更好的方法。请看下面。XSLT基于逗号分隔的字符串创建多个元素块
源XML:
<?xml version="1.0"?>
<reservations>
<reservation>
<id>1</id>
<guestId>1111</guestId>
<!-- other fields -->
</reservation>
<reservation>
<id>2</id>
<guestId>2222,3333,4444</guestId>
<!-- other fields -->
</reservation>
</reservations>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<reservations>
<reservation>
<id>1</id>
<csvGuestString>1111</csvGuestString>
<guestId>1111</guestId>
<!-- other fields -->
</reservation>
<reservation>
<id>2</id>
<csvGuestString>2222,3333,4444</csvGuestString>
<guestId>2222</guestId>
<!-- other fields -->
</reservation>
<reservation>
<id>2</id>
<csvGuestString>2222,3333,4444</csvGuestString>
<guestId>3333</guestId>
<!-- other fields -->
</reservation>
<reservation>
<id>2</id>
<csvGuestString>2222,3333,4444</csvGuestString>
<guestId>4444</guestId>
<!-- other fields -->
</reservation>
</reservations>
规则:
- 对于
<reservation>
元素公顷如果客户(由<guestId>
中的逗号分隔值定义),则每次都使用下一个guestId值复制该元素 - 以及其后代 -n
次。 - 原始的
<guestId>
元素的值必须保留,并且必须放置在新元素<csvGuestString>
中。 - 必须在XSLT 1.0中完成。
- 完美合理地使用EXSLT进行标记。
我有什么到目前为止(它的工作原理,但如果它是不知道的最有效的解决方案):
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vTokenName" select="'token'"/>
<xsl:variable name="vDoc" select="/"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="guestId">
<csvGuestString>
<xsl:apply-templates />
</csvGuestString>
</xsl:template>
<xsl:template match="reservation">
<xsl:variable name="vGuestRtfPass1">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="guestId"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($vGuestRtfPass1)/*" mode="pass2">
<xsl:with-param name="pPosition" select="position()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="token" mode="pass2">
<xsl:param name="pPosition" />
<reservation>
<xsl:apply-templates select="$vDoc/*/reservation[$pPosition]/*" />
<guestId>
<xsl:apply-templates />
</guestId>
</reservation>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text,$delimiter)">
<xsl:element name="{$vTokenName}">
<xsl:value-of select="substring-before($text,$delimiter)"/>
</xsl:element>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$text">
<xsl:element name="{$vTokenName}">
<xsl:value-of select="$text"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
和往常一样,感谢您的帮助。
@ColinD - 我目前工作的一个解决方案使用命名的标记化模板将CSV字符串分开,在这些片段上运行标识模板,但以这种方式每个原始的''元素被复制。我的意图是在这里问这个问题,看看有人能在我做之前想出答案。我应该首先到达那里,我会发布;我会津津乐道这个机会,看看我的解决方案是否可以提高效率。 –
ABach
2012-08-08 17:02:18
@ColinD - 我已经添加了我到目前为止所提出的。 – ABach 2012-08-08 17:20:15