我有一個結構有點像這樣的XML文檔: -XSL轉換隻能元素一定的命名空間
<catalog xmlns="format_old" xmlns:final="format_new">
<final:book>
<final:title>blah</final:title>
<final:author>more blah</final:author>
</final:book>
<book>
<description title="blah2"/>
<writer name="more blah2"/>
</book>
</catalog>
顯然,這是問題的一個簡化版本。我想要做的是將其轉換成類似: -
<catalog xmlns="format_new">
<book>
<title>blah</title>
<author>more blah</author>
</book>
<book>
<title>blah2</title>
<author>more blah2</author>
</book>
</catalog>
,我現在所擁有的樣式表是這樣的: -
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:orig="format_old"
xmlns="format_new"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//orig:book">
<xsl:element name="title">
<xsl:value-of select="./orig:description/@title" />
</xsl:element>
<xsl:element name="author">
<xsl:value-of select="./orig:writer/@name" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
這給了我一個輸出,如: -
<catalog xmlns="format_old">
<book xmlns="format_new">
<title>blah</title>
<author>more blah</author>
</book>
<book xmlns:orig="format_old" xmlns="format_new">
<title>blah2</title>
</author>more blah2</author>
</book>
</catalog>
有兩個問題與這個樣式表: -
1)(重大問題)根的Elemen t被複制而不是更改根元素的默認名稱空間。所以基本上,目錄元素仍然位於名稱空間format_old中。
2)(小問題)這將元素轉換爲: -
<book xmlns:orig="format_old" xmlns="format_new">
...
</book>
,而不是從根元素拿起命名爲保持它作爲
<book>
...
</book>
我是什麼在這裏失蹤?我正在使用Xalan-C。
謝謝。在我的實際場景中試試這個... – owagh