user8683439
user8683439

Reputation: 49

XSL transformation failing with multiple templates

We are facing problem with XSL transformation of this XML

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <documentListResponse xmlns="http://ws.ouaf.oracle.com/">
      <documentListResult>
        <docdata>
          <account>106743720016</account>
          <date>2017/07/18</date>
          <format>afp</format>
          <file>20170719041902</file>
          <pointer>00064CE900012D14</pointer>
          <pages>4</pages>
        </docdata>
        <docdata>
          <account>104243722316</account>
          <date>2017/07/28</date>
          <format>afp</format>
          <file>20170712331331902</file>
          <pointer>00064CE900012D14</pointer>
          <pages>4</pages>
        </docdata>
        <moredata>0</moredata>
      </documentListResult>
    </documentListResponse>
  </S:Body>
</S:Envelope>

to this XML

<responseMessage>
  <documentList>
    <filename>20170719041902</filename>
    <format>afp</format>
    <filePointer>00064CE900012D14</filePointer>
    <totalPages>4</totalPages>
  </documentList>
  <documentList>
    <filename>20170712331331902</filename>
    <format>afp</format>
    <filePointer>00064CE900012D14</filePointer>
    <totalPages>4</totalPages>
  </documentList>
</responseMessage>

using XSLT as follows (trying to select multiple nodes having same node name deeply embedded inside an XML tree).

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:res="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" 
    exclude-result-prefixes="S">
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="documentListResult">
    <responseMessage>
      <documentList>
        <xsl:for-each select="docdata">
          <filename>
            <xsl:value-of select="file"/>
          </filename>

          <format>
            <xsl:value-of select="format"/>
          </format>
          <filePointer>
            <xsl:value-of select="pointer"/>
          </filePointer>
          <totalPages>
            <xsl:value-of select="pages"/>
          </totalPages>
        </xsl:for-each>
      </documentList>
    </responseMessage>
  </xsl:template>

Can anybody please guide me here? All the transformation functions that I am using seem to work against me. Every time I change the select clause in template, it gives me inconsistent results. I am not sure whether I am going in the right direction.

Upvotes: 2

Views: 65

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52848

The documentListResponse, and all of its descendants, are in the default namespace http://ws.ouaf.oracle.com. You need to bind that namespace to a prefix and use it in your XPaths.

You should also move the <documentList> inside the xsl:for-each.

Also note that since you don't use the res or S prefixes, you can remove those bindings.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:o="http://ws.ouaf.oracle.com/"
  exclude-result-prefixes="o">
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="o:documentListResult">
    <responseMessage>
      <xsl:for-each select="o:docdata">
        <documentList>
          <filename>
            <xsl:value-of select="o:file"/>
          </filename>
          <format>
            <xsl:value-of select="o:format"/>
          </format>
          <filePointer>
            <xsl:value-of select="o:pointer"/>
          </filePointer>
          <totalPages>
            <xsl:value-of select="o:pages"/>
          </totalPages>
        </documentList>
      </xsl:for-each>
    </responseMessage>
  </xsl:template>
</xsl:stylesheet>

Working example: http://xsltransform.net/3MvmrAC/1

Upvotes: 2

Related Questions