需要帮助选择xsl:for-each和xsl:template

时间:2016-03-08 06:27:46

标签: xml xslt

我正在尝试将xml从一种格式转换为另一种格式,我需要一些帮助,因为我是xslt的新手。 我的输入/源xml是这样的:

<field id="1" media="video" name="ContentLayout" value="Refer to Content Layout View">
        <specifiedLayout />
        <actualLayout>
          <segment endsmptetimecode="00:00:01:03" endtimecode="00:00:01:126" startsmptetimecode="00:00:00:00" starttimecode="00:00:00:000" type="Black" />        
          <segment endsmptetimecode="00:03:23:03" endtimecode="00:03:23:120" startsmptetimecode="00:00:01:03" starttimecode="00:00:01:126" type="Content" />
        Goes on...
        </actualLayout>
</field>

所需的输出xml应该是这样的:

<field id="1" media="video" name="ContentLayout" value="Refer to Content Layout View">
        <specifiedLayout/>
        <actualLayout>
          <segment end="00:00:02:002" endSMPTE="00:00:02:00" start="00:00:00:000" startSMPTE="00:00:00:00" type="Black"/>
          <segment end="00:00:47:081" endSMPTE="00:00:47:02" start="00:00:02:002" startSMPTE="00:00:02:00" type="Content"/>
        </actualLayout>
</field>

属性的映射是这样的:

start = starttimecode
end = endtimecode
startSMPTE = startsmptetimecode
endSMPTE = endsmptetimecode
type = type

我能够像这样识别xml节点:

<xsl:when test="@name='ContentLayout'">

我正在考虑使用xsl:for-each,并在循环内声明5个变量来存储属性值,然后重新分配它。

我觉得这样做的方法并不正确。我不知道如何在这种情况下使用xsl:template。 任何人都可以帮助我/建议吗?

2 个答案:

答案 0 :(得分:1)

在这种情况下,您需要保留大部分现有结构并只进行一些更改,以identity transform模板作为规则开始,并添加一些模板作为例外是很方便的遵守规则:

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="@starttimecode">
    <xsl:attribute name="start">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="@endtimecode">
    <xsl:attribute name="end">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="@startsmptetimecode">
    <xsl:attribute name="startSMPTE">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="@endsmptetimecode">
    <xsl:attribute name="endSMPTE">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

您可以使用local-name()来获取元素/属性的值。  例如:

你尝试下面的代码          

 <xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template     match="@starttimecode|@endtimecode|@startsmptetimecode|@endsmptetimecode|@type">
<xsl:choose>
    <xsl:when test="local-name() = 'starttimecode'">
  <xsl:attribute name="start">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'endtimecode'">
  <xsl:attribute name="end">
        <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'startsmptetimecode'">
  <xsl:attribute name="startSMPTE">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'endsmptetimecode'">
  <xsl:attribute name="endSMPTE">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:when>
<xsl:when test="local-name() = 'type'">
  <xsl:attribute name="type">
<xsl:value-of select="."/>
  </xsl:attribute>
</xsl:when>
</xsl:choose>

</xsl:template>