如何使用 xsl 连接具有两个不同分隔符的字符串
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XSLT 2.0 解决方案可以进一步缩短为:
The XSLT 2.0 solution can be shortened further to:
我。 XSLT 1.0 解决方案:
应用于提供的 XML 文档时:
产生所需的正确结果:
说明:
< strong>Muenchian 分组方法 -- 查找
GROUP_ID
的所有不同值。只要该项目(或组)不是第一个,就在项目(或组)之前添加分隔符的简单逻辑。
二. XSLT 2.0 解决方案:
当应用于同一个 XML 文档(上面)时,再次生成所需的正确结果:
说明:
使用 < code>
使用
current-group()
在每个项目(或组)之前使用相同的简单逻辑,但不是第一个,带有相应的分隔符。
I. XSLT 1.0 solution:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
Muenchian method for grouping -- to find all distinct values of
GROUP_ID
.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:
when applied to the same XML document (above), again the wanted, correct result is produced:
Explanation:
Use of
<xsl:for-each-group>
Use of
current-group()
The same simple logic for preceding each item (or group), which isn't the first, with the respective delimiter.