XML - удалить вложенный элемент на основе соответствующего условия, а затем переименовать остальные элементы? - PullRequest
0 голосов
/ 23 мая 2019

У меня есть XML-файл, я хотел бы удалить определенный элемент на основе соответствующего условия, а затем переименовать / перенумеровать оставшиеся элементы.

<AudioTracks> в целом может содержать несколько различных <ScannedTrack> и число в пределах

<AudioTracks>
 <AudioTrack>
  <ScannedTrack>

содержит входную аудиодорожку, которая читается

<AudioTracks>
 <AudioTrack>

содержит настройки кодирования звука для вывода

Я хотел бы внести следующие изменения, основываясь на вводе:

If <ScannedTrack>
    <TrackNumber>1</TrackNumber> AND <Codec>262144</Codec>
AND If <ScannedTrack>
    <TrackNumber>2</TrackNumber> AND <Codec>8192</Codec>

затем DELETE <AudioTrack> элемент, где <TrackNumber>1

тогда

IF <TrackNumber>2 exist rename as <TrackNumber>1
IF <TrackNumber>3 exist rename as <TrackNumber>2
IF <TrackNumber>4 exist rename as <TrackNumber>3

(и т. Д. До бесконечности)

т.е. Все последующие дорожки затем перемещаются назад на 1 идентификатор дорожки (т.е. оригинальная дорожка 3 теперь становится новой дорожкой 2, оригинальная дорожка 4 теперь перемещается в новую дорожку 3 и т. Д.)

Может ли кто-нибудь помочь с решением изменить файл XML в соответствии с вышеуказанными требованиями?

