我想向元素添加默認名稱空間(轉換時)。我用xslt進行這個轉換。請查找下面的代碼片段。默認名稱空間不能正確轉換
<xsl:variable name="ns" select="'http://ABC.org/Standards/SChool/2'" />
<xsl:element name="School" namespace="{$ns}">
<xsl:attribute name="Version">2.13.92</xsl:attribute>
<xsl:apply-templates select="ltc:UserAuthRequest" />
</xsl:element>
<xsl:template match="ltc:UserAuthRequest">
<xsl:element name="{local-name()}">
</xsl:template>
轉換後,我得到了波紋管xml輸出。
<School xmlns="http://ABC.org/Standards/SChool/2" Version="2.13.92">
<UserAuthRequest xmlns=""></UserAuthRequest>
</School>
這裏的問題是默認命名空間也是在子節點中創建的。
任何人都可以解釋爲什麼名稱空間是在子元素中創建的,即使我沒有爲它定義任何名稱空間以及如何避免此問題?
輸入XML
<?xml version="1.0" encoding="UTF-8"?>
<School>
<UserAuth>
<UserName>K.Senthuran</UserName>
<Password>Javaworld</Password>
</UserAuth>
</School>
轉換邏輯
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/School">
<xsl:variable name="ns">www.school.com/myLife</xsl:variable>
<xsl:element name="School" namespace="{$ns}">
<xsl:apply-templates select="UserAuth" />
</xsl:element>
</xsl:template>
<xsl:template match="UserAuth">
<xsl:element name="UserAuth">
<xsl:attribute name="id">sdmnsdnk</xsl:attribute>
<xsl:apply-templates select="UserName" />
<xsl:apply-templates select="Password" />
</xsl:element>
</xsl:template>
<xsl:template match="UserName">
<xsl:element name="{local-name()}">
<xsl:attribute name="userIDREF">dmsdmsl</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="Password">
<xsl:element name="{local-name()}">
<xsl:attribute name="uniqueIDREF">asakjhs</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
電流輸出
<?xml version="1.0" encoding="UTF-8"?>
<School xmlns="www.school.com/myLife">
<UserAuth xmlns="" id="sdmnsdnk">
<UserName userIDREF="dmsdmsl">K.Senthuran</UserName>
<Password uniqueIDREF="asakjhs">Javaworld</Password>
</UserAuth>
</School>
期望輸出
<?xml version="1.0" encoding="UTF-8"?>
<School xmlns="www.school.com/myLife">
<UserAuth id="sdmnsdnk">
<UserName userIDREF="dmsdmsl">K.Senthuran</UserName>
<Password uniqueIDREF="asakjhs">Javaworld</Password>
</UserAuth>
</School>
請提供實際的XML文檔和確切的通緝令 轉型。 –