2013-09-25 63 views
2

在我的XML命名空間URI的命名空間http://abc.com/source/error,應該由http://abc.com/error/i1更換。我必須使用xslt1.0,這是非常具有挑戰性的單獨更換的URI的一部分。所有其他的應該是輸出。如果這個命名空間不存在,輸入xml應該按原樣輸出。XSLT替換單獨

我輸入XML

<a xmlns:hj="http://abc.com/source/error"> 
<hj:b>sam</hj:b> 
</a> 

預期輸出

<a xmlns:hj="http://abc.com/source/error/i1"> 
<hj:b>sam</hj:b> 
</a> 

回答

0

這XSLT將做的工作。但要注意它只能在輸入的XML命名空間前綴等於hj:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hjold="http://abc.com/source/error" xmlns:hj="http://abc.com/source/error/i1" exclude-result-prefixes="hjold"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="a[namespace-uri-for-prefix('hj', /*) != '']"> 
     <a xmlns:hj="http://abc.com/source/error/i1"> 
      <xsl:apply-templates select="@*|node()" /> 
     </a> 
    </xsl:template> 

    <xsl:template match="hjold:*" > 
     <xsl:element name="hj:{local-name()}"> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="*" > 
     <xsl:element name="{local-name()}"> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

要在XSLT 1.0使用和命名空間,即使沒有命名空間的輸出並不重要宣稱,變化:

<xsl:template match="a[namespace-uri-for-prefix('hj', /*) != '']"> 

<xsl:template match="a"> 
+0

感謝。名稱空間前綴對於xslt應該沒有關係。即使是XML, sam 應working..also的XSLT版本是1.0 – Suresh

+0

對不起,但如果你想在XSLT 1.0使用它,如果它是在結果與否,改變'的'到'的' –

+0

標記,輸入的XML可以是任何。即元素可以是任何數字。只有名稱空間應該被替換。 – Suresh