media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
174 stars 58 forks source link

Namespace for Apple TTML #164

Closed br0kenpixel closed 1 week ago

br0kenpixel commented 9 months ago

Hi, I'm trying to parse an Apple TTML document from Apple Music, however I can't figure out the right namespaces.

<tt xmlns="http://www.w3.org/ns/ttml" xmlns:itunes="http://music.apple.com/lyric-ttml-internal" itunes:timing="Line" xml:lang="en">
    <head>
        <metadata>
            <iTunesMetadata xmlns="http://music.apple.com/lyric-ttml-internal">
                <songwriters>
                    <songwriter>John Doe</songwriter>
                </songwriters>
            </iTunesMetadata>
        </metadata>
    </head>
   <!-- ... -->
</tt>

Can someone please explain which namespaces should I specify in #[yaserde(namespace = ...)]?

MarcAntoine-Arnaud commented 9 months ago

Hi @br0kenpixel

It's basic xml problem. xmlns="http://www.w3.org/ns/ttml" defines ttml as default namespace, so every tag without prefix uses that namespace. It means: tt, head, metadata comes from ttml namespace.

On iTunesMetadata tag, a new default namespace is declared. It means: iTunesMetadata, songwriters and songwriter are from http://music.apple.com/lyric-ttml-internal

To wrote that with YaSerde you have to define:


#[derive(YaDeserialize, Default, Debug, PartialEq)]
#[yaserde(
  prefix = "tt", 
  namespace = "tt: http://www.w3.org/ns/ttml"
)
struct TimedText {
  #[yaserde(prefix = "tt")]
  head: Head
}

#[derive(YaDeserialize, Default, Debug, PartialEq)]
#[yaserde(
  prefix = "tt", 
  namespace = "tt: http://www.w3.org/ns/ttml"
)
struct Head {
  #[yaserde(prefix = "tt")]
  metadata: Metadata
}

...

#[derive(YaDeserialize, Default, Debug, PartialEq)]
#[yaserde(
  prefix = "itunes", 
  namespace = "itunes: http://music.apple.com/lyric-ttml-internal"
)
struct ITunesMetadata {
  #[yaserde(prefix = "itunes")]
  songwriters: Vec<SongWriter>,
}

...