如何解决如何在未绑定的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:create-nodeset-from-delimited-string函数,但是它给了我一个错误。有什么方法可以在XSLT或任何模板中填充此输出?
解决方法
这是使用XSLT 1.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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。