如何连接 xml 的多个属性值?
How concat many attribute values for xml?
SOAP 消息:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<field_21>
<row StartDate="2017-01-01" EndDate="2017-01-07" DaysCount="7" Diagnoz="A00.0"/>
<row StartDate="2019-02-01" EndDate="2019-02-07" DaysCount="8" Diagnoz="A10.0"/>
</field_21>
</soapenv:Body>
</soapenv:Envelope>
我的 xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:template match="/">
<result>
<xsl:value-of select="string-join(
concat(//field_21/row/@StartDate, ' ',
//field_21/row/@EndDate, ' ',
//field_21/row/@DaysCount, ' ',
//field_21/row/@Diagnoz), ';')"/>
</result>
</xsl:template>
</xsl:stylesheet>
结果:
<result xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">2017-01-01 2017-01-07 7 A00.0</result>
我需要的结果:
<result xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">2017-01-01 2017-01-07 7 A00.0; 2019-02-01 2019-02-07 8 A10.0</result>
告诉我如何更正我的转换以获得所需的结果
这有点令人困惑,因为您的样式表中包含 version="1.0"
,但 string-join
仅在 XSLT 2.0 中可用。但是,如果您使用的是 XSLT 2.0,我预计 concat
会失败,因为 //field_21/row/@StartDate
returns 多个节点,这在 XSLT 2.0
中是不允许的
但是,如果您确实在使用 XSLT 2.0,您可以这样写表达式:
<xsl:value-of select="string-join(//field_21/row/concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz), ';')"/>
或者像这样利用 XSLT 2.0 中可用的 separator
属性。
<xsl:value-of select="//field_21/row/concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz)" separator="; " />
但是,如果您只能使用 XSLT 1.0,则必须使用 xsl:for-each
(或 xsl:apply-templates
<xsl:for-each select="//field_21/row">
<xsl:if test="position() > 1">; </xsl:if>
<xsl:value-of select="concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz)" />
</xsl:for-each>
SOAP 消息:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<field_21>
<row StartDate="2017-01-01" EndDate="2017-01-07" DaysCount="7" Diagnoz="A00.0"/>
<row StartDate="2019-02-01" EndDate="2019-02-07" DaysCount="8" Diagnoz="A10.0"/>
</field_21>
</soapenv:Body>
</soapenv:Envelope>
我的 xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:template match="/">
<result>
<xsl:value-of select="string-join(
concat(//field_21/row/@StartDate, ' ',
//field_21/row/@EndDate, ' ',
//field_21/row/@DaysCount, ' ',
//field_21/row/@Diagnoz), ';')"/>
</result>
</xsl:template>
</xsl:stylesheet>
结果:
<result xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">2017-01-01 2017-01-07 7 A00.0</result>
我需要的结果:
<result xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">2017-01-01 2017-01-07 7 A00.0; 2019-02-01 2019-02-07 8 A10.0</result>
告诉我如何更正我的转换以获得所需的结果
这有点令人困惑,因为您的样式表中包含 version="1.0"
,但 string-join
仅在 XSLT 2.0 中可用。但是,如果您使用的是 XSLT 2.0,我预计 concat
会失败,因为 //field_21/row/@StartDate
returns 多个节点,这在 XSLT 2.0
但是,如果您确实在使用 XSLT 2.0,您可以这样写表达式:
<xsl:value-of select="string-join(//field_21/row/concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz), ';')"/>
或者像这样利用 XSLT 2.0 中可用的 separator
属性。
<xsl:value-of select="//field_21/row/concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz)" separator="; " />
但是,如果您只能使用 XSLT 1.0,则必须使用 xsl:for-each
(或 xsl:apply-templates
<xsl:for-each select="//field_21/row">
<xsl:if test="position() > 1">; </xsl:if>
<xsl:value-of select="concat(@StartDate, ' ', @EndDate, ' ', @DaysCount, ' ', @Diagnoz)" />
</xsl:for-each>