-1
請建議訪問沒有任何名稱空間的元素。但是,我的代碼能夠訪問和更改具有名稱空間的節點(元素)。我正在使用XSLT2版本。查找我的XML(我用映射到我的本地路徑DTD路徑,請還建議爲訪問XML沒有DTD幫助XSLT:如何在同一篇文章中訪問既有名稱空間又沒有名稱空間的元素
輸入XML:
<!DOCTYPE article PUBLIC "-//ES//DTD journal article DTD version 5.2.0//EN//XML" "D:/DTDs/Els-parser/art520.dtd">
<article>
<fm>
<ce:title>The title</ce:title>
<ce:author-group>
<ce:author><ce:surname>Rudramuni</ce:surname><ce:given-names>TP</ce:given-names></ce:author>
</ce:author-group>
</fm>
<body>
<ce:sections>
<ce:section>
<ce:section-title>The first Head</ce:section-title>
<ce:para>Tha first para</ce:para>
</ce:section>
</ce:sections>
</body>
<back>
<ref><ce:author>Vijay</ce:author></ref>
</back>
</article>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ce="http://www.elsevier.com/xml/common/dtd"
xmlns:sb="http://www.elsevier.com/xml/common/struct-bib/dtd"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
version='2.0'>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fm">
<xsl:element name="ce:front"><xsl:apply-templates/></xsl:element>
</xsl:template>
<xsl:template match="ce:author">
<xsl:element name="name"><xsl:apply-templates/></xsl:element>
</xsl:template>
</xsl:stylesheet>
要求的結果:
<?xml version="1.0" encoding="UTF-8"?>
<article>
<ce:front>
<ce:title>The title</ce:title>
<ce:author-group><name><ce:surname>Rudramuni</ce:surname><ce:given-names>TP</ce:given-names></name></ce:author-group>
</ce:front>
<body><ce:sections><ce:section><ce:section-title>The first Head</ce:section-title><ce:para>Tha first para</ce:para></ce:section></ce:sections></body>
<back>
<ref><name>Vijay</name></ref>
</back>
</article>
但我得到一些額外的命名空間,如 「的xmlns =」 http://www.elsevier.com/xml/ja/dtd」和xmlns =「」,併爲view =「all」等元素找到了一些額外的屬性。提前致謝。請建議。
非常感謝主席先生,你的建議。我會盡力遵守你的指導方針。 –
使用您的代碼,現在我可以訪問命名空間和命名空間元素。感謝您的簡單代碼,並獲得完美結果。還有一個小小的疑問,是問更多的問題是不正確的? –
@RudramuniTP:你可以提出更多的問題,這很好,只要你問它作爲單獨的問題(我的意思是,不要把多個問題放在一個問題上)。因此,繼續詢問所需的所有XSLT相關問題;)。也請閱讀(需要5分鐘):[問]。 – Abel