【发布时间】:2014-11-16 18:49:09
【问题描述】:
基本上我是 xlst 新手,负责对大型 xls 文件进行一些更改,该文件处理德国市场的电影元数据转换。
xls 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:str="http://exslt.org/strings" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:redirect="http://xml.apache.org/xalan/redirect" extension-element-prefixes="redirect" xmlns:xalan="http://xml.apache.org/xslt" exclude-result-prefixes="xalan str">
<xsl:output method="xml" indent="yes" xalan:indent-amount="4"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/metadata">
<xsl:variable name="featureID" select="substring(mpm_product_id, 7, string-length(mpm_product_id))"/>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<Metadata>
<some values...>
<xsl:for-each select="genres/genre">
<Genre>
<xsl:choose>
<!-- Mappings for German Genres -->
<xsl:when test="/metadata/base/territory_code='DE'">
<xsl:choose>
<xsl:when test=".= 'Action'">Action und Abenteuer</xsl:when>
<xsl:when test=".= 'Adventure'">Action und Abenteuer</xsl:when>
<xsl:when test=".= 'Animation'">Zeichentrick</xsl:when>
<xsl:when test=".= 'Anime'">Zeichentrick</xsl:when>
<xsl:when test=".= 'Bollywood'">Bollywood</xsl:when>
<xsl:when test=".= 'Classics'">Drama > Klassiker</xsl:when>
<xsl:when test=".= 'Comedy'">Komödie</xsl:when>
<xsl:when test=".= 'Concert Film'">Musik</xsl:when>
<xsl:when test=".= 'Crime'">Kriminalfilm > Drama</xsl:when>
<xsl:when test=".= 'Drama'">Drama</xsl:when>
<xsl:when test=".= 'Fantasy'">Drama > Sci-Fi und Fantasy</xsl:when>
<xsl:when test=".= 'Foreign'">International</xsl:when>
<xsl:when test=".= 'Horror'">Kriminalfilm > Horror</xsl:when>
<xsl:when test=".= 'Independent'">Independentfilm & Arthouse</xsl:when>
<xsl:when test=".= 'Japanese Cinema'">International > Japan</xsl:when>
<xsl:when test=".= 'Jidaigeki'">International > Japan</xsl:when>
<xsl:when test=".= 'Kids & Family'">Kinderfilm > Familie</xsl:when>
<xsl:when test=".= 'Music Documentary'">Musik > Dokumentation</xsl:when>
<xsl:when test=".= 'Music Feature Film'">Musik</xsl:when>
<xsl:when test=".= 'Musicals'">Musik > Musical</xsl:when>
<xsl:when test=".= 'Mystery'">Drama > Mystery</xsl:when>
<xsl:when test=".= 'Nonfiction - Documentary'">Dokumentation</xsl:when>
<xsl:when test=".= 'Regional Indian'">International > Indien & Pakistan</xsl:when>
<xsl:when test=".= 'Romance'">Drama > Romanze</xsl:when>
<xsl:when test=".= 'Science Fiction'">Science Fiction und Fantasy</xsl:when>
<xsl:when test=".= 'Short Films'">Independentfilm & Arthouse > Experimentalfilm</xsl:when>
<xsl:when test=".= 'Special Interest'">Hobby</xsl:when>
<xsl:when test=".= 'Sports'">Sport</xsl:when>
<xsl:when test=".= 'Thrillers'">Thriller</xsl:when>
<xsl:when test=".= 'Tokusatsu'">International > Japan</xsl:when>
<xsl:when test=".= 'Urban'">Drama > Alltag</xsl:when>
<xsl:when test=".= 'Westerns'">Western</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</Genre>
</xsl:for-each>
<More values...>
</Metadata>
</xsl:template>
问题是当类型转换时我们以重复值结尾,例如当输入包含元素时
<genres>
<genre>Comedy</genre>
<genre>Adventure</genre>
<genre>Action</genre>
</genres>
转换后我们有
<Genre>Komödie</Genre>
<Genre>Action und Abenteuer</Genre>
<Genre>Action und Abenteuer</Genre>
我已经尝试为此寻找一些解决方案,但我还没有找到解决方案,我们将不胜感激。
编辑澄清:我需要的是从输出中消除重复的流派元素。这些元素不能彼此相邻,我们无法通过第二次转换运行输出,因为我们无法修改处理此问题的服务代码。
谢谢
【问题讨论】:
-
好吧,您至少需要准确地告诉我们您想要的结果。您想消除重复的
genre元素吗?您是否只想在它们相邻的情况下(例如在您的样本中)或即使它们分开?您是否考虑过链接两个样式表,您只需将当前样式表的输出运行一秒钟即可通过 Muenchian 分组 jenitennison.com/xslt/grouping/muenchian.xml 消除重复? -
理想情况下,重复的流派元素需要从列表中删除,它们并不总是彼此相邻。此外,它不可能通过第二个样式表运行输出。我将用这些说明更新问题。
-
您需要另一个包含
if条目值的 XML,因此您可以使用 XPath 检查preceding-sibling节点。