Вот исходный 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:\Temp\Example.mkv</ScannedSourcePath>
    <Status>Waiting</Status>
    <Task>
      <IsNotifying>true</IsNotifying>
      <ShowAdvancedTab>false</ShowAdvancedTab>
      <AudioTracks>
        <AudioTrack>
          <IsNotifying>true</IsNotifying>
          <DRC>0</DRC>
          <Gain>0</Gain>
          <MixDown>5point1</MixDown>
          <Encoder>Ac3</Encoder>
          <SampleRate>0</SampleRate>
          <EncoderRateType>Bitrate</EncoderRateType>
          <Bitrate>384</Bitrate>
          <Quality>0</Quality>
          <IsDefault>false</IsDefault>
          <SampleRateDisplayValue>Auto</SampleRateDisplayValue>
          <ScannedTrack>
            <TrackNumber>1</TrackNumber>
            <Language>English</Language>
            <LanguageCode>eng</LanguageCode>
            <Description>English (DTS-HD MA) (5.1 ch)</Description>
            <Codec>262144</Codec>
            <SampleRate>48000</SampleRate>
            <Bitrate>1</Bitrate>
            <ChannelLayout>1551</ChannelLayout>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <IsNotifying>true</IsNotifying>
          <DRC>0</DRC>
          <Gain>0</Gain>
          <MixDown>5point1</MixDown>
          <Encoder>Ac3</Encoder>
          <SampleRate>0</SampleRate>
          <EncoderRateType>Bitrate</EncoderRateType>
          <Bitrate>384</Bitrate>
          <Quality>0</Quality>
          <IsDefault>false</IsDefault>
          <SampleRateDisplayValue>Auto</SampleRateDisplayValue>
          <ScannedTrack>
            <TrackNumber>2</TrackNumber>
            <Language>English</Language>
            <LanguageCode>eng</LanguageCode>
            <Description>English (DTS) (5.1 ch)</Description>
            <Codec>8192</Codec>
            <SampleRate>48000</SampleRate>
            <Bitrate>1536000</Bitrate>
            <ChannelLayout>1551</ChannelLayout>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <IsNotifying>true</IsNotifying>
          <DRC>0</DRC>
          <Gain>0</Gain>
          <MixDown>5point1</MixDown>
          <Encoder>Ac3</Encoder>
          <SampleRate>48</SampleRate>
          <EncoderRateType>Bitrate</EncoderRateType>
          <Bitrate>384</Bitrate>
          <Quality xsi:nil="true" />
          <TrackName />
          <IsDefault>false</IsDefault>
          <SampleRateDisplayValue>48</SampleRateDisplayValue>
          <ScannedTrack>
            <TrackNumber>3</TrackNumber>
            <Language>English</Language>
            <LanguageCode>eng</LanguageCode>
            <Description>English (DTS-HD MA) (5.1 ch)</Description>
            <Codec>262144</Codec>
            <SampleRate>48000</SampleRate>
            <Bitrate>1</Bitrate>
            <ChannelLayout>1551</ChannelLayout>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <IsNotifying>true</IsNotifying>
          <DRC>0</DRC>
          <Gain>0</Gain>
          <MixDown>5point1</MixDown>
          <Encoder>Ac3</Encoder>
          <SampleRate>48</SampleRate>
          <EncoderRateType>Bitrate</EncoderRateType>
          <Bitrate>384</Bitrate>
          <Quality xsi:nil="true" />
          <TrackName />
          <IsDefault>false</IsDefault>
          <SampleRateDisplayValue>48</SampleRateDisplayValue>
          <ScannedTrack>
            <TrackNumber>4</TrackNumber>
            <Language>English</Language>
            <LanguageCode>eng</LanguageCode>
            <Description>English (DTS-HD MA) (5.1 ch)</Description>
            <Codec>262144</Codec>
            <SampleRate>48000</SampleRate>
            <Bitrate>1</Bitrate>
            <ChannelLayout>1551</ChannelLayout>
          </ScannedTrack>
        </AudioTrack>
      </AudioTracks>
      <SubtitleTracks>
        <SubtitleTrack>
          <IsNotifying>true</IsNotifying>
          <Burned>false</Burned>
          <Default>false</Default>
          <Forced>false</Forced>
          <SourceTrack>
            <SourceId>0</SourceId>
            <TrackNumber>1</TrackNumber>
            <Language>English [VOBSUB]</Language>
            <LanguageCode>eng</LanguageCode>
            <SubtitleType>VobSub</SubtitleType>
          </SourceTrack>
          <SrtOffset>0</SrtOffset>
          <SubtitleType>VobSub</SubtitleType>
        </SubtitleTrack>
        <SubtitleTrack>
          <IsNotifying>true</IsNotifying>
          <Burned>false</Burned>
          <Default>false</Default>
          <Forced>false</Forced>
          <SourceTrack>
            <SourceId>0</SourceId>
            <TrackNumber>2</TrackNumber>
            <Language>English [VOBSUB]</Language>
            <LanguageCode>eng</LanguageCode>
            <SubtitleType>VobSub</SubtitleType>
          </SourceTrack>
          <SrtOffset>0</SrtOffset>
          <SubtitleType>VobSub</SubtitleType>
        </SubtitleTrack>
      </SubtitleTracks>
      <Source>C:\Temp.mkv</Source>
      <Title>1</Title>
      <Angle>1</Angle>
      <PointToPointMode>Chapters</PointToPointMode>
      <StartPoint>1</StartPoint>
      <EndPoint>20</EndPoint>
      <Destination>C:\Temp\ExampleEncoded.mkv</Destination>
      <OutputFormat>Mkv</OutputFormat>
      <OptimizeMP4>false</OptimizeMP4>
      <IPod5GSupport>false</IPod5GSupport>
      <AlignAVStart>false</AlignAVStart>
      <Width>720</Width>
      <Height>404</Height>
      <MaxWidth xsi:nil="true" />
      <MaxHeight xsi:nil="true" />
      <Cropping>
        <Top>0</Top>
        <Bottom>0</Bottom>
        <Left>0</Left>
        <Right>0</Right>
      </Cropping>
      <HasCropping>false</HasCropping>
      <Anamorphic>Automatic</Anamorphic>
      <DisplayWidth>746</DisplayWidth>
      <KeepDisplayAspect>true</KeepDisplayAspect>
      <PixelAspectX>202</PixelAspectX>
      <PixelAspectY>195</PixelAspectY>
      <Modulus>2</Modulus>
      <DeinterlaceFilter>Off</DeinterlaceFilter>
      <CombDetect>Off</CombDetect>
      <CustomCombDetect />
      <Detelecine>Off</Detelecine>
      <CustomDetelecine />
      <Denoise>Off</Denoise>
      <DenoisePreset>Light</DenoisePreset>
      <DenoiseTune>None</DenoiseTune>
      <CustomDenoise />
      <Deblock>4</Deblock>
      <Grayscale>false</Grayscale>
      <Rotation>0</Rotation>
      <FlipVideo>false</FlipVideo>
      <Sharpen>Off</Sharpen>
      <SharpenPreset>
        <DisplayName>Medium</DisplayName>
        <Key>medium</Key>
      </SharpenPreset>
      <SharpenTune>
        <DisplayName>None</DisplayName>
        <Key>none</Key>
      </SharpenTune>
      <SharpenCustom />
      <VideoEncodeRateType>ConstantQuality</VideoEncodeRateType>
      <VideoEncoder>X264</VideoEncoder>
      <FramerateMode>VFR</FramerateMode>
      <Quality>19</Quality>
      <VideoBitrate xsi:nil="true" />
      <TwoPass>true</TwoPass>
      <TurboFirstPass>false</TurboFirstPass>
      <Framerate xsi:nil="true" />
      <AllowedPassthruOptions>
        <AudioAllowAACPass>true</AudioAllowAACPass>
        <AudioAllowAC3Pass>false</AudioAllowAC3Pass>
        <AudioAllowDTSHDPass>false</AudioAllowDTSHDPass>
        <AudioAllowDTSPass>false</AudioAllowDTSPass>
        <AudioAllowMP3Pass>true</AudioAllowMP3Pass>
        <AudioAllowTrueHDPass>false</AudioAllowTrueHDPass>
        <AudioAllowFlacPass>false</AudioAllowFlacPass>
        <AudioAllowEAC3Pass>false</AudioAllowEAC3Pass>
        <AudioEncoderFallback>Ac3</AudioEncoderFallback>
      </AllowedPassthruOptions>
      <IncludeChapterMarkers>true</IncludeChapterMarkers>
      <ChapterNames>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>1</ChapterNumber>
          <Duration />
          <ChapterName>00:00:00.000</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>2</ChapterNumber>
          <Duration />
          <ChapterName>00:03:43.723</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>3</ChapterNumber>
          <Duration />
          <ChapterName>00:10:31.756</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>4</ChapterNumber>
          <Duration />
          <ChapterName>00:13:25.847</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>5</ChapterNumber>
          <Duration />
          <ChapterName>00:17:38.641</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>6</ChapterNumber>
          <Duration />
          <ChapterName>00:19:35.841</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>7</ChapterNumber>
          <Duration />
          <ChapterName>00:25:12.302</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>8</ChapterNumber>
          <Duration />
          <ChapterName>00:28:01.305</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>9</ChapterNumber>
          <Duration />
          <ChapterName>00:35:09.065</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>10</ChapterNumber>
          <Duration />
          <ChapterName>00:43:07.084</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>11</ChapterNumber>
          <Duration />
          <ChapterName>00:49:29.592</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>12</ChapterNumber>
          <Duration />
          <ChapterName>00:52:14.214</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>13</ChapterNumber>
          <Duration />
          <ChapterName>00:59:24.978</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>14</ChapterNumber>
          <Duration />
          <ChapterName>01:02:53.728</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>15</ChapterNumber>
          <Duration />
          <ChapterName>01:15:50.921</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>16</ChapterNumber>
          <Duration />
          <ChapterName>01:21:28.801</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>17</ChapterNumber>
          <Duration />
          <ChapterName>01:24:23.183</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>18</ChapterNumber>
          <Duration />
          <ChapterName>01:31:45.875</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>19</ChapterNumber>
          <Duration />
          <ChapterName>01:35:52.664</ChapterName>
        </ChapterMarker>
        <ChapterMarker>
          <IsNotifying>true</IsNotifying>
          <ChapterNumber>20</ChapterNumber>
          <Duration />
          <ChapterName>01:44:06.282</ChapterName>
        </ChapterMarker>
      </ChapterNames>
      <AdvancedEncoderOptions />
      <VideoProfile>
        <DisplayName>Auto</DisplayName>
        <ShortName>auto</ShortName>
      </VideoProfile>
      <VideoLevel>
        <DisplayName>Auto</DisplayName>
        <ShortName>auto</ShortName>
      </VideoLevel>
      <VideoPreset>
        <DisplayName>Fast</DisplayName>
        <ShortName>fast</ShortName>
      </VideoPreset>
      <VideoTunes />
      <ExtraAdvancedArguments>level=31</ExtraAdvancedArguments>
      <MetaData />
      <IsPreviewEncode>false</IsPreviewEncode>
      <PreviewEncodeDuration xsi:nil="true" />
      <PreviewEncodeStartAt xsi:nil="true" />
    </Task>
    <Configuration>
      <IsDvdNavDisabled>true</IsDvdNavDisabled>
      <EnableQuickSyncDecoding>false</EnableQuickSyncDecoding>
      <UseQSVDecodeForNonQSVEnc>false</UseQSVDecodeForNonQSVEnc>
      <ScalingMode>Lanczos</ScalingMode>
      <PreviewScanCount>10</PreviewScanCount>
      <Verbosity>2</Verbosity>
      <MinScanDuration>5</MinScanDuration>
      <SaveLogToCopyDirectory>false</SaveLogToCopyDirectory>
      <SaveLogWithVideo>false</SaveLogWithVideo>
      <SaveLogCopyDirectory />
      <RemoteServiceEnabled>false</RemoteServiceEnabled>
      <RemoteServicePort>8080</RemoteServicePort>
    </Configuration>
    <Statistics>
      <IsNotifying>true</IsNotifying>
      <StartTime>0001-01-01T00:00:00</StartTime>
      <EndTime>0001-01-01T00:00:00</EndTime>
      <FinalFileSize xsi:nil="true" />
    </Statistics>
  </QueueTask>
