首页 > 解决方案 > 如何在未绑定的 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 中填充这个输出或使用任何模板?

标签: xsltxslt-1.0xslt-2.0transformationbpel

解决方案


这是一种在 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


推荐阅读