Xpath 查询以提供逗号分隔的输出 [英] Xpath Query to provide a comma-delimited output

查看:27
本文介绍了Xpath 查询以提供逗号分隔的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope> 
   <Body> 
      <RetrieveChangeKeysListResponse message="Success"> 
         <keys> 
            <ChangeID type="String">C10286</ChangeID> 
         </keys> 
         <keys> 
            <ChangeID type="String">C10296</ChangeID>
         </keys> 
      </RetrieveChangeKeysListResponse> 
   </Body> 
</Envelope>

从上面的 xml 文件中,下面 xpath 查询的输出给出了没有分隔符的值.

From the above xml file, output for the below xpath query is giving the values without delimiter.

查询:

/Envelope/Body/RetrieveChangeKeysListResponse/keys/ChangeID/text()

输出: C10286C10296

如何在值之间使用分隔符获取输出.

How can I get the output with a delimiter between the values.

示例:(C10286,C10296)

需要你的帮助.

感谢和问候

里亚斯侯赛因 A

推荐答案

看起来你有两个 ChangeID 节点,如果你调用 XPATH 作为

It looks like you have two ChangeID nodes and if you call XPATH as

/Envelope/Body/RetrieveChangeKeysListResponse/keys/ChangeID/text()

然后它将通过连接在一起输出所有值.

then it will output all the values by concatenating together.

所以将 XPATH 称为

/Envelope/Body/RetrieveChangeKeysListResponse/keys/ChangeID

然后是 concat() 你喜欢的 text() 值.

and then concat() the value that is text() as you like.

使用 XSLT 1.0,你可以这样做:-

Using XSLT 1.0, you can do this way:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates select="/Envelope/Body/RetrieveChangeKeysListResponse/keys/ChangeID"/>
    </xsl:template>
    <xsl:template match="ChangeID">
        <xsl:if test="position()='1'">
            <xsl:value-of select="'('"/>
        </xsl:if>
        <xsl:choose>
            <xsl:when test="position()=last()">
                <xsl:value-of select="text()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(text(),',')"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="position()=last()">
            <xsl:value-of select="')'"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出:

(C10286,C10296)

Output:

(C10286,C10296)

这篇关于Xpath 查询以提供逗号分隔的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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