2013-09-25 61 views
1

我想從MediaInfo的XML生成的duration標記中輸出一致的hh:mm:ss標記。但我不確定如何創建一個xsl:模板,它將按照我想要的方式來設置持續時間的格式。的MediaInfo輸出爲我的音頻文件的持續時間在兩個方面,例如:使用MediaInfo XML輸出中的xslt創建hhmmss模板

<MediaInfoDuration>1h 58mn</MediaInfoDuration> 

我想它翻譯成

<Duration>01:58:00</Duration> 

<MediaInfoDuration>56mn 48s</MediaInfoDuration> 

我」 d想翻譯成

<Duration>00:56:48</Duration> 

我真正知道如何做的唯一的事情就是使用子字符串,但由於「mn」可以出現在不同的位置,而「s」有時根本不會,所以我不知道該怎麼做。我想我需要僱員變量,也許xsl:選擇,但我不確定。

回答

0

這是一種方法。舉例來說,假設包含'h'的元素永遠不會包含's'。

這XSLT樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/> 

    <!-- The identity transform. --> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Match MediaInfoDuration elements containing 'h'. --> 
    <xsl:template match="MediaInfoDuration[contains(., 'h')]"> 
    <Duration> 
     <xsl:call-template name="hh-mm-ss"> 
     <xsl:with-param name="hours" select="substring-before(., 'h')" /> 
     <xsl:with-param name="minutes" select="substring-before(substring-after(., 'h '), 'mn')"/> 
     </xsl:call-template> 
    </Duration> 
    </xsl:template> 

    <!-- Match the other kind of MediaInfoDuration element. --> 
    <xsl:template match="MediaInfoDuration"> 
    <Duration> 
     <xsl:call-template name="hh-mm-ss"> 
     <xsl:with-param name="minutes" select="substring-before(., 'mn')" /> 
     <xsl:with-param name="seconds" select="substring-before(substring-after(., 'mn '), 's')"/> 
     </xsl:call-template> 
    </Duration> 
    </xsl:template> 

    <!-- Formatting the output. --> 
    <xsl:template name="hh-mm-ss"> 
    <xsl:param name="hours" select=" '0' "/> 
    <xsl:param name="minutes" select=" '0' " /> 
    <xsl:param name="seconds" select=" '0' "/> 

    <xsl:value-of select="format-number($hours, '00')"/> 
    <xsl:text>:</xsl:text> 
    <xsl:value-of select="format-number($minutes, '00')"/> 
    <xsl:text>:</xsl:text> 
    <xsl:value-of select="format-number($seconds, '00')"/> 
    </xsl:template> 

</xsl:stylesheet> 

時,該輸入用於XML:

<root> 
    <MediaInfoDuration>56mn 48s</MediaInfoDuration> 
    <MediaInfoDuration>1h 58mn</MediaInfoDuration> 
</root> 

產生這樣的輸出:

<root> 
    <Duration>00:56:48</Duration> 
    <Duration>01:58:00</Duration> 
</root> 
0

這個效果很好,雖然我不知道如何使其適合我現有的XSL。我想我簡化了它,現在我不確定在哪裏粘貼它。這是我正在改變的文件。

<File> 
<track type="General"> 
<Complete_name>F:\NYAC\WNYC-NYAC-2002-06-06-24719-B.wav</Complete_name> 
<Format>Wave</Format> 
<File_size>573 MiB</File_size> 
<MediaInfoDuration>56mn 48s</MediaInfoDuration> 
<Overall_bit_rate_mode>Constant</Overall_bit_rate_mode> 
<Overall_bit_rate>1 411 Kbps</Overall_bit_rate> 
<Album>Album 020D5001</Album> 
<Part_Position>0</Part_Position> 
<Part_Total>0</Part_Total> 
<Genre>Unknown/Unknown</Genre> 
<Released_date>0000</Released_date> 
<Recorded_date>0000/0000</Recorded_date> 
<Original_source_form_Name>Album 020D5001</Original_source_form_Name> 
<MCDI>Yes</MCDI> 
</track> 

<track type="Audio"> 
<Format>PCM</Format> 
<Format_settings__Endianness>Little</Format_settings__Endianness> 
<Format_settings__Sign>Signed</Format_settings__Sign> 
<Codec_ID>1</Codec_ID> 
<Duration>56mn 48s</Duration> 
<Bit_rate_mode>Constant</Bit_rate_mode> 
<Bit_rate>1 411.2 Kbps</Bit_rate> 
<Channel_s_>2 channels</Channel_s_> 
<Sampling_rate>44.1 KHz</Sampling_rate> 
<Bit_depth>16 bits</Bit_depth> 
<Stream_size>573 MiB (100%)</Stream_size> 
</track> 

