XSLT 基于逗号分隔字符串创建多个元素 block

标签 xslt xslt-1.0

编辑:我有一个解决方案,但我确信还有更好的方法。请参阅下文。

源 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>

规则:

  1. 对于<reservation>具有 n 的元素guest(由 <guestId> 中的逗号分隔值定义),复制 <reservation>元素 -- 及其后代 -- n次,每次都使用下一个 guestId 值。
  2. 原文<guestId>元素的值必须保留并且必须放置在新的 <csvGuestString> 中元素。
  3. 必须在 XSLT 1.0 中完成。
  4. 使用 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>

一如既往,感谢您的帮助。

最佳答案

不需要使用任何 tokenize() 扩展函数,并且此转换可以在仅具有 xxx 的 XSLT 处理器上运行: node-set()扩展函数:

<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:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="@*|node()" name="identity">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="reservation/guestId">
   <xsl:call-template name="identity"/>
  <csvGuestString><xsl:value-of select="."/></csvGuestString>
 </xsl:template>

 <xsl:template match="reservation[contains(guestId, ',')]" name="explode">
   <xsl:param name="pCurrent" select="."/>
   <xsl:param name="pLastId" select="substring-before($pCurrent/guestId, ',')"/>

   <xsl:variable name="vrtfResult">
     <xsl:apply-templates select="$pCurrent" mode="explode"/>
   </xsl:variable>
   <xsl:copy-of select="$vrtfResult"/>

   <xsl:variable name="vResult" select="ext:node-set($vrtfResult)/*"/>

   <xsl:if test="contains(substring-after($vResult/csvGuestString, $vResult/guestId), ',')">
     <xsl:call-template name="explode">
       <xsl:with-param name="pCurrent" select="$vResult"/>
       <xsl:with-param name="pLastId" select="$vResult/guestId"/>
     </xsl:call-template>
   </xsl:if>
 </xsl:template>

 <xsl:template match="node()" mode="explode">
  <xsl:call-template name="identity"/>
 </xsl:template>

 <xsl:template match="reservation" mode="explode">
  <xsl:copy>
   <xsl:apply-templates select="node()" mode="explode"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="guestId[contains(.,',')]" mode="explode">
  <csvGuestString><xsl:value-of select="."/></csvGuestString>
  <guestId><xsl:value-of select="substring-before(., ',')"/></guestId>
 </xsl:template>

 <xsl:template match="guestId" mode="explode">
  <guestId>
    <xsl:value-of select="substring-before(substring-after(concat(../csvGuestString, ','), concat(current(),',')), ',')"/>
  </guestId>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<reservations>
  <reservation>
    <id>1</id>
    <guestId>1111</guestId>
    <!-- other fields -->
  </reservation>
  <reservation>
    <id>2</id>
    <guestId>2222,3333,4444</guestId>
    <!-- other fields -->
  </reservation>
</reservations>

产生了想要的正确结果:

<reservations>
  <reservation>
    <id>1</id>
    <guestId>1111</guestId>
    <csvGuestString>1111</csvGuestString>
    <!-- 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>

关于XSLT 基于逗号分隔字符串创建多个元素 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11869441/

相关文章:

XSLT:将 URL 查询字符串作为参数传递

xslt - XSLT 中的动态排序?

xml - 如何在 XSLT 1.0 中迭代 IDREFS 值?

xslt - 在 XSLT 1.0 中根据批量大小输出新行

XSLT 查找和替换回车

xml - 在 XSL :FO 中添加方形复选框

xml - 如何在 XSLT 中拆分数据并存储在多个节点中

xslt - 如何从元素中的文本末尾删除空格。 (XSL)

xml - 使用 XSLT 从另一个 xml 文件获取 XML 节点描述

xml - 如何使用 Microsoft xslt 1.0 高效地稍微修改大型 xml 文档