返回节点中一次使用的所有 XML 元素 [英] Return all XML element taken one time in a node

查看:20
本文介绍了返回节点中一次使用的所有 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个节点在 LIGHT_COLOUR 字段中包含一个颜色列表,由字母 R(红色)、W(白色)等标识.在 LIGHT_COLOUR 字段中,只能是一种颜色/字母,也可以是多种颜色/字母,以-"分隔.

Each node contains a list of colour in the field LIGHT_COLOUR, identied by a letter R(red), W(white), etc etc. In the field LIGHT_COLOUR can be only one colour/letter or more than one separated by '-'.

这是我用于测试的 XML:

This is my XML for testing:

<LIGHT_INFORMATION_LIST>
    <LIGHT_INFORMATION>
        <LIGHT_CHARACTERISTICS>Al</LIGHT_CHARACTERISTICS>
        <LIGHT_COLOUR>W-G</LIGHT_COLOUR>
    </LIGHT_INFORMATION>
    <LIGHT_INFORMATION>
        <LIGHT_CHARACTERISTICS>Al</LIGHT_CHARACTERISTICS>
        <LIGHT_COLOUR>W-R</LIGHT_COLOUR>
    </LIGHT_INFORMATION>
    <LIGHT_INFORMATION>
        <LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
        <LIGHT_COLOUR>R</LIGHT_COLOUR>
    </LIGHT_INFORMATION>
    <LIGHT_INFORMATION>
        <LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
        <LIGHT_COLOUR>G</LIGHT_COLOUR>
    </LIGHT_INFORMATION>
    <LIGHT_INFORMATION>
        <LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
        <LIGHT_COLOUR>W</LIGHT_COLOUR>
    </LIGHT_INFORMATION>
</LIGHT_INFORMATION_LIST>

这是我的 XSLT:

<xsl:for-each select="LIGHT_INFORMATION_LIST">
    <xsl:for-each select="LIGHT_INFORMATION">
        <xsl:for-each select="LIGHT_COLOUR">
            <xsl:value-of select="."/>
        </xsl:for-each> 
    </xsl:for-each> 
</xsl:for-each> 

现在我的输出是:W-G W-R R G W
这是 LIGHT_COLOUR 字段中的所有元素.

Now my output is : W-G W-R R G W
This is all element in LIGHT_COLOUR field.

我会在输出所有元素一次(LIGHT_COLOUR 不同).

I would in Output all element taken one time (LIGHT_COLOUR distinct).

在输出中我会:W G R

In Output I would : W G R

推荐答案

一种方法是首先创建一个变量,其中包含连接成一个字符串的所有单独的 LIGHT_COLOUR 元素

One way to do this would be first to create a variable containing all the separate LIGHT_COLOUR elements concatenated into one string

  <xsl:variable name="allColours">
     <xsl:for-each select="LIGHT_INFORMATION_LIST/LIGHT_INFORMATION">
        <xsl:value-of select="concat(LIGHT_COLOUR, '-')" />
     </xsl:for-each>
  </xsl:variable>

因此,在您的示例中,它将设置为W-G-T-W-R-E-E-R-E-E-".

So, in your example, it would be set to "W-G-T-W-R-E-E-R-E-E-".

然后,您可以使用递归模板将字符串拆分为字符,但同时保留不同值的运行列表.因此,在每次迭代中,它都会获取连字符之前的下一个字符,并检查它是否不在不同值的列表中.如果没有,则将其添加到此列表中,递归模板继续处理,直到没有任何东西为止.

You could then have a recursive template that splits the string into characters, but at the same time keeps a running list of the distinct values. So, on each iteration, it gets the next character before the hyphen and checks whether it is not already in the list of distinct values. If not, it adds it to this list, and the recursive template carries on processing until nothing left.

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <xsl:variable name="allColours">
         <xsl:for-each select="LIGHT_INFORMATION_LIST/LIGHT_INFORMATION">
            <xsl:value-of select="concat(LIGHT_COLOUR, '-')" />
         </xsl:for-each>
      </xsl:variable>

      <xsl:call-template name="tokenizeDistinct">
         <xsl:with-param name="string" select="$allColours" />
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="tokenizeDistinct">
      <xsl:param name="string" />
      <xsl:param name="splitChar" select="'-'" />
      <xsl:param name="distinctList" select="' '" />

      <xsl:variable name="nextChar" select="substring-before(concat($string, $splitChar), $splitChar)" />
      <xsl:variable name="newDistinctList">
         <xsl:value-of select="$distinctList" />
         <xsl:if test="$nextChar != '' and not(contains($distinctList, concat(' ', $nextChar, ' ')))">
            <xsl:value-of select="concat($nextChar, ' ')" />
         </xsl:if>
      </xsl:variable>
      <xsl:choose>
         <xsl:when test="contains($string, $splitChar)">
            <xsl:call-template name="tokenizeDistinct">
               <xsl:with-param name="string" select="substring-after($string, $splitChar)" />
               <xsl:with-param name="splitChar" select="$splitChar" />
               <xsl:with-param name="distinctList" select="$newDistinctList" />
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$newDistinctList" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

应用于您的 XML 时,输出如下

When applied to your XML, the following is output

W G R 

这篇关于返回节点中一次使用的所有 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