将xslt应用于具有名称空间的xml

问题描述 投票:0回答:1

[我知道已经在这个问题上回答了多个问题,我已经阅读了其中的几个问题,并试图实施建议,但到目前为止没有成功。

我正在尝试创建一个xsl以应用于xml。 xml来自P2相机卡,并具有名称空间声明。

我已经在我的xsl中添加了一个名称空间声明,但是我仍然似乎无法选择所需的元素。

我的xsl看起来像这样(当前迭代)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1">
<xsl:output method="xml" indent="yes" />

    <xsl:template match="/xsi:P2Main">

<xsl:element name="clip">
  <video>
  <codec>
    <xsl:value-of select="xsi:ClipContent/xsi:EssenceList/xsi:Video/xsi:Codec" />
    </codec>
    <byteoffset>
    <xsl:value-of select="/xsi:P2Main/xsi:ClipContent/xsi:EssenceList/xsi:Video/xsi:VideoIndex/xsi:StartByteOffset" />
    </byteoffset>
    <bytecount>
    <xsl:value-of select="//xsi:Video/xsi:VideoIndex/xsi:DataSize" />
    </bytecount>
     <starttimecode>
     <xsl:value-of select="//xsi:Video/xsi:StartTimeCode" />
      </starttimecode>
      <framerate>
     <xsl:value-of select="//xsi:Video/xsi:FrameRate" /> 
      </framerate>
    </video>
  </xsl:element>
    </xsl:template>
    </xsl:stylesheet>

以及我要转换的xml看起来像这样(不是完整的文件):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<P2Main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1">
  <ClipContent>
    <ClipName>0002CC</ClipName>
    <GlobalClipID>060A2B340101010501010D43130000005E4D83E9398105D4008045826CF62010</GlobalClipID>
    <Duration>220</Duration>
    <EditUnit>1001/24000</EditUnit>
    <EssenceList>
      <Video ValidAudioFlag="false">
        <VideoFormat>MXF</VideoFormat>
        <Codec Class="100">AVC-I_1080/29.97p</Codec>
        <FrameRate>23.98p</FrameRate>
        <StartTimecode>08:24:36:04</StartTimecode>
        <StartBinaryGroup>A30F24C3</StartBinaryGroup>
        <AspectRatio>16:9</AspectRatio>
        <VideoIndex>
          <StartByteOffset>32768</StartByteOffset>
          <DataSize>103854592</DataSize>
        </VideoIndex>
      </Video>
      <Audio>
        <AudioFormat>MXF</AudioFormat>
        <SamplingRate>48000</SamplingRate>
        <BitsPerSample>16</BitsPerSample>
        <AudioIndex>
          <StartByteOffset>32768</StartByteOffset>
          <DataSize>880880</DataSize>
        </AudioIndex>
      </Audio>
      <Audio>
        <AudioFormat>MXF</AudioFormat>
        <SamplingRate>48000</SamplingRate>
        <BitsPerSample>16</BitsPerSample>
        <AudioIndex>
          <StartByteOffset>32768</StartByteOffset>
          <DataSize>880880</DataSize>
        </AudioIndex>
      </Audio>

但是,我似乎永远无法选择所需的节点,要么生成一个仅包含新元素的空文件,要么(包含上述迭代)生成一个包含所有值但不包含所有值的文件元素本身。

have使用xsl v1.0,但我绝不是xsl的专家(可能显示),所以我不确定问题是否出在我的Xpath声明,名称空间或组合上两者之中。

xml xslt xslt-1.0 xml-namespaces
1个回答
0
投票

您没有使用正确的名称空间。 xsi是旨在将XML模式附加到文档的属性的前缀。您的文档已通过urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1 URI绑定到名称空间。

您需要像这样修改样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:P2="urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/P2:P2Main">

  <xsl:element name="clip">
    <video>
      <codec>
        <xsl:value-of select="P2:ClipContent/P2:EssenceList/P2:Video/P2:Codec" />
    </codec>
    <byteoffset>
    <xsl:value-of select="/P2:P2Main/P2:ClipContent/P2:EssenceList/P2:Video/P2:VideoIndex/P2:StartByteOffset" />
    </byteoffset>
    <bytecount>
    <xsl:value-of select="//P2:Video/P2:VideoIndex/xsi:DataSize" />
    </bytecount>
     <starttimecode>
     <xsl:value-of select="//P2:Video/P2:StartTimeCode" />
      </starttimecode>
        <framerate>
          <xsl:value-of select="//P2:Video/P2:FrameRate" /> 
        </framerate>
      </video>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

如果您使用的是XSL-T 1.0,则需要为@select@match属性中的路径中的所有元素加上上述前缀。

对于XSL-T 2.0或更高版本,您可以使用xpath-default-namespace,而无需在所有内容前加上前缀,例如通过使用这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xpath-default-namespace="urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1">

  <xsl:template match="/P2Main">
     ...
     <xsl:value-of select="ClipContent/EssenceList/Video/Codec" />
     ... 

© www.soinside.com 2019 - 2024. All rights reserved.