使用另一个位置上的值将XML转换为XSL

时间:2015-04-08 21:48:42

标签: html xml xslt tags

我试图将我的XML转换为XSL。

我成功完成了第一部分,但现在我仍然坚持第二部分。我尝试从其他地方定义的键中获取值。

也许更容易展示你:

传入XML文件:

<?xml version="1.0" encoding="utf-16"?>
<AXFRoot>
  <MAObject type="default" mdclass="PROGRAM">
    <GUID dmname="">201406121715517010024191178005056B93D790000004460B00000D0F000989</GUID>
    <Meta name="VRT_ESSENCE_DURATION" format="string" frate="">355280</Meta>
    <Meta name="VRT_ESSENCE_AUDIO_CHANNELS" format="string" frate="">4</Meta>
    <Meta name="VRT_CLIP_ID" format="string" frate="" />


    <StratumEx name="VRT_CONTENT">
      <Group orderidx="0" id="0" lastchanged="00010101000000">
        <Segment id="0" contentid="999e584a-7192-45ff-8570-af6587e2938f" begin="0" end="8600" />
        <Segment id="1" contentid="23502d29-a0b4-48c5-a8b2-596a195e2768" begin="8640" end="19760" />
        <Segment id="2" contentid="b246ffb6-a56f-4e4e-8bd9-5da3b9cd7351" begin="19800" end="34840" />
        <Segment id="3" contentid="1643dde7-3c5d-4675-a52e-77d00b70309b" begin="49000" end="49000" />
        <Segment id="4" contentid="5ce3968f-5324-4172-90f3-bd6d22f696a7" begin="54800" end="58480" />
      </Group>
    </StratumEx>
    <StratumEx name="VRT_ITEMS">
      <Group orderidx="0" id="0" lastchanged="00010101000000">
        <Segment id="0" contentid="328626e6-c794-494d-939a-39f7aa2e5a85" begin="11120" end="49200" />
        <Segment id="1" contentid="1effe83f-7076-418f-b86f-cbd6ac6ba7ba" begin="81200" end="86600" />
      </Group>
    </StratumEx>
    <StratumEx name="VRT_RESTRICTION" />
  </MAObject>

  <MAObject type="default" mdclass="S_VRT_CONTENT">
    <GUID dmname="">999e584a-7192-45ff-8570-af6587e2938f</GUID>
    <Meta name="VRT_CONTENT_TXT" format="string" frate="">Frag1</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_CONTENT">
    <GUID dmname="">23502d29-a0b4-48c5-a8b2-596a195e2768</GUID>
    <Meta name="VRT_CONTENT_TXT" format="string" frate="">Frag2</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_CONTENT">
    <GUID dmname="">b246ffb6-a56f-4e4e-8bd9-5da3b9cd7351</GUID>
    <Meta name="VRT_CONTENT_TXT" format="string" frate="">Frag3</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_CONTENT">
    <GUID dmname="">1643dde7-3c5d-4675-a52e-77d00b70309b</GUID>
    <Meta name="VRT_CONTENT_TXT" format="string" frate="">Frag4</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_CONTENT">
    <GUID dmname="">5ce3968f-5324-4172-90f3-bd6d22f696a7</GUID>
    <Meta name="VRT_CONTENT_TXT" format="string" frate="">Frag5</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_ITEMS">
    <GUID dmname="">328626e6-c794-494d-939a-39f7aa2e5a85</GUID>
    <Meta name="VRT_ITEMS_TXT" format="string" frate="">Frag6</Meta>
  </MAObject>
  <MAObject type="default" mdclass="S_VRT_ITEMS">
    <GUID dmname="">1effe83f-7076-418f-b86f-cbd6ac6ba7ba</GUID>
    <Meta name="VRT_ITEMS_TXT" format="string" frate="">Frag7</Meta>
  </MAObject>
</AXFRoot>

通缉结果:

