使用xslt

时间:2018-01-18 11:06:31

标签: xslt

我需要将带有逗号分隔字符串的两个标记拆分为父子标记列表,如下所示。

例如,输入将是: -

<UserID>162,163</UserID>
<UserName>Stacy,Stephen</UserName>

预期产量: - Expected Output 请使用xslt代码

帮助实现此结果

我尝试了从另一个查询获得的以下格式,但它生成嵌套模式而不是列表: -

<xsl:template name="tokenize">
    <xsl:param name="textID" select="."/>
    <xsl:param name="textName" select="."/>
    <xsl:param name="separator" select="','"/>
  <User>
    <xsl:choose>
      <xsl:when test="not(contains($textID, $separator))">
        <ID>
          <xsl:value-of select="normalize-space($textID)"/>
        </ID>
      </xsl:when>
      <xsl:otherwise>
        <ID>
          <xsl:value-of select="normalize-space(substring-before($textID, $separator))"/>
        </ID>
        <xsl:call-template name="tokenize">
          <xsl:with-param name="textID" select="substring-after($textID, $separator)"/>
          <xsl:with-param name="textName" select="substring-after($textName, $separator)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="not(contains($textName, $separator))">
        <Name>
          <xsl:value-of select="normalize-space($textName)"/>
        </Name>
      </xsl:when>
      <xsl:otherwise>
        <Name>
          <xsl:value-of select="normalize-space(substring-before($textName, $separator))"/>
        </Name>
        <xsl:call-template name="tokenize">
          <xsl:with-param name="textID" select="substring-after($textID, $separator)"/>
          <xsl:with-param name="textName" select="substring-after($textName, $separator)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </User>
</xsl:template> 

1 个答案:

答案 0 :(得分:1)

在简化XSLT代码中共享的tokenize模板之前,几乎没有什么假设。

  1. comma<UserID><UserName>个分隔值的计数始终相等。
  2. 值为162 <-> Stacy163 <-> Stephen的索引有1-1对应关系。
  3. XSLT版本为1.0
  4. 已添加父节点<UserList>作为共享输入XML的根节点。
  5. XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
         <xsl:strip-space elements="*" />
    
         <xsl:template match="UserList">
            <xsl:copy>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="textID" select="normalize-space(UserID)" />
                    <xsl:with-param name="textName" select="normalize-space(UserName)" />
                </xsl:call-template>
            </xsl:copy>
         </xsl:template>
    
        <xsl:template name="tokenize">
            <xsl:param name="textID" />
            <xsl:param name="textName" />
            <xsl:param name="separator" select="','" />
    
            <xsl:choose>
                <xsl:when test="not(contains($textID, $separator) and contains($textName, $separator)) ">
                    <User>
                        <ID><xsl:value-of select="$textID" /></ID>
                        <Name><xsl:value-of select="$textName" /></Name>
                    </User>
                </xsl:when>
                <xsl:otherwise>
                    <User>
                        <ID><xsl:value-of select="substring-before($textID, $separator)" /></ID>
                        <Name><xsl:value-of select="substring-before($textName, $separator)" /></Name>
                    </User>
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="textID" select="normalize-space(substring-after($textID, $separator))" />
                        <xsl:with-param name="textName" select="normalize-space(substring-after($textName, $separator))" />
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template> 
    </xsl:stylesheet>
    

    输出

    <UserList>
        <User>
            <ID>162</ID>
            <Name>Stacy</Name>
        </User>
        <User>
            <ID>163</ID>
            <Name>Stephen</Name>
        </User>
    </UserList>
    
相关问题