尝试创建通用XSL以允许稍微不同的XML格式

时间:2016-03-19 13:57:01

标签: xml xslt xpath namespaces transform

我是编写XSL转换的新手,我正在尝试创建一个涵盖某种XML格式的XSL,我在这里有两个例子:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Programme  xmlns="http://www.digitalproductionpartnership.co.uk/ns/as11/2012">
<Editorial xmlns="">
<SeriesTitle>Paul And Nicks Big American Food Trip Series 2</SeriesTitle>
<ProgrammeTitle>Paul And Nicks Big American Food Trip</ProgrammeTitle>
<EpisodeTitleNumber>Washington DC</EpisodeTitleNumber>
<ProductionNumber>PRG86396_1</ProductionNumber>
<Synopsis>Paul and Nick visit Washington DC to cook up an Ulster Scots meal for their guests.</Synopsis>
<Originator>Waddell Media</Originator>
<CopyrightYear>2015</CopyrightYear>
</Editorial>
<Technical xmlns="">
<ShimName>DPP V4.1 HD AVC-I AS-11</ShimName>
<Video>
  <PictureFormat>1080i50 16:9</PictureFormat>
  <AFD>10</AFD>
  <PictureRatio>16:9 (1.78:1)</PictureRatio>
  <ProductPlacement>false</ProductPlacement>
  <VideoComments>
  </VideoComments>
  <VideoBitRate unit="Mbps">100</VideoBitRate>
  <VideoCodecParameters>High 4:2:2 Intra@L4.1</VideoCodecParameters>
</Video>
<Audio>
  <PrimaryAudioLanguage>eng</PrimaryAudioLanguage>
  <SecondaryAudioLanguage>zxx</SecondaryAudioLanguage>
  <TertiaryAudioLanguage>zxx</TertiaryAudioLanguage>
  <AudioLoudnessStandard>_UKDPP_AUDIO_LOUDNESS_STANDARD_EBU_R_128</AudioLoudnessStandard>
  <AudioComments>
  </AudioComments>
</Audio>
<Timecodes>
  <LineUpStart>09:58:00:00</LineUpStart>
  <IdentClockStart>09:59:30:00</IdentClockStart>
  <Parts>
    <Part>
      <PartNumber>1</PartNumber>
      <PartTotal>3</PartTotal>
      <PartSOM>10:00:00:00</PartSOM>
      <PartDuration>00:10:46:00</PartDuration>
    </Part>
    <Part>
      <PartNumber>2</PartNumber>
      <PartTotal>3</PartTotal>
      <PartSOM>10:12:00:00</PartSOM>
      <PartDuration>00:11:34:00</PartDuration>
    </Part>
    <Part>
      <PartNumber>3</PartNumber>
      <PartTotal>3</PartTotal>
      <PartSOM>10:24:38:15</PartSOM>
      <PartDuration>00:00:20:20</PartDuration>
    </Part>
  </Parts>
  <TotalNumberOfParts>3</TotalNumberOfParts>
  <TotalProgrammeDuration>00:22:40:20</TotalProgrammeDuration>
</Timecodes>
<AccessServices>
  <SigningPresent>No</SigningPresent>
  <OpenCaptionsPresent>
  </OpenCaptionsPresent>
</AccessServices>
<AdditionalNode>
  <TextlessElementsExist>True</TextlessElementsExist>
  <CompletionDate>11/02/2016 17:47:42</CompletionDate>
  <ProgrammeHasText>eng</ProgrammeHasText>
</AdditionalNode>
<ContactInformation>
  <ContactEmail>mikemcg@waddellmedia.com</ContactEmail>
</ContactInformation>
</Technical>
</Programme>