<MediaHAVEN_external_metadata version="1.0" name="VIAA">
    <MDProperties>

      <MEDIA_ID>201406121715517010024191178005056B93D790000004460B00000D0F000989</MEDIA_ID>
      <VRT_ESSENCE_DURATION>355280</VRT_ESSENCE_DURATION>
      <VRT_ESSENCE_AUDIO_CHANNELS>4</VRT_ESSENCE_AUDIO_CHANNELS>
      <VRT_CLIP_ID></VRT_CLIP_ID>


    </MDProperties>
    <fragments>
        <fragment>
            <title>Frag1</title>
            <original_start_z>0</original_start_z>
            <original_end_z>8600</original_end_z>
        </fragment>
        <fragment>
            <title>Frag2</title>
            <original_start_z>8640</original_start_z>
            <original_end_z>19760</original_end_z>
        </fragment>
        <fragment>
            <title>Frag3</title>
            <original_start_z>19800</original_start_z>
            <original_end_z>34840</original_end_z>
        </fragment>
        <fragment>
            <title>Frag4</title>
            <original_start_z>49000</original_start_z>
            <original_end_z>49000</original_end_z>
        </fragment>
        <fragment>
            <title>Frag5</title>
            <original_start_z>54800</original_start_z>
            <original_end_z>58480</original_end_z>
        </fragment>
        <fragment>
            <title>Frag6</title>
            <original_start_z>11120</original_start_z>
            <original_end_z>49200</original_end_z>
        </fragment>
        <fragment>
            <title>Frag7</title>
            <original_start_z>81200</original_start_z>
            <original_end_z>86600</original_end_z>
        </fragment>
    </fragments>
</MediaHAVEN_external_metadata>

正如您所看到的那样,那些有争议的人会查找“Frag&#39; Frag&#39;数据。在我的示例中:http://xsltransform.net/94hvTzt&#39; Frag&#39;你现在看到的数据,不应该属于那里......

有人能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,不要这样做以在XSLT中创建元素......

 <xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="@name" /><xsl:text disable-output-escaping="yes">&gt;</xsl:text>

那种方式就是疯狂。如果要创建动态命名元素,请使用xsl:element命令。你只需要这样做....

 <xsl:element name="{@name}">
      <xsl:value-of select="."/>
 </xsl:element>

但是在回答您的问题时,要从XML文档中的其他位置查找日期,请使用xsl:key

 <xsl:key name="fragment" match="MAObject" use="GUID" />

要使用此密钥,要查找数据,您只需执行以下操作(假设您位于segment元素上)

 <xsl:value-of select="key('fragment', @contentid)/Meta" />

试试这个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="fragment" match="MAObject" use="GUID" />

<xsl:template match="/">
    <MediaHAVEN_external_metadata version="1.0" name="VIAA">
        <MDProperties>
            <MEDIA_ID><xsl:value-of select="AXFRoot/MAObject[StratumEx]/GUID" /></MEDIA_ID>
            <xsl:for-each select="AXFRoot/MAObject[StratumEx]/Meta">
                <xsl:element name="{@name}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
        </MDProperties>
        <fragments>
            <xsl:for-each select="AXFRoot/MAObject/StratumEx/Group/Segment">
                <fragment>
                    <title><xsl:value-of select="key('fragment', @contentid)/Meta" /></title>
                    <original_start_z><xsl:value-of select="@begin" /></original_start_z>
                    <original_end_z><xsl:value-of select="@end" /></original_end_z>
                </fragment>
            </xsl:for-each>
         </fragments>
    </MediaHAVEN_external_metadata>
</xsl:template>
</xsl:stylesheet>

或者也许这样,采取更基于模板的方法

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="fragment" match="MAObject" use="GUID" />

<xsl:template match="/">
    <MediaHAVEN_external_metadata version="1.0" name="VIAA">
        <xsl:apply-templates select="AXFRoot/MAObject[StratumEx]" />
    </MediaHAVEN_external_metadata>
</xsl:template>

<xsl:template match="MAObject">
    <MDProperties>
        <MEDIA_ID><xsl:value-of select="GUID" /></MEDIA_ID>
        <xsl:apply-templates select="Meta" />
    </MDProperties>
    <fragments>
        <xsl:apply-templates select="StratumEx/Group/Segment" />
     </fragments>
</xsl:template>

<xsl:template match="Meta">
    <xsl:element name="{@name}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

<xsl:template match="Segment">
    <fragment>
        <title><xsl:value-of select="key('fragment', @contentid)/Meta" /></title>
        <original_start_z><xsl:value-of select="@begin" /></original_start_z>
        <original_end_z><xsl:value-of select="@end" /></original_end_z>
    </fragment>
</xsl:template>
</xsl:stylesheet>