我正在使用XSLT和XML。如何使用XSLT從兩個XML獲得所需的XML輸出
第一件事我要在兩個XML上工作。
首先XML:
<?xml version="1.0"?>
<tcm:ListItems xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ID="tcm:232-83752-2" Managed="10682">
<tcm:Item ID="tcm:232-564598" Title="010 News Mapping"/>
<tcm:Item ID="tcm:232-564599" Title="020 CUGOs"/>
<tcm:Item ID="tcm:232-614307" Title="030 Reserved Urls"/>
</tcm:ListItems>
的第二XML我們將使用上述ID即中醫得到它:232-564598等,下面是XML的ID中醫之一:232-564598和其他ID將具有相同類型的XML。
<tcm:Component ID="tcm:229-564598" IsEditable="false" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<tcm:Data>
<tcm:Content>
<MappingCollection xmlns="uuid:922EEC29-2DE3-4BA1-A46A-A300CB8FA85F">
<VanityUrl>
<old>mbp</old>
<new>/SessionHandler.aspx?pageurl=/BP.aspx&pub=/english&section=IBE&j=f</new>
<dateAdded>2010-05-03T14:45:00</dateAdded>
<comments> News mapping </comments>
</VanityUrl>
<VanityUrl>
<old>about/news</old>
<new>about/news/news.aspx</new>
<dateAdded>2010-05-03T14:45:00</dateAdded>
<comments> News mapping </comments>
</VanityUrl>
</MappingCollection>
</tcm:Content>
</tcm:Data>
</tcm:Component>
我試圖使用上面的兩種XML獲得格式的XML格式。
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<!-- News mapping -->
<mapping old="mbp" new="/SessionHandler.aspx?pageurl=/BP.aspx&pub=/english&section=IBE&j=f"/>
<mapping old="about/news" new="about/news/news.aspx"/>
<!-- CUGO's-->
<mapping old="/nhs" new="/cugo.aspx?promoCode=UKNHS01&pub=/uk/english"/>
<mapping old="/hk/ukstudentfare" new="/cugo.aspx?promoCode=HKSTU10&pub=/hk/Chinese"/>
</mappings>
這裏是我的XSLT,我試圖生成上述格式的XML,但它不適合我。請注意第一個XML是主XML將使用以下XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:em="http://www.espire.com/tridion/schemas" xmlns:tcmse="http://www.tridion.com/ContentManager/5.1/TcmScriptAssistant" exclude-result-prefixes="em xlink tcmse tcm">
<xsl:output method="xml" version="1.0" encoding="UTF-16" indent="yes"/>
<!-- root match-->
<xsl:template match="tcm:ListItems">
<mappings>
<xsl:apply-templates select="tcm:Item"/>
</mappings>
</xsl:template>
<xsl:template match="tcm:Item">
<xsl:variable name="doc" select="document(@ID)"/>
<xsl:if test="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:comments">
<xsl:comment>
<xsl:value-of select="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:comments"/>
</xsl:comment>
</xsl:if>
<xsl:for-each select="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl">
<xsl:element name="mapping">
<xsl:if test="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:old">
<xsl:attribute name="old">
<xsl:value-of select="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:old"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:new">
<xsl:attribute name="new">
<xsl:value-of select="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:new"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:dateAdded">
<xsl:attribute name="dateAdded">
<xsl:value-of select="$doc/tcm:Component/tcm:Data/tcm:Content/em:MappingCollection/em:VanityUrl/em:dateAdded"/>
</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在上面的XSLT我能夠獲得數據環也要去正確的,但它已經到來的數據是相同的轉化,我的意思是環路表現正確,但節點值相同
請推薦!
許多感謝亞歷杭德羅,它獨立完美地與第二個XML的工作,你可以看看我添加的代碼,並建議如何將它與您的XSLT合併。 – 2010-11-03 14:00:52
@MKS:第一個XML提到的所有文檔都只是一種類型(即所有'VanityUrl'都有'comments ='新聞映射'')? – 2010-11-03 15:00:37
謝謝,但對不起,我不明白你的觀點,你能否詳細說明一下,是的格式將是相同的,但可以有許多評論,如「新映射」,「Cugos」等。如果你看到第一個XML每個節點的文檔將被加載的屬性ID xml進來然後上面xslt代碼將被執行 –
2010-11-03 16:03:01