0

If I remove all namespaces and prefixes, my XSLT transformations work perfectly.

I've had a few attempts at introducing the namespaces but always end up with some sort of error or no output, so here I am looking for a bit of help and understanding.

My expected output, whittled down to namespaces and prefixes, is:

<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0" creator="GSAK"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd http://www.gsak.net/xmlv1/6 http://www.gsak.net/xmlv1/6/gsak.xsd"
xmlns="http://www.topografix.com/GPX/1/0">
 <desc>...</desc>
 <author>...</author>
 <wpt lat="..." lon="...">
  <name>...</name>
  ...
  <gsak:wptExtension xmlns:gsak="http://www.gsak.net/xmlv1/6">
    <gsak:DNF>...</gsak:DNF>
    ...
  </gsak:wptExtension>
  <groundspeak:cache id="..." available="..." archived="..." xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/1">
    <groundspeak:name>...</groundspeak:name>
  </groundspeak:cache>
 </wpt>
 <wpt lat="..." lon="...">
   ...
 </wpt>
</gpx>

The XML input, again whittled down, is:

<AdventureModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/GamePlayer.Business.WebAPI.Contracts.Models">

  <Message i:nil="..."/>
  <DeepLink>..</DeepLink>
  <Description>...</Description>
  ...
  <GeocacheSummaries>
    <GeocacheSummaryModel>
      <Mode i:nil="..."/>
      <Id>...</Id>
      <Location>
        <Altitude i:nil="..."/>
        <Latitude>...</Latitude>
        <Longitude>...</Longitude>
      </Location>
      ...
    </GeocacheSummaryModel>
    <GeocacheSummaryModel>
      ...
    </GeocacheSummaryModel>
  </GeocacheSummaries>
  ...
</AdventureModel>

What do I need to add to the XSLT header to achieve correct namespace transformation?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ...
</xsl:stylesheet>

1 Answers1

1

The XSLT version is not specified. XSLT 2.0/3.0 makes namespace handling much easier.

So here is how it works for XSLT 1.0

The input XML has a default namespace:

xmlns="http://schemas.datacontract.org/2004/07/GamePlayer.Business.WebAPI.Contracts.Models"

It means the following:

  • Every single element in the input XML is bound to it.
  • XSLT needs to designate that default namespace with a prefix. Let's say 'a'.

So the XSLT would be like follows:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://schemas.datacontract.org/2004/07/GamePlayer.Business.WebAPI.Contracts.Models">

    <xsl:template match="/">
        <xsl:value-of select="a:AdventureModel/a:DeepLink"/>
    </xsl:template>

</xsl:stylesheet>
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • That is a great help and I am now outputting the nodes I need. Thank you. Just the issue of trying to create those namespaces that need to be added to the output XML. Can you give me some guidance with this? – Billy Wallace Dec 06 '20 at 17:06