第二个显示在这里:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Programme xmlns="http://www.digitalproductionpartnership.co.uk/ns/as11/2015"><Editorial><SeriesTitle>ART</SeriesTitle><ProgrammeTitle>Ep26 Col safe test</ProgrammeTitle><EpisodeTitleNumber>Ep26</EpisodeTitleNumber><ProductionNumber>FKRD991W/01</ProductionNumber><Synopsis>James and Charlie</Synopsis><Originator>STV Productions</Originator><CopyrightYear>2016</CopyrightYear></Editorial><Technical><ShimName>UK DPP HD</ShimName><ShimVersion>1.1</ShimVersion><Video><VideoBitRate unit="Mbps">100</VideoBitRate><VideoCodec>AVC-Intra 100</VideoCodec><VideoCodecParameters>High 4:2:2 Intra@L4.1</VideoCodecParameters><PictureFormat>1080i50 16:9</PictureFormat><AFD>10</AFD><PictureRatio>16:9 (1.78:1)</PictureRatio><ThreeD>false</ThreeD><ProductPlacement>false</ProductPlacement><PSEPass>Yes</PSEPass><PSEManufacturer>Ofcom</PSEManufacturer><PSEVersion>2009</PSEVersion></Video><Audio><AudioSamplingFrequency unit="kHz">48</AudioSamplingFrequency><AudioBitDepth>24</AudioBitDepth><AudioCodecParameters>PCM</AudioCodecParameters><AudioTrackLayout>EBU R 48: 2a</AudioTrackLayout><PrimaryAudioLanguage>eng</PrimaryAudioLanguage><SecondaryAudioLanguage>zxx</SecondaryAudioLanguage><TertiaryAudioLanguage>zxx</TertiaryAudioLanguage><AudioLoudnessStandard>EBU R 128</AudioLoudnessStandard></Audio><Timecodes><LineUpStart>09:59:30:00</LineUpStart><IdentClockStart>09:59:50:00</IdentClockStart><Parts><Part><PartNumber>1</PartNumber><PartTotal>1</PartTotal><PartSOM>10:00:00:00</PartSOM><PartDuration>00:05:25:10</PartDuration></Part></Parts><TotalNumberOfParts>2</TotalNumberOfParts><TotalProgrammeDuration>00:05:25:10</TotalProgrammeDuration></Timecodes><AccessServices><AudioDescriptionPresent>false</AudioDescriptionPresent><ClosedCaptionsPresent>false</ClosedCaptionsPresent><OpenCaptionsPresent>false</OpenCaptionsPresent><SigningPresent>No</SigningPresent></AccessServices><Additional><CompletionDate>2016-01-11</CompletionDate><TextlessElementsExist>false</TextlessElementsExist><ProgrammeHasText>true</ProgrammeHasText><ProgrammeTextLanguage>eng</ProgrammeTextLanguage><AssociatedMediaFilename>Ep26 Col safe test.mxf</AssociatedMediaFilename><MediaChecksumType>MD5</MediaChecksumType><MediaChecksumValue></MediaChecksumValue></Additional><ContactInformation><ContactEmail>lucy.austin@stv.tv</ContactEmail><ContactTelephoneNumber>01413003000</ContactTelephoneNumber></ContactInformation></Technical></Programme>

我正在尝试提取部件的总数,但是想要一个适用于两者的单个XSL。我设法得到一个适用于下面各个单独显示的,但看起来他们的XPATH两者都不同,其中一个需要它的前缀全部通过XPATH而另一个不需要。显然另一个问题是它们的命名空间都略有不同,是否有办法解释它?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//edx:Programme" xmlns:edx="http://www.digitalproductionpartnership.co.uk/ns/as11/2012"><xsl:value-of select="/edx:Programme/Technical/Timecodes/TotalNumberOfParts" /></xsl:template>
</xsl:stylesheet>

第二个XSL在这里:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//edx:Programme" xmlns:edx="http://www.digitalproductionpartnership.co.uk/ns/as11/2015"><xsl:value-of select="/edx:Programme/edx:Technical/edx:Timecodes/edx:TotalNumberOfParts" /></xsl:template>
</xsl:stylesheet>

所以,只是为了澄清是否有一种方法可以编写单个XSL来从两个XML中提取部分数量,因此无论哪个XML都是源,转换对两者都有效?

非常感谢

3 个答案:

答案 0 :(得分:2)

如果您将两个模板(在xsl:template上使用本地名称空间声明)放入单个样式表中,那么XSLT处理器将应用与输入格式匹配的模板。

答案 1 :(得分:0)

通常,当您需要处理两个非常相似的不同XML词汇表时(特别是如果命名空间不同),最简单的方法是编写一个转换,将一个词汇表转换为另一个词汇表(或转换它们两者)进入一些常见的格式),而不是试图在同一过程中处理这两种变种。好处是,一旦您编写了转换代码,它就可以高度重复使用,而且您再也无法应对这两种不同的风格。

答案 2 :(得分:0)

您可以在元素的结构和/books/thug-kitchen上更一般地进行XPath匹配,无论元素绑定到哪个名称空间。

XSLT 2.0:

local-name()

XSLT 1.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="*:Programme">
    <xsl:value-of select="*:Technical/*:Timecodes/*:TotalNumberOfParts" />
  </xsl:template>
</xsl:stylesheet>