我试图根据其值选择应用哪个变换。 如果测试是高清晰度,将删除高清晰度,只留下高清,但如果测试是标准,则只有SD将是设定值。
来源xml
<?xml version="1.0" encoding="UTF-8"?>
<file_information>
<asset_data>
<upn>FF074172</upn>
<title>test</title>
<version>High Definition</version>
<duration>00:30</duration>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<aspect_ratio>16X9</aspect_ratio>
<segment>
<sequence>1</sequence>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<comment></comment>
</segment>
</asset_data>
</file_information>
变换
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<file_information>
<asset_data>
<upn>
<xsl:value-of select="//upn"/>
</upn>
<title>
<xsl:value-of select="//title"/>
</title>
<xsl:choose>
<xsl:when test="High Definition">
<version>
<xsl:value-of select="translate(//version,'igh efinition','')"/>
</version>
</xsl:when>
<xsl:otherwise>
<version>
<xsl:value-of select="translate(//version,'tandard','D',)"/>
</version>
</xsl:otherwise>
</xsl:choose>
<duration>
<xsl:value-of select="//duration"/>
</duration>
<tc_in>
<xsl:value-of select="//tc_in"/>
</tc_in>
<tc_out>
<xsl:value-of select="//tc_out"/>
</tc_out>
<aspect_ratio>
<xsl:value-of select="translate(//aspect_ratio,'X',':')"/>
</aspect_ratio>
</asset_data>
</file_information>
预期产出
<?xml version="1.0" encoding="UTF-8"?>
<file_information>
<asset_data>
<upn>FF074172</upn>
<title>test</title>
<version>HD</version>
<duration>00:30</duration>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<aspect_ratio>16:9</aspect_ratio>
<segment>
<sequence>1</sequence>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<comment></comment>
</segment>
</asset_data>
</file_information>
或
<?xml version="1.0" encoding="UTF-8"?>
<file_information>
<asset_data>
<upn>FF074172</upn>
<title>test</title>
<version>SD</version>
<duration>00:30</duration>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<aspect_ratio>16:9</aspect_ratio>
<segment>
<sequence>1</sequence>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<comment></comment>
</segment>
</asset_data>
</file_information>
你知道出了什么问题吗? 谢谢
答案 0 :(得分:0)
试试这个:这里我使用了IDENTITY模板,它将导致所有节点输出,然后我使用了选择性匹配,例如。 'version'和'aspect_ratio'元素。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()"><!--Identity template-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="version">
<xsl:choose>
<xsl:when test=". eq 'High Definition'">
<version><xsl:value-of select="'HD'"/></version>
</xsl:when>
<xsl:otherwise>
<version><xsl:value-of select="'SD'"/></version>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="aspect_ratio">
<xsl:element name="{name()}">
<xsl:value-of select="replace(., 'X', ':')"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()"><!--Identity template-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="version">
<xsl:choose>
<xsl:when test=". = 'High Definition'">
<version><xsl:value-of select="'HD'"/></version>
</xsl:when>
<xsl:otherwise>
<version><xsl:value-of select="'SD'"/></version>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="aspect_ratio">
<xsl:element name="{name()}">
<xsl:value-of select="translate(., 'X', ':')"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>