2014-10-27 112 views
0

我试图根据它的值选择应用哪种转换。 如果测试是高清晰度,高清晰度将被删除,只留下HD,但如果测试是标准,则只有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

谢谢你们! 这解决了我的问题(而不是=当量,而不是翻译替换):

<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> 
0

试试这个:在这里我使用了IDENTITY模板,这将导致所有节点输出,然后我使用了选择性匹配,例如。 '版本'和'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> 
+0

正因为如此,这是一个XSLT 2.0答案(即使版本设置为1.0)。 – 2014-10-27 16:20:07

+0

更改 to and (逗号在'D'移除后) – 2014-10-27 16:56:44