這裏是我當前的XSL

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mt="http://www.iana.org/assignments/media-types/" xmlns:la="http://www.loc.gov/standards/iso639-2/" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str" xmlns="http://www.pbcore.org/PBCore/PBCoreNamespace.html" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/> 

<xsl:template match="Mediainfo"> 
    <pbcoreCollection> 
    </pbcoreCollection> 
</xsl:template> 

<xsl:template match="File"> 
     <pbcoreCollection> 
      <pbcoreDescriptionDocument> 
     <xsl:variable name="mediainfoSource">MediaInfoLib</xsl:variable> 
     <xsl:variable name="mediainfoVersion"><xsl:value-of select="../@version"/></xsl:variable> 
     <xsl:for-each select="track[@type='General']"> 
      <!-- pbcoreTitle/pbcoreTitleType --> 
      <pbcoreTitle titleType="Collection">WNYC</pbcoreTitle> 
      <!-- pbcoreIdentifier/pbcoreIdentifierSource --> 
      <xsl:if test="Complete_name"> 
       <pbcoreIdentifier> 
        <xsl:attribute name="source">WNYC Archive Catalog</xsl:attribute> 
        <xsl:value-of select="substring(Complete_name,30,5)"/> 
       </pbcoreIdentifier> 
      </xsl:if> 

      <!-- instantiationIdentifier/instantiationIdentifierSource --> 
      <pbcoreInstantiation> 
      <xsl:if test="Complete_name"> 
       <instantiationIdentifier> 
        <xsl:attribute name="source">David Title</xsl:attribute> 
        <xsl:value-of select="substring(Complete_name,9,26)"/> 
       </instantiationIdentifier> 
      </xsl:if> 

      <!-- dates --> 
      <xsl:if test="Complete_name"> 
       <instantiationDate> 
        <xsl:attribute name="dateType">Broadcast</xsl:attribute> 
        <xsl:value-of select="substring(Complete_name,19,10)"/> 
       </instantiationDate> 
      </xsl:if> 

       <instantiationDate> 
        <xsl:attribute name="dateType">Created</xsl:attribute> 
        <xsl:text>2013-09-14</xsl:text> 
       </instantiationDate> 


      <!-- formatDigital --> 
      <instantiationDigital><xsl:text>BWF</xsl:text></instantiationDigital> 

      <!-- formatLocation --> 
      <instantiationLocation>DAVID</instantiationLocation> 

      <!-- formatMediaType --> 
      <instantiationMediaType>Sound</instantiationMediaType> 

      <!-- formatGenerations --> 
      <instantiationGenerations>Master: preservation</instantiationGenerations> 

      <!-- formatFileSize --> 
      <xsl:if test="File_size"><!-- this expresses filesize as an integer count of bytes --> 
       <instantiationFileSize> 
        <xsl:attribute name="unitsOfMeasure">bytes</xsl:attribute> 
        <xsl:value-of select="normalize-space(File_size)"/> 
       </instantiationFileSize> 
      </xsl:if> 

      <!-- formatChannelConfiguration --> 
      <instantiationChannelConfiguration><xsl:text>Stereo</xsl:text></instantiationChannelConfiguration> 

      <!-- language --> 
       <instantiationLanguage><xsl:text>eng</xsl:text></instantiationLanguage>   
      <instantiationEssenceTrack> 
       <essenceTrackType>audio</essenceTrackType> 
       <essenceTrackIdentifier> 
        <xsl:attribute name="source">David Title</xsl:attribute> 
        <xsl:value-of select="substring(Complete_name,9,26)"/> 
       </essenceTrackIdentifier> 
       <essenceTrackStandard>Linear PCM</essenceTrackStandard> 
       <essenceTrackEncoding>Captured from CD via MF Digital Ripstation</essenceTrackEncoding> 
       <essenceTrackSamplingRate>44.1 kHz</essenceTrackSamplingRate> 
       <essenceTrackBitDepth>16</essenceTrackBitDepth> 

       </xsl:if> 
      </instantiationEssenceTrack> 
      </pbcoreInstantiation> 
      </xsl:for-each> 
      </pbcoreDescriptionDocument> 
      </pbcoreCollection> 
</xsl:template>