0
我有一個XML文件:XSLT:使用地圖更改子元素
<?xml version="1.0" encoding="UTF-8"?>
<root>
<category id="Cat1" owner="Team1">
<entry id="Ent1" owner="John">
<title>This is Entry 1</title>
</entry>
<entry id="Ent2" owner="Matt">
<title>This is Entry 2</title>
</entry>
</category>
<category id="Cat2" owner="Team2">
<entry id="Ent3" owner="Arnold">
<title>This is Entry 3</title>
</entry>
<entry id="Ent4" owner="Jim">
<title>This is Entry 4</title>
</entry>
</category>
</root>
每個條目的元素,我想改變它的TITLE子元素的基礎上它的@id屬性的值。我創建了以下XSLT,它首先定義了一個映射...每個映射條目的關鍵字是要更改標題的元素的@id。每個地圖項的值就是我想要的標題元素(這是項的孩子)的值成爲:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- set up the map -->
<xsl:variable name="map">
<entry key="Ent1">Here is the first entry</entry>
<entry key="Ent2">Here is the second entry</entry>
<entry key="Ent3">Here is the third entry</entry>
<entry key="Ent4">Here is the fourth entry</entry>
</xsl:variable>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Problem area: use the key to set the attribute of the correct procedure -->
<xsl:template match="entry">
<title><xsl:value-of select="$map/entry[@key = current()/@id]"/></title>
</xsl:template>
</xsl:stylesheet>
從我的理解,這是一個兩個步驟:
- 執行恆等變換
- 創建一個新的模板,更改標題元素
但是,這創造出一個離奇的輸出,這已經取代了我的整個條目元素的標題的Elemen ts ...就好像所有的事情都被執行了1步太高。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<category id="Cat1">
<title>Here is the first entry</title>
<title>Here is the second entry</title>
<title>Here is the third entry</title>
</category>
<category id="Cat2">
<title>Here is the fourth entry</title>
<title>Here is the fifth entry</title>
<title>Here is the sixth entry</title>
</category>
</root>
我的地圖有問題嗎?我是否誤解了身份轉換後必須使用的新模板?
你能改造後的輸入示例的預期效果? –