Matjaz
Matjaz

Reputation: 484

xsl template match exact node

I'm starter at xslt and I stuck with this:

abc.xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:procesResponse xmlns:ns2="http://schemas.com/2014/generic">
      <a>
        <b>
          <c>
            <d>test</d>
            <e>someValue</e>
          </c>
        </b>
        <b>
          <c>
            <d>test 2</d>
            <e>someValue</e>
          </c>
        </b>
        <b>
          <c>
            <d>test</d>
            <e>someValue</e>
          </c>
        </b>
      </a>
    </ns2:procesResponse>
  </soap:Body>
</soap:Envelope>

What I did so far:

doit.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <something>
      <stillSomething>
        <Author>administrator</Author>
        <Version>V1_4</Version>
        <Date>09012014</Date>
      </stillSomething>
      <values>
        <xsl:apply-templates select="Envelope/Body/procesResponse/a/b/c" />
      </values>
    </something>
  </xsl:template>
    <xsl:template match="Envelope/Body/procesResponse/a/b/c">
      <result>succeeded</result>
  </xsl:template>
</xsl:stylesheet>

result after xsltproc execution:

result.xml

<something>
  <stillSomething>
    <Author>administrator</Author>
    <Version>V1_4</Version>
    <Date>09012014</Date>
  </stillSomething>
  <values/>
</something>

But I would like to get this:

<something>
  <stillSomething>
    <Author>administrator</Author>
    <Version>V1_4</Version>
    <Date>09012014</Date>
  </stillSomething>
  <values>
    <result>succeeded</result>
    <result>succeeded</result>
    <result>succeeded</result>
  </values>
</something>

Succeeded should be there three times as three nodes are found.

I know that problem is in this two lines:

<xsl:apply-templates select="Envelope/Body/procesResponse/a/b/c" />

and

<xsl:template match="Envelope/Body/procesResponse/a/b/c">

Thank you for your help!

Upvotes: 0

Views: 716

Answers (1)

JLRishe
JLRishe

Reputation: 101778

The largest issue here is the failure to use namespaces properly. Once they are declared in the XSLT and used in the XPath, then this produces the expected result:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:ns2="http://schemas.com/2014/generic"
                exclude-result-prefixes="soap ns2">
  <xsl:template match="/">
    <something>
      <stillSomething>
        <Author>administrator</Author>
        <Version>V1_4</Version>
        <Date>09012014</Date>
      </stillSomething>
      <values>
        <xsl:apply-templates 
           select="soap:Envelope/soap:Body/ns2:procesResponse/a/b/c" />
      </values>
    </something>
  </xsl:template>
  <xsl:template match="c">
    <result>succeeded</result>
  </xsl:template>
</xsl:stylesheet>

Result:

<something>
  <stillSomething>
    <Author>administrator</Author>
    <Version>V1_4</Version>
    <Date>09012014</Date>
  </stillSomething>
  <values>
    <result>succeeded</result>
    <result>succeeded</result>
    <result>succeeded</result>
  </values>
</something>

Upvotes: 1

Related Questions