1

我想从 MediaInfo 的 XML 生成的持续时间标记中输出一致的 hh:mm:ss 标记。但我不确定如何创建一个 xsl:template 以按照我想要的方式格式化持续时间。MediaInfo 以两种方式为我输出音频文件的持续时间,例如:

<MediaInfoDuration>1h 58mn</MediaInfoDuration> 

我想翻译成

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

<MediaInfoDuration>56mn 48s</MediaInfoDuration> 

我想翻译成

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

我唯一真正知道怎么做的是使用子字符串,但是由于“mn”可以出现在不同的位置,而“s”有时根本不会出现,我不知道该怎么做。我想我需要员工变量,也许还有 xsl:choose,但我不确定。

4

2 回答 2

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> 

于 2013-09-30T16:27:03.140 回答
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>
于 2013-09-25T21:12:54.333 回答