xslt - 如何在未绑定的 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
推荐阅读
- php - Lumen/Laravel:嵌套关系求和
- ios - 在 Mapbox xamarin 表单 IOS 信息窗口符号的文本上添加填充
- shell - Shell 脚本可以逐行读取文件,但不能对每一行执行操作
- node.js - 电话号码唯一验证(无法将响应绑定到 HTML)
- reactjs - 如何使用来自firebase的数据结果发布到数组中
- rdf - rdfs:Literal "x" 是 rdfs:Resource of rdf:value "x" 吗?
- python - 使用 Biopython 后解析 BLAST 结果的 XML 输出
- woocommerce - 如何在woocomerce中仅为延期交货产品(简单和变化)启用存款
- powershell - Powershell 脚本在命令提示符下运行
- python - 来自 trapz 或 simpson 积分的数组输出