</ArrayOfQueueTask>

Что-то вроде:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-match="TrackNumber = 1"/>

  <xsl:template match="AudioTrack[ScannedTrack[Codec = (262144, 8192)]]/Encoder">
      <xsl:delete>AudioTrack</xsl:copy>
  </xsl:template>

</xsl:stylesheet>

У меня плохое кодирование, но я также не знаю, как запустить цикл for для остальных треков.

EDIT: исправлена ​​ошибка, исправлен XML-файл

1 Ответ

0 голосов
/ 24 мая 2019

Рассмотрим следующий упрощенный пример:

XML

<ArrayOfQueueTask>
  <QueueTask>
    <Task>
      <AudioTracks>
        <AudioTrack>
          <ScannedTrack>
            <TrackNumber>1</TrackNumber>
            <Description>A</Description>
            <Codec>262144</Codec>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <ScannedTrack>
            <TrackNumber>2</TrackNumber>
            <Description>B</Description>
            <Codec>8192</Codec>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <ScannedTrack>
            <TrackNumber>3</TrackNumber>
            <Description>C</Description>
            <Codec>262144</Codec>
          </ScannedTrack>
        </AudioTrack>
        <AudioTrack>
          <ScannedTrack>
            <TrackNumber>4</TrackNumber>
            <Description>D</Description>
           <Codec>262144</Codec>
          </ScannedTrack>
        </AudioTrack>
      </AudioTracks>
    </Task>
  </QueueTask>
