如何使用 xsl 连接具有两个不同分隔符的字符串

发布于 2024-12-01 13:28:45 字数 2673 浏览 0 评论 0原文

我有一个 XML

<main>
  <DATA_RECORD>
    <COMPONENT_SID>100</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>200</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>400</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>10</COMPONENT_SID>
    <GROUP_ID>2</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>20</COMPONENT_SID>
    <GROUP_ID>2</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>2</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>4</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>8</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>16</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  </main>

,我想使用 xsl 解析为另一个 XML。输出将是

<comp value="100,200,400|10,20|2,4,8,16"/> 

属于不同 group_id 的 component_sids 用“|”分隔。属于同一组id的component_sids应该用“,”连接。我使用了以下xsl,

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
          <xsl:element name="comp">
             <xsl:attribute name="value">
                 <xsl:call-template name="join"> 
                        <xsl:with-param name="list" select="//DATA_RECORD[GROUP_ID=1]/COMPONENT_SID" /> 
                        <xsl:with-param name="separator" select="','" />                                             
                  </xsl:call-template> 
              </xsl:attribute>
        </xsl:element>
  </xsl:template>

   <xsl:template name="join"> 
    <xsl:param name="list" /> 
    <xsl:param name="separator"/>     
    <xsl:for-each select="$list"> 
      <xsl:value-of select="." /> 
      <xsl:if test="position() != last()"> 
        <xsl:value-of select="$separator" />         
      </xsl:if> 
    </xsl:for-each> 
  </xsl:template> 
</xsl:stylesheet>

结果是

<comp value="100,200,400"/>

但我无法弄清楚如何用“|”分隔其他组的component_sid。有人可以帮助我吗?

提前致谢

I have a XML

<main>
  <DATA_RECORD>
    <COMPONENT_SID>100</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>200</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>400</COMPONENT_SID>
    <GROUP_ID>1</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>10</COMPONENT_SID>
    <GROUP_ID>2</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>20</COMPONENT_SID>
    <GROUP_ID>2</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>2</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>4</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>8</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  <DATA_RECORD>
    <COMPONENT_SID>16</COMPONENT_SID>
    <GROUP_ID>3</GROUP_ID>
  </DATA_RECORD>
  </main>

I would like to use xsl to parse into another XML. The output would be

<comp value="100,200,400|10,20|2,4,8,16"/> 

where the component_sids belonged to different group_id are separated by "|" . The component_sids belonged to the same group id should be concatenated with ",". I used the following xsl

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
          <xsl:element name="comp">
             <xsl:attribute name="value">
                 <xsl:call-template name="join"> 
                        <xsl:with-param name="list" select="//DATA_RECORD[GROUP_ID=1]/COMPONENT_SID" /> 
                        <xsl:with-param name="separator" select="','" />                                             
                  </xsl:call-template> 
              </xsl:attribute>
        </xsl:element>
  </xsl:template>

   <xsl:template name="join"> 
    <xsl:param name="list" /> 
    <xsl:param name="separator"/>     
    <xsl:for-each select="$list"> 
      <xsl:value-of select="." /> 
      <xsl:if test="position() != last()"> 
        <xsl:value-of select="$separator" />         
      </xsl:if> 
    </xsl:for-each> 
  </xsl:template> 
</xsl:stylesheet>

The result is

<comp value="100,200,400"/>

But I could not figured out how to separate other groups of component_sid with "|". Can someone help me?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会发光的星星闪亮亮i 2024-12-08 13:28:46

XSLT 2.0 解决方案可以进一步缩短为:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
  <xsl:variable name="vResult">
   <xsl:for-each-group select="*/*" group-by="GROUP_ID">
    <xsl:if test="not(position()=1)">|</xsl:if>
    <xsl:value-of select="current-group()/COMPONENT_SID" separator=","/>
   </xsl:for-each-group>
  </xsl:variable>

  <comp value="{$vResult}"/>
 </xsl:template>
</xsl:stylesheet>

The XSLT 2.0 solution can be shortened further to:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
  <xsl:variable name="vResult">
   <xsl:for-each-group select="*/*" group-by="GROUP_ID">
    <xsl:if test="not(position()=1)">|</xsl:if>
    <xsl:value-of select="current-group()/COMPONENT_SID" separator=","/>
   </xsl:for-each-group>
  </xsl:variable>

  <comp value="{$vResult}"/>
 </xsl:template>
</xsl:stylesheet>
风筝在阴天搁浅。 2024-12-08 13:28:46

