希望找到上師的幫助來找出下一個問題。XSLT苟平,for-each和牽連循環。如何消除重複?
我有兩個xml文件。 Firts一個在這裏(text.xml):
<text>
<ref>Author1, Title1, Date1</ref>
<ref>Author75, Title75, Date2</ref>
<ref>Author2, Title2, Date2</ref>
<ref>Author3, Title3, Date3</ref>
<text>
,第二個這樣的(list.xml):
<list>
<bibl xml:id="1"><author>Author1</author><date>Date1</date></bibl>
<bibl xml:id="2"><author>Author2</author><date>Date2</date></bibl>
<bibl xml:id="3"><author>Author3</author><date>Date3</date></bibl>
</list>
我想查詢text.xml和檢查(從list.xml)id來<ref>
(從text.xml)WIC:對list.xml添加@xml h包含相同的作者和日期。如果不是,那麼只需複製原始<ref>
。
所以我想獲得:
<ref xml:id="1">Author1, Title1, Date1</ref>
<ref>Author75, Title75, Date2</ref>
<ref xml:id="2>Author2, Title2, Date2</ref>
etc.
我XSLT識別以及所有correpondence:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ref">
<xsl:variable name="ref" select="."/>
<xsl:for-each select="document('list.xml')//bibl">
<xsl:variable name="bibl" select="."/>
<xsl:variable name="author" select="author"/>
<xsl:variable name="date" select="date"/>
<xsl:choose>
<xsl:when test="contains($ref, $author) and contains($ref, $date)">
<ref>
<xsl:attribute name="xml:id">
<xsl:value-of select="$bibl/@xml:id"/>
</xsl:attribute>
<xsl:value-of select="$ref"/>
</ref>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$ref"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但是,再就是沒有correpondence它不只是複製權<ref>
,但複製全部<ref>
我在第二個文件中有<bibl>
個節點的時間數。
所以問題在<xsl:otherwise><xsl:copy-of select="$ref"/></xsl:otherwise>
。 任何想法如何才能獲得我需要的獨特價值?我知道它實際上必須非常簡單,我嘗試了key,generate-id,for-each-group,distinct-values,但無法弄清楚。
非常感謝你!它工作完美! 完全同意你的第二個解決方案更加優雅和高效。但實際上第一個XML文件(** text.xml **)它只是一個示例。真正的''這就像' J. Smith,Book title,Publisher,2001,p.211'或' Book title,eds。史密斯約翰,2005'等等。所以我現在不是真的可以在哪裏找到作者或日期,我只知道它在某處。 再次感謝您! – 2012-03-28 17:14:19