修改节点项目,其中项目大于或小于整数?

时间:2019-05-25 15:10:20

标签: xml batch-file xslt cmd vbscript

我需要以XML格式输出/修改项目

  1. \conf\carbon.xml向前匹配数字2(至无穷大)整数值
  2. 从XML读取的值大于,小于,小于或等于指定的整数值

条件:对于从TrackNumber 2开始(无限远)匹配的项目:

<TrackNumber>

注意::元素FOR <ScannedTrack> WHERE <TrackNumber>2 (and onwards) IF <Codec>2048</Codec> AND <Bitrate> is <= LESS THAN OR EQUAL TO <Bitrate>192000</Bitrate> OUTPUT/WRITE <Encoder> item as: <Encoder>Ac3Passthrough</Encoder> AND DELETE element/node <MixDown> FOR <ScannedTrack> <TrackNumber>2 (and onwards) IF <Codec>2048</Codec> or <Codec>16777216</Codec> or <Codec>262144</Codec> or <Codec>8192</Codec> or <Codec>4444</Codec> AND <Bitrate> is > GREATER THAN <Bitrate>192000</Bitrate> OUTPUT/WRITE the following to each specific item: <AudioTrack> <MixDown>stereo</MixDown> <Encoder>Ac3</Encoder> <Bitrate>192</Bitrate> 已经包含现有值,因此如果匹配条件,请用上面显示的输出/写入值替换/覆盖这些值。

有人可以帮我编写脚本-VBScript,XSLT或带有Regex的批处理以帮助实现所需的输出吗?

我尝试使用下面的XSL样式表代码,但是我认为我的某些语法不正确,可以使用一些帮助:

<MixDown>, <Encoder> and <Bitrate>

以下是要测试的示例XML文件:

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

        <xsl:mode on-no-match="shallow-copy"/>

        <xsl:template match="AudioTrack[ScannedTrack/TrackNumber = (>= '2')]">
            <xsl:template match="AudioTrack[ScannedTrack[Codec = (2048)] and [Bitrate (<=192000)]/Encoder">
                <xsl:copy>Ac3Passthrough</xsl:copy>
            </xsl:template>


            <xsl:template match="AudioTrack[ScannedTrack/TrackNumber = (>= '2')]">
                <xsl:template match="AudioTrack[ScannedTrack[Codec = (2048, 262144, 8192, 4444, 16777216)] and [Bitrate = (>192000)]/MixDown/Encoder/Bitrate">
                    <xsl:copy>Stereo</xsl:copy>
                    <xsl:copy>Ac3</xsl:copy>
                    <xsl:copy>192</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>
        <Task>
            <AudioTracks>
                <AudioTrack>
                    <MixDown>5point1</MixDown>
                    <Encoder>EAc3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>448</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>1</TrackNumber>
                        <Description>English (E-AC3) (5.1 ch)</Description>
                        <Codec>16777216</Codec>
                        <Bitrate>448000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>5point1</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>2</TrackNumber>
                        <Description>English (AC3) (5.1 ch)</Description>
                        <Codec>2048</Codec>
                        <Bitrate>192000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>EAc3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>224</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>3</TrackNumber>
                        <Description>English (E-AC3) (5.1 ch)</Description>
                        <Codec>16777216</Codec>
                        <Bitrate>224000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>5point1</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>128</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>4</TrackNumber>
                        <Description>English (AC3) (5.1 ch)</Description>
                        <Codec>2048</Codec>
                        <Bitrate>224000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>5point1</MixDown>
                    <Encoder>ffaac</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>256</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>5</TrackNumber>
                        <Description>English (DTS-HD MA) (5.1 ch)</Description>
                        <Codec>262144</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Lame</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>256</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>6</TrackNumber>
                        <Description>English (DTS) (5.1 ch)</Description>
                        <Codec>8192</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>ffaac</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>160</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>7</TrackNumber>
                        <Description>TrueHD 5.1</Description>
                        <Codec>4444</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
            </AudioTracks>
        </Task>
    </QueueTask>
</ArrayOfQueueTask>

原始XML文件的顺序

