如果匹配文本字符串,则删除子级

时间:2019-06-20 23:48:38

标签: xml xslt xslt-1.0

我正在尝试删除特定的<SubtitleTrack>分支,其中<SubtitleType>包含文本 PGS

我想我已经快到了,但是我不知道如何删除特定的SubtitleTrack分支。

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

<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="SubtitleTracks">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="SubtitleTrack/SourceTrack/SubtitleType=PGS">
                [???]
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </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>E:\TEMP\MyVideo.mkv</ScannedSourcePath>
    <Status>Waiting</Status>
    <Task>
      <IsNotifying>true</IsNotifying>
      <ShowAdvancedTab>false</ShowAdvancedTab>
      <SubtitleTracks>
        <SubtitleTrack>
          <IsNotifying>true</IsNotifying>
          <Burned>false</Burned>
          <Default>false</Default>
          <Forced>false</Forced>
          <SourceTrack>
            <SourceId>7</SourceId>
            <TrackNumber>1</TrackNumber>
            <Language>English [PGS]</Language>
            <LanguageCode>eng</LanguageCode>
            <SubtitleType>PGS</SubtitleType>
          </SourceTrack>
          <SrtOffset>0</SrtOffset>
          <SubtitleType>PGS</SubtitleType>
        </SubtitleTrack>
        <SubtitleTrack>
          <IsNotifying>true</IsNotifying>
          <Burned>false</Burned>
          <Default>false</Default>
          <Forced>false</Forced>
          <SourceTrack>
            <SourceId>4</SourceId>
            <TrackNumber>2</TrackNumber>
            <Language>English [UTF-8]</Language>
            <LanguageCode>eng</LanguageCode>
            <SubtitleType>UTF8Sub</SubtitleType>
          </SourceTrack>
          <SrtOffset>0</SrtOffset>
          <SubtitleType>UTF8Sub</SubtitleType>
        </SubtitleTrack>
      </SubtitleTracks>
      <Source>E:\TEMP\MyVideo.mkv</Source>
      <Destination>C:\TempOutput\MyVideoEncode.mkv</Destination>
      <OutputFormat>Mkv</OutputFormat>
      <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" />
  </QueueTask>
</ArrayOfQueueTask>

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

源xml缺少结束标记任务

<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="SubtitleTrack">    
    <xsl:choose>
        <xsl:when test="SourceTrack[not(contains(SubtitleType,'PGS'))]">
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

您可以使用 predicate 直接匹配要删除的元素,并通过一个空模板对其进行处理:

<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="SubtitleTrack[SourceTrack/SubtitleType='PGS']"/>

</xsl:stylesheet>

或者不将模板应用于它们以开始:

<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="SubtitleTracks">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::SubtitleTrack[SourceTrack/SubtitleType='PGS'])]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

答案 2 :(得分:0)

only change following code:-

<xsl:when test="SubtitleTrack/SourceTrack/SubtitleType='PGS'">