</ArrayOfQueueTask>

XSLT 2.0

<xsl:stylesheet version="2.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="AudioTracks">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="AudioTrack[1]/ScannedTrack/Codec=262144 and AudioTrack[2]/ScannedTrack/Codec=8192">
                <xsl:apply-templates select="AudioTrack[position() > 1]">
                    <xsl:with-param name="remove-track1" select="1" tunnel="yes"/>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

<xsl:template match="ScannedTrack/TrackNumber">
    <xsl:param name="remove-track1" select="0" tunnel="yes"/>
    <xsl:copy>
        <xsl:value-of select=". - $remove-track1" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Результат

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfQueueTask>
   <QueueTask>
      <Task>
         <AudioTracks>
            <AudioTrack>
               <ScannedTrack>
                  <TrackNumber>1</TrackNumber>
                  <Description>B</Description>
                  <Codec>8192</Codec>
               </ScannedTrack>
            </AudioTrack>
            <AudioTrack>
               <ScannedTrack>
                  <TrackNumber>2</TrackNumber>
                  <Description>C</Description>
                  <Codec>262144</Codec>
               </ScannedTrack>
            </AudioTrack>
            <AudioTrack>
               <ScannedTrack>
                  <TrackNumber>3</TrackNumber>
                  <Description>D</Description>
                  <Codec>262144</Codec>
               </ScannedTrack>
            </AudioTrack>
         </AudioTracks>
      </Task>
   </QueueTask>
</ArrayOfQueueTask>

Демо : https://xsltfiddle.liberty -development.net / ncdD7mQ

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...