<?xml version="1.0"?>
<ArrayOfQueueTask xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <QueueTask>
        <Task>
            <AudioTracks>
                <AudioTrack>
                    <MixDown>5point1</MixDown>
                    <Encoder>EAc3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>448</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>1</TrackNumber>
                        <Description>English (E-AC3) (5.1 ch)</Description>
                        <Codec>16777216</Codec>
                        <Bitrate>448000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <Encoder>Ac3Passthrough</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>2</TrackNumber>
                        <Description>English (AC3) (5.1 ch)</Description>
                        <Codec>2048</Codec>
                        <Bitrate>192000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>3</TrackNumber>
                        <Description>English (E-AC3) (5.1 ch)</Description>
                        <Codec>16777216</Codec>
                        <Bitrate>224000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>4</TrackNumber>
                        <Description>English (AC3) (5.1 ch)</Description>
                        <Codec>2048</Codec>
                        <Bitrate>224000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>5</TrackNumber>
                        <Description>English (DTS-HD MA) (5.1 ch)</Description>
                        <Codec>262144</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>6</TrackNumber>
                        <Description>English (DTS) (5.1 ch)</Description>
                        <Codec>8192</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
                <AudioTrack>
                    <MixDown>stereo</MixDown>
                    <Encoder>Ac3</Encoder>
                    <EncoderRateType>Bitrate</EncoderRateType>
                    <Bitrate>192</Bitrate>
                    <ScannedTrack>
                        <TrackNumber>7</TrackNumber>
                        <Description>TrueHD 5.1</Description>
                        <Codec>4444</Codec>
                        <Bitrate>1536000</Bitrate>
                    </ScannedTrack>
                </AudioTrack>
            </AudioTracks>
        </Task>
    </QueueTask>
</ArrayOfQueueTask>

1 个答案:

答案 0 :(得分:2)

这是非常困难且费时的。 AFAICT,您想这样做:

XSLT 1.0

<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="AudioTrack[ScannedTrack/TrackNumber > 1 and ScannedTrack/Codec = 2048 and ScannedTrack/Bitrate &lt;= 192000]">
    <xsl:copy>
        <Encoder>Ac3Passthrough</Encoder>
        <xsl:apply-templates select="*[not(self::MixDown or self::Encoder)]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="AudioTrack[ScannedTrack/TrackNumber > 1 and (ScannedTrack/Codec = 2048 or ScannedTrack/Codec = 262144 or ScannedTrack/Codec = 8192 or ScannedTrack/Codec = 4444) and ScannedTrack/Bitrate > 192000]">
    <xsl:copy>
        <MixDown>stereo</MixDown>
        <Encoder>Ac3</Encoder>
        <Bitrate>192</Bitrate>
        <xsl:apply-templates select="*[not(self::MixDown or self::Encoder or self::Bitrate)]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

添加为回应您的评论:

如果要保留AudioTrack下元素的原始顺序,请尝试:

XSLT 1.0

<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="AudioTrack[ScannedTrack/TrackNumber > 1 and ScannedTrack/Codec = 2048 and ScannedTrack/Bitrate &lt;= 192000]">
    <xsl:copy>
        <xsl:apply-templates select="IsNotifying | DRC | Gain"/>        
        <Encoder>Ac3Passthrough</Encoder>
        <xsl:apply-templates select="SampleRate | EncoderRateType | Bitrate | Quality | TrackName | IsDefault | SampleRateDisplayValue | ScannedTrack"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="AudioTrack[ScannedTrack/TrackNumber > 1 and (ScannedTrack/Codec = 2048 or ScannedTrack/Codec = 262144 or ScannedTrack/Codec = 8192 or ScannedTrack/Codec = 4444) and ScannedTrack/Bitrate > 192000]">
    <xsl:copy>
        <xsl:apply-templates select="IsNotifying | DRC | Gain"/>        
        <MixDown>stereo</MixDown>
        <Encoder>Ac3</Encoder>
        <xsl:apply-templates select="SampleRate | EncoderRateType"/>  
        <Bitrate>192</Bitrate>
        <xsl:apply-templates select="Quality | TrackName | IsDefault | SampleRateDisplayValue | ScannedTrack"/>      
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这是假设原始顺序为:

  • 正在通知
  • DRC
  • 获得
  • MixDown
  • 编码器
  • SampleRate
  • EncoderRateType
  • 比特率
  • 质量
  • TrackName
  • IsDefault
  • SampleRateDisplayValue
  • ScannedTrack