maw2be
maw2be

Reputation: 91

namespace from xml in xsl template

I have problem with namespace in xml (it's xml from supplier) and how to work this out on xsl.

My xml:

<?xml version="1.0" encoding="utf-8"?>
  <string xmlns="https://www.website.com/getdata/service/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xmlresponse>
      <header>
        <reportinformation>
          <time>27/06/2017 19:42:07</time>
          <reporttype>getcompanyinformation</reporttype>
        </reportinformation>
      </header>
    </xmlresponse>
  </string>

And my xsl is

?xml version="1.0"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:my="https://www.website.com/getdat/service/"> 
<xsl:output indent="yes"  method="xhtml" omit-xml-declaration="yes"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/my:xmlresponse">
<html>
  <head>
    <title>www.website.com GetData XML Stylesheet</title>   
  </head>
  <body>
    <table>
      <tr>
        <td><span style="{$reporttitle}">GetData </span></td>
      </tr>

      <xsl:apply-templates select="my:reportinformation"/>

    </table>
  </body>
</html>
</xsl:template>

<xsl:template match="my:reportinformation">
<tr>
  <td align="left">
    <table style="{$datatable}">
      <tr>
        <td><span style="{$prompt}">Timestamp:</span></td>
        <td><span style="{$data}"><xsl:value-of select="my:time"/></span></td>
      </tr>
      <tr>
        <td><span style="{$prompt}">Report type:</span></td>
        <td><span style="{$data}"><xsl:value-of select="my:reporttype"/></span></td>
      </tr>
    </table>
  </td>
</tr>
</xsl:template>

Problem what I have is displaying information from template reportinformation but not matching/displaying information from main template.

I'm not good in xml, much more in frontend. Good friends what to fix to transforming correctly?

I search on SO and google but still or displaying main template or sub-template.

Upvotes: 0

Views: 108

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116957

You have numerous syntax mistakes which you should be able to detect by comparing your stylesheet with this one:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="https://www.website.com/getdata/service/"
exclude-result-prefixes="my">

<xsl:template match="/my:string">
    <html>
        <head>
            <title>www.website.com GetData XML Stylesheet</title>   
        </head>
        <body>
            <table>
                <tr>
                    <td>
                        <span style="{UNDEFINED_VARIABLE}">GetData </span>
                    </td>
                </tr>
                <xsl:apply-templates select="my:xmlresponse/my:header/my:reportinformation"/>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="my:reportinformation">
    <tr>
        <td align="left">
            <table style="{UNDEFINED_VARIABLE}">
                <tr>
                    <td>
                        <span style="{UNDEFINED_VARIABLE}">Timestamp:</span>
                    </td>
                    <td>
                        <span style="{UNDEFINED_VARIABLE}">
                            <xsl:value-of select="my:time"/>
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span style="{UNDEFINED_VARIABLE}">Report type:</span>
                    </td>
                    <td>
                        <span style="{UNDEFINED_VARIABLE}">
                            <xsl:value-of select="my:reporttype"/>
                        </span>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

kumesana
kumesana

Reputation: 2490

I can see you're correctly using a prefix, my:, to designate elements in your namespace in XPath and matching patterns. That's indeed what you need to do.

However you also need to link this prefix to your namespace, otherwise this prefix doesn't mean anything and is actually invalid to use.

All you need to do is replace in your XSLT,

xmlns="https://www.website.com/getdat/service/"

with

xmlns:my="https://www.website.com/getdat/service/"

Edit to add about the other problems:

You need to appropriately match and select the elements you want to act on.

Your first pattern should not target xmlresponse. It should target the root which can be simply reached with match="/". Using match="/my:xmlresponse" will only match if xmlresponse is the root element, which it is not.

When calling apply-templates, you should not specify a select. Applying templates will ultimately reach reportinformation as you wish to. However if you try to select it with select="my:reportinformation" that will only work if reportinformation is a child. Which it is not, it is a descendant. You'd need

select=".//my:reportinformation"

or simply not specify a select for the templates to apply to children and themselves pass the bucket down to reportinformation.

Upvotes: 1

Related Questions