XSLT 1.0-条件匹配时替换节点中的文本字符串

时间:2019-06-26 01:08:39

标签: xml xslt

使用XSLT 1.0匹配条件时,我需要覆盖/替换字符串的一部分

Destination 始终包含文本字符串-MyEnc480p-AC3-CRF19.mkv

不过,开头的标题可能会有所不同,例如MyVideo1-MyEnc480p-AC3-CRF19.mkvMyVideo2-MyEnc480p-AC3-CRF19.mkv

预期结果:

AudioTrack/TrackNumber = 1AudioTrack/Description contains = AAC

在节点Destination中,文本AC3AAC取代

MyVideo1-MyEnc480p-AC3-CRF19.mkv应更改为MyVideo1-MyEnc480p-AAC-CRF19.mkv

这是我到目前为止所拥有的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  >

    <!-- copy all nodes and attributes -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- match item where the trackNumber = 1 and contains text = AAC -->
<xsl:template match="AudioTracks(AudioTrack(ScannedTrack/TrackNumber) = 1 and (ScannedTrack/Description) contains = AAC)]">

    <xsl:template match="Destination">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>

            <!-- replace text AC3 with AAC -->
        <xsl:when test="contains('AC3')">
        <xsl:sequence select="replace('AC3', 'AAC')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

这是我正在使用的XML:

<?xml version="1.0"?>
<ArrayOfQueueTask xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <QueueTask>
    <IsNotifying>true</IsNotifying>
    <ScannedSourcePath>C:\VIDEO\MyVideo.mkv</ScannedSourcePath>
    <Status>Waiting</Status>
    <Task>
      <IsNotifying>true</IsNotifying>
      <ShowAdvancedTab>false</ShowAdvancedTab>
      <AudioTracks>
        <AudioTrack>
          <IsNotifying>true</IsNotifying>
          <ScannedTrack>
            <TrackNumber>1</TrackNumber>
            <Language>English</Language>
            <LanguageCode>eng</LanguageCode>
            <Description>English (AAC LC) (2.0 ch)</Description>
            <Codec>65536</Codec>
            <SampleRate>48000</SampleRate>
            <Bitrate>1</Bitrate>
            <ChannelLayout>3</ChannelLayout>
          </ScannedTrack>
          <Encoder>AacPassthru</Encoder>
          <IsDefault>false</IsDefault>
        </AudioTrack>
      </AudioTracks>
     <Source>C:\VIDEO\MyVideo.mkv</Source>
      <Title>1</Title>
      <Angle>1</Angle>
      <PointToPointMode>Chapters</PointToPointMode>
      <StartPoint>1</StartPoint>
      <EndPoint>1</EndPoint>
      <Destination>C:\Temp\MyVideo-MyEnc480p-AC3-CRF19.mkv</Destination>
      <OutputFormat>Mkv</OutputFormat>
      <OptimizeMP4>false</OptimizeMP4>
      <IPod5GSupport>false</IPod5GSupport>
      <AlignAVStart>false</AlignAVStart>
    </Task>
  </QueueTask>
</ArrayOfQueueTask>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您的情况尚不完全清楚。假设当DestinationDescription的{​​{1}}的{​​{1}}的{​​{1}}包含ScannedTrack时,您想修改TrackNumber,可以执行以下操作: / p>

XSLT 1.0

1

重新尝试:

  • 模板不能嵌套;
  • "AAC"<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:strip-space elements="*"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Destination[contains(../AudioTracks/AudioTrack/ScannedTrack[TrackNumber=1]/Description, 'AAC')]"> <xsl:copy> <xsl:value-of select="substring-before(., '-MyEnc480p-AC3-CRF19.mkv')" /> <xsl:text>-MyEnc480p-AAC-CRF19.mkv</xsl:text> </xsl:copy> </xsl:template> </xsl:stylesheet> 需要XSLT 2.0;
  • 谓词使用方括号。