我。 XSLT 1.0 解决方案

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

 <xsl:key name="kRecByGrId" match="DATA_RECORD"
                            use="GROUP_ID"/>

 <xsl:variable name="vGrIds" select=
 "/*/DATA_RECORD
      [generate-id()
      =
       generate-id(key('kRecByGrId', GROUP_ID)[1])
       ]
        /GROUP_ID
 "/>

 <xsl:template match="/">
   <xsl:variable name="vResult">
       <xsl:for-each select="$vGrIds">
        <xsl:if test="not(position()=1)">|</xsl:if>
        <xsl:apply-templates select=
          "key('kRecByGrId', .)/COMPONENT_SID"/>
       </xsl:for-each>
     </xsl:variable>

     <comp value="{$vResult}"/>
 </xsl:template>

 <xsl:template match="COMPONENT_SID">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<main>
    <DATA_RECORD>
        <COMPONENT_SID>100</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>200</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>400</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>10</COMPONENT_SID>
        <GROUP_ID>2</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>20</COMPONENT_SID>
        <GROUP_ID>2</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>2</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>4</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>8</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>16</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
</main>

产生所需的正确结果

<comp value="100,200,400|10,20|2,4,8,16"/>

说明

  1. < strong>Muenchian 分组方法 -- 查找 GROUP_ID 的所有不同值。

  2. 只要该项目(或组)不是第一个,就在项目(或组)之前添加分隔符的简单逻辑

二. XSLT 2.0 解决方案:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:variable name="vResult">
   <xsl:for-each-group select="*/*" group-by="GROUP_ID">
    <xsl:if test="not(position()=1)">|</xsl:if>
    <xsl:for-each select="current-group()/COMPONENT_SID">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select="."/>
    </xsl:for-each>
   </xsl:for-each-group>
  </xsl:variable>

     <comp value="{$vResult}"/>
 </xsl:template>
</xsl:stylesheet>

当应用于同一个 XML 文档(上面)时,再次生成所需的正确结果

<comp value="100,200,400|10,20|2,4,8,16"/>

说明

  1. 使用 < code>

  2. 使用current-group()

  3. 在每个项目(或组)之前使用相同的简单逻辑,但不是第一个,带有相应的分隔符。

I. XSLT 1.0 solution:

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

 <xsl:key name="kRecByGrId" match="DATA_RECORD"
                            use="GROUP_ID"/>

 <xsl:variable name="vGrIds" select=
 "/*/DATA_RECORD
      [generate-id()
      =
       generate-id(key('kRecByGrId', GROUP_ID)[1])
       ]
        /GROUP_ID
 "/>

 <xsl:template match="/">
   <xsl:variable name="vResult">
       <xsl:for-each select="$vGrIds">
        <xsl:if test="not(position()=1)">|</xsl:if>
        <xsl:apply-templates select=
          "key('kRecByGrId', .)/COMPONENT_SID"/>
       </xsl:for-each>
     </xsl:variable>

     <comp value="{$vResult}"/>
 </xsl:template>

 <xsl:template match="COMPONENT_SID">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<main>
    <DATA_RECORD>
        <COMPONENT_SID>100</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>200</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>400</COMPONENT_SID>
        <GROUP_ID>1</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>10</COMPONENT_SID>
        <GROUP_ID>2</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>20</COMPONENT_SID>
        <GROUP_ID>2</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>2</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>4</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>8</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
    <DATA_RECORD>
        <COMPONENT_SID>16</COMPONENT_SID>
        <GROUP_ID>3</GROUP_ID>
    </DATA_RECORD>
</main>

produces the wanted, correct result:

<comp value="100,200,400|10,20|2,4,8,16"/>

Explanation:

  1. Muenchian method for grouping -- to find all distinct values of GROUP_ID .

  2. Simple logic to precede an item (or group) with a delimiter, whenever this item (or group) isn't the first.

II. XSLT 2.0 Solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:variable name="vResult">
   <xsl:for-each-group select="*/*" group-by="GROUP_ID">
    <xsl:if test="not(position()=1)">|</xsl:if>
    <xsl:for-each select="current-group()/COMPONENT_SID">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select="."/>
    </xsl:for-each>
   </xsl:for-each-group>
  </xsl:variable>

     <comp value="{$vResult}"/>
 </xsl:template>
</xsl:stylesheet>

when applied to the same XML document (above), again the wanted, correct result is produced:

<comp value="100,200,400|10,20|2,4,8,16"/>

Explanation:

  1. Use of <xsl:for-each-group>

  2. Use of current-group()

  3. The same simple logic for preceding each item (or group), which isn't the first, with the respective delimiter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文