SOAP Response converting to CSV

2019-08-29 08:57发布

        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Body>
          <ns0:DataResponse xmlns:ns0="http://somenamspace/v1.0">
             <ns0:ResponseId>
                <ns0:RequestID>12345</ns0:RequestID>
             </ns0:ResponseId>
             <ns0:Payload>
                <ns1:Product xmlns:ns1="http://anothernamespace/v1.x">
                   <ns1:ProductName>productName</ns1:ProductName>
                   <ns1:ProductIdentifier>12222</ns1:ProductIdentifier>
                   <ns1:ProdInst>
                      <ns1:Type>Conv</ns1:Type>
                      <ns1:Descr>Conventional Loan</ns1:Descr>
                      <ns1:AllowedTypes>
                         <ns1:ScheduleSchedule>true</ns1:ScheduleSchedule>
                      </ns1:AllowedTypes>
                      <ns1:prdExist>false</ns1:prdExist>
                      <ns1:AdditionalAttributes>
                         <ns1:AdditionalAttribute name="gura" value="C"/>
                      </ns1:AdditionalAttributes>
                   </ns1:ProdInst>
                   <ns1:ProductGroups>
                      <ns1:ProductGroupName>1111</ns1:ProductGroupName>
                      <ns1:ProductGroupName>2222</ns1:ProductGroupName>
                   </ns1:ProductGroups>
                </ns1:Product>
                 <ns1:Product>
                   .......
                 </ns1:Product>
             </ns0:Payload>
          </ns0:DataResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
------------------------------------
<xsl:stylesheet version="1.0"

        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:ns0="http://somenamespace/v1.0"
        xmlns:ns1="http://anothernamespace/v1.x"
        exclude-result-prefixes="ns1">
        <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>

        <xsl:template match="ns1:Products">
            <xsl:value-of select="."/>
        </xsl:template>

    </xsl:stylesheet>
----------------------------------------------------
  <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://namespace/v1.x"
    exclude-result-prefixes="ns1">
    <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
    <xsl:template match="/">
            <xsl:for-each select="ns1:Product">
                    <xsl:value-of select="ns1:ProductName" />
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="ns1:ProdInst/ns1:Type" />
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="ns1:ProdInst/ns1:Descr" />
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="ns1:ProdInst/ns1:AdditionalAttributes/@gura" />
                    <xsl:text>,</xsl:text>
                    <xsl:for-each select="ns1:ProductGroups">
                      <xsl:value-of select="."/>
                    </xsl:for-each>,                    
            </xsl:for-each>
    </xsl:template>

Basically I am trying to write the XSL to convert all these "Product" inner attribute values to the CSV format. I already struggled with overcome using the namespaces but still could not write up in the perfect format, sometimes the styles going on next line , and if I tried the generic one then the problem as could not read the additionalAtrributes values. any help on this would be great.

Expecting output as all the tag inner attributes lined up in the CSV format with "," in order .

 <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:ns0="http://somenamspace/v1.0"
        xmlns:ns1="http://anothernamespace/v1.x"
        exclude-result-prefixes="ns1">


      <xsl:output  method="text" indent="no"/>
      <xsl:strip-space elements="*"/>

      <xsl:template match="/">
          <xsl:apply-templates select="ns1:Product"/>
      </xsl:template> 

      <xsl:template match="ns1:Product">
        <xsl:value-of select="*"/>
        <xsl:apply-templates select="ns1:ProdInst"/>
        <xsl:apply-templates select="ns1:ProductGroups"/>
      </xsl:template>

      <xsl:template match="ns1:ProdInst">
        <xsl:value-of select="."/>
        <xsl:apply-templates select="ns1:AllowedTypes"/>
        <xsl:apply-templates select="ns1:AdditionalAttributes"/>
      </xsl:template>

      <xsl:template match="ns1:AllowedTypes">
        <xsl:value-of select="."/>
      </xsl:template>

      <xsl:template match="ns1:AdditionalAttributes">
        <xsl:for-each select="@*">
           <xsl:copy-of select="." />
       </xsl:for-each>
      </xsl:template>


</xsl:stylesheet>

I have tried as above and got atleast all in the same line, but still its not comma seperated also the "AdditionalAttributes" are not included. can anybody help on this?

标签: xml xslt soap csv
1条回答
迷人小祖宗
2楼-- · 2019-08-29 09:47

Please have a look this solution:

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns0="http://somenamspace/v1.0" xmlns:ns1="http://anothernamespace/v1.x"
  exclude-result-prefixes="ns1">


  <xsl:output method="text" indent="no"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:for-each select="//ns1:Product">
      <xsl:call-template name="getProduct">
        <xsl:with-param name="Product" select="self::*"/>
      </xsl:call-template>
      <xsl:text>

      </xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="getProduct">
    <xsl:param name="Product"/>
    <xsl:variable name="ProductContent">
      <xsl:for-each select="$Product/*">
        <xsl:choose>
          <xsl:when test="*">
            <xsl:call-template name="getChild">
              <xsl:with-param name="Child" select="*"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="@*">
              <xsl:for-each select="@*">
                <xsl:value-of select="."/>
                <xsl:text>,</xsl:text>
              </xsl:for-each>
              <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:value-of select="."/>
            <xsl:text>,</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="substring($ProductContent,0,string-length($ProductContent))"/>
  </xsl:template>

  <xsl:template name="getChild">
    <xsl:param name="Child"/>
    <xsl:for-each select="$Child/node()">
      <xsl:choose>
        <xsl:when test="* and not(@*)">
          <xsl:call-template name="getChild">
            <xsl:with-param name="Child" select="*"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="text()">
          <xsl:value-of select="."/>
          <xsl:text>,</xsl:text>
        </xsl:when>
        <xsl:when test="*">
          <xsl:for-each select="@*">
            <xsl:value-of select="."/>
            <xsl:text>,</xsl:text>
          </xsl:for-each>
          <xsl:call-template name="getChild">
            <xsl:with-param name="Child" select="*"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="not(*) and @*">
          <xsl:for-each select="@*">
            <xsl:value-of select="."/>
            <xsl:text>,</xsl:text>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="@*">
            <xsl:for-each select="@*">
              <xsl:value-of select="."/>
              <xsl:text>,</xsl:text>
            </xsl:for-each>
            <xsl:text>,</xsl:text>
          </xsl:if>
          <xsl:value-of select="."/>
          <xsl:text>,</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:if test="$Child/@* and not($Child/node())">
      <xsl:for-each select="$Child/@*">
        <xsl:value-of select="."/>
        <xsl:text>,</xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

OUTPUT:

productName,12222,Conv,Conventional Loan,true,false,gura,C,1111,2222

      produ222222222ctName,12222,Conv,Conventional Loan,true,false,gura,C,1111,2222
查看更多
登录 后发表回答