-1

我正在尝试转换以下 XML:

<entities xmlns="http://ws.wso2.org/dataservice"><entityIds>
137651b03d18c0efee947f8bda341fb1
</entityIds>
<entityIds>
aa88ce76d454a0135c89bfbd4def62cd
</entityIds>
</entities>

使用以下 XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://ws.wso2.org/dataservice">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<urlDetails>
<customerId>

<xsl:value-of select="//p:entityList/p:entity[1]/p:customerId" />
</customerId>
<entityIds>
<xsl:apply-templates/>
<xsl:for-each select="//p:entityList/p:entity">

<xsl:value-of select="p:entityId" />,
</xsl:for-each>
</entityIds>
</urlDetails>
</xsl:template>
</xsl:stylesheet>

我得到如下输出:

<?xml version="1.0" encoding="utf-8"?>
<urlDetails xmlns:p="http://ws.wso2.org/dataservice">
<customerId/>
<entityIds>
137651b03d18c0efee947f8bda341fb1
aa88ce76d454a0135c89bfbd4def62cd
</entityIds>
</urlDetails> 

如何以逗号分隔输出,例如:

  <?xml version="1.0" encoding="utf-8"?>
    <urlDetails xmlns:p="http://ws.wso2.org/dataservice">
    <customerId/>
    <entityIds>
    137651b03d18c0efee947f8bda341fb1 ,
    aa88ce76d454a0135c89bfbd4def62cd
    </entityIds>
    </urlDetails> 

我使用了字符串 concat 并使用 version2.0 我使用了值分隔符,两者都不起作用。还有其他首选技术吗?

4

2 回答 2

1

你有没有试过这个:

<xsl:for-each select="//p:entityList/p:entity">
  <xsl:value-of select="p:entityId" /><xsl:text>,</xsl:text>
</xsl:for-each>
于 2013-10-01T09:48:05.357 回答
1

在 XSLT 2.0 中,您应该能够删除for-each(即将整个节点传递给value-of)并使用separator,例如

<xsl:value-of select="//p:entityList/p:entity/p:entityId" separator=","/>

对于 1.0 value-of,一次只处理一个节点,因此您需要 for-each:

<xsl:for-each select="//p:entityList/p:entity/p:entityId">
  <xsl:if test="position() &gt; 1">,</xsl:if>
  <xsl:value-of select="." />
</xsl:for-each>

确保您不会在列表中的第一if之前添加额外的逗号。

于 2013-10-01T10:56:02.227 回答