0

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.

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

@maw2be Wrong file. I said in XSLT. Besides, can you actually modify the XML your provider is giving you?
sorry, your'e right I edit wrong file. should edit xslt
Okay, I guess namespaces are not your only problem. I edited my response to address the other issues.
@maw2be - Also note that the namespace uri in the XML is https://www.website.com/getdata/service/, but you're using https://www.website.com/getdat/service/ (missing an "a" in "getdata").
I give up. If I only could remove this namespace from xml or ignore it be great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.