2014-10-27 26 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