如何在未绑定的xml结构中生成逗号分隔的字符串



我的输入如下

<ns0:input>AZX1,P81,IKJU,RED</ns0:input>

我已经用未绑定的元素创建了目标xsd来存储值

<element name="Response">
<complexType>
<sequence>
<element name="parameter" minOccurs="1" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="value" type="string"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>

所以我想要以下格式的输出。

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:Response  xmlns:ns0="http://xmlns.oracle.com/CDM/Append/AppendBPELProcess">
<ns0:parameter>
<ns0:value>AZX1</ns0:value>
</ns0:parameter>
<ns0:parameter>
<ns0:value>P81</ns0:value>
</ns0:parameter>
<ns0:parameter>
<ns0:value>IKJU</ns0:value>
</ns0:parameter>
<ns0:parameter>
<ns0:value>RED</ns0:value>
</ns0:parameter>
</ns0:Response>

我尝试过在XSLT中使用oraext:从分隔字符串函数创建节点集,但它给了我一个错误。有没有任何方法可以用XSLT或使用任何模板填充此输出?

下面是一种在XSLT1.0中使用递归模板的方法。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xmlns.oracle.com/CDM/Append/AppendBPELProcess"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<ns0:Response>
<xsl:apply-templates select="//ns0:input"/>
</ns0:Response>
</xsl:template>

<xsl:template match="ns0:input">
<xsl:call-template name="inputs">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</xsl:template>

<!-- Recursive template will call itself for all comma separated elements -->
<xsl:template name="inputs">
<xsl:param name="input"/>
<xsl:choose>
<xsl:when test="contains($input,',')">
<ns0:parameter>
<ns0:value><xsl:value-of select="substring-before($input,',')"/></ns0:value>
</ns0:parameter>
<xsl:call-template name="inputs">
<xsl:with-param name="input" select="substring-after($input,',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<ns0:parameter>
<ns0:value><xsl:value-of select="$input"/></ns0:value>
</ns0:parameter>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

请在此处查看它的工作情况:https://xsltfiddle.liberty-development.net/a9HjZW/1

更新:排除第一个元素

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xmlns.oracle.com/CDM/Append/AppendBPELProcess"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<ns0:Response>
<xsl:apply-templates select="//ns0:input"/>
</ns0:Response>
</xsl:template>

<xsl:template match="ns0:input">
<xsl:call-template name="inputs">
<xsl:with-param name="input" select="."/>
<xsl:with-param name="pos" select="1"/>
</xsl:call-template>
</xsl:template>

<!-- Recursive template will call itself for all comma separated elements -->
<xsl:template name="inputs">
<xsl:param name="input"/>
<xsl:param name="pos"/>
<xsl:choose>
<xsl:when test="contains($input,',')">
<xsl:if test="$pos>1">
<ns0:parameter>
<ns0:value><xsl:value-of select="substring-before($input,',')"/></ns0:value>
</ns0:parameter>
</xsl:if>
<xsl:call-template name="inputs">
<xsl:with-param name="input" select="substring-after($input,',')"/>
<xsl:with-param name="pos" select="$pos+1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$pos>1">
<ns0:parameter>
<ns0:value><xsl:value-of select="$input"/></ns0:value>
</ns0:parameter>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

请在此处查看它的工作情况:https://xsltfiddle.liberty-development.net/a9HjZW/3

最新更新