0

I'm trying to get some information from XML with a XSLT. I'm facing an XML (from MediaInfo tool https://mediaarea.net/fr/MediaInfo) with various namespaces. I tried many solutions found here (and there is lot of posts about it), but I don't find the correct way to do it... I thik my problem is due to namespace. My XSL namespace is probably too much regarding what I want.

In my example below, I simplify XML, and I just try to get value of "Title"

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<MediaInfo
    xmlns="https://mediaarea.net/mediainfo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://mediaarea.net/mediainfo https://mediaarea.net/mediainfo/mediainfo_2_0.xsd"
    version="2.0">
    <creatingLibrary version="19.09" url="https://mediaarea.net/MediaInfo">MediaInfoLib</creatingLibrary>
    <media ref="F:\media\audio\Madonna - Die Another Day.aif">
        <track type="General">
            <AudioCount>1</AudioCount>
            <FileExtension>aif</FileExtension>
            <Title>Die Another Day</Title>
            <Album>American Life</Album>
            <Track>1</Track>
            <Performer>Madonna</Performer>
            <Composer>Mirwais Ahmadzaï - Madonna</Composer>
            <Genre>Pop</Genre>
            <Comment>Die Another Day (soundtrack)</Comment>
        </track>
        <track type="Audio">
            <Format>PCM</Format>
            <Format_Settings_Endianness>Big</Format_Settings_Endianness>
            <BitRate_Mode>CBR</BitRate_Mode>
            <Channels>2</Channels>
            <SamplingRate>48000</SamplingRate>
            <BitDepth>24</BitDepth>
        </track>
    </media>
</MediaInfo>

and my XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="https://mediaarea.net/mediainfo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://mediaarea.net/mediainfo https://mediaarea.net/mediainfo/mediainfo_2_0.xsd">
<xsl:output method="xml" encoding="UTF-8" indent="yes" version="1.0"/>
<xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:variable name="Title">
            <xsl:value-of select="MediaInfo/media/track[@type='General']/Title"/>
        </xsl:variable> 
        <xsl:element name="AUDIO" >
            <xsl:element name="title">
                <xsl:value-of select="$Title"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

my output is:

<AUDIO xmlns="https://mediaarea.net/mediainfo">
   <title/>
</AUDIO>

and I'm looking for:

<AUDIO xmlns="https://mediaarea.net/mediainfo">
   <title>Die Another Day</title>
</AUDIO>
  • There are 732 posts found by a search for "XSLT default namespace": I've chosen one of these arbitrarily to link as a duplicate, but it's worth looking at a selection of the others. – Michael Kay Jul 29 '20 at 11:32

1 Answers1

0

Since https://mediaarea.net/mediainfo is a default namespace, you should bind it to a prefix and use that in your XPaths.

Example (I use mi as the prefix.)...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:mi="https://mediaarea.net/mediainfo"
  exclude-result-prefixes="mi">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:element name="AUDIO" namespace="https://mediaarea.net/mediainfo">      
      <xsl:element name="title" namespace="https://mediaarea.net/mediainfo">
        <xsl:value-of select="mi:MediaInfo/mi:media/mi:track[@type='General']/mi:Title"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>

or maybe this...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="https://mediaarea.net/mediainfo"
  xmlns:mi="https://mediaarea.net/mediainfo"
  exclude-result-prefixes="mi">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <AUDIO>      
      <TITLE>
        <xsl:value-of select="mi:MediaInfo/mi:media/mi:track[@type='General']/mi:Title"/>
      </TITLE>
    </AUDIO>
  </xsl:template>
  
</xsl:stylesheet>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95