【问题标题】:XSLT to create multiple element blocks based on comma-separated stringXSLT 基于逗号分隔的字符串创建多个元素块
【发布时间】:2012-08-08 16:55:13
【问题描述】:

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

源 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. 对于具有n 来宾(由&lt;guestId&gt; 中的逗号分隔值定义)的&lt;reservation&gt; 元素,复制该&lt;reservation&gt; 元素——连同它的后代——n 次,每次使用下一个 guestId 值。
  2. 必须保留原始&lt;guestId&gt; 元素的值,并且必须将其放入新的&lt;csvGuestString&gt; 元素中。
  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>

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

【问题讨论】:

  • @ColinD - 我目前正在研究一种解决方案,该解决方案使用命名标记化模板将 CSV 字符串分开,在这些片段上运行标识模板,但以这样一种方式,每个原始 @复制了 987654331@ 个元素。我的目的是在这里问这个问题,看看是否有人能在我之前提出答案。如果我先到那里,我会发帖;我很想有机会看看我的解决方案是否可以提高效率。
  • @ColinD - 我已经添加了到目前为止我想出的内容。

标签: xslt xslt-1.0


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用 EXSLT 字符串函数的tokenize 方法:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:str="http://exslt.org/strings"
      exclude-result-prefixes="str">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="reservation">
      <xsl:variable name="this" select="."/>
      <xsl:for-each select="str:tokenize(guestId, ',')" >
        <xsl:apply-templates select="$this" mode="copy">
          <xsl:with-param name="id" select="."/>
        </xsl:apply-templates>
      </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="reservation" mode="copy">
      <xsl:param name="id"/>
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="node()">
          <xsl:with-param name="id" select="$id"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="reservation/guestId">
      <xsl:param name="id"/>
      <csvGuestString>
        <xsl:value-of select="."/>
      </csvGuestString>
      <guestId>
        <xsl:value-of select="$id"/>
      </guestId>
    </xsl:template>
    
    </xsl:stylesheet>
    

    这样 xsltproc 将您的输入样本转换为

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

    【讨论】:

    • 有道理:如果您已经在使用 EXSLT,为什么不利用 tokenize?谢谢你的帮助,@Martin。
    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2011-12-06
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多