从节点中删除命名空间,然后使用xslt将该节点复制到新节点中



我有xml,它有名称空间,所以我想要

  1. 删除命名空间2( 在移除不同节点下的名称空间副本后,以下是我的代码

有人能告诉我如何在单个xsl中实现这两个功能吗。在上面的代码之后,我得到的只是删除的名称空间。我看过很多文章,但没有找到与之相关的东西。输入:

<UWInitial xmlns:d3p1="http:someUrl">
<d3p1:string>ABC</d3p1:string>
<d3p1:string>EFG</d3p1:string>
<d3p1:string>EHG</d3p1:string>
<d3p1:string>EFD</d3p1:string>
<d3p1:string>ESF</d3p1:string>
</UWInitial>
'''
output what i want :
'''
<UWInitial>
<NewNode>
<string>ABC</string>
</NewNode>
<NewNode>
<string>EFG</string>
</NewNode>
<NewNode>
<string>EHG</string>
</NewNode>
<NewNode>
<string>EFD</string>
</NewNode>
<NewNode>
<string>ESF</string>
</NewNode>
</UWInitial>
'''
XSLT that i use:
'''
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d3p1="someUrl">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
<xsl:template match="d3p1:string" >
<xsl:element name="NewNode">
<xsl:copy>                  
<xsl:copy-of select="@*|node()"/>
</xsl:copy>
</xsl:element>

</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
<xsl:call-template name="Test" />
</xsl:template>
<xsl:template match="@*" >
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="comment() | text() | processing-instruction()" >
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
'''

output which i get:
'''
<UWInitial>
<string>ABC</string>
<string>EFG</string>
<string>EHG</string>
<string>EFD</string>
<string>ESF</string>
</UWInitial>
'''

如果您想将任何字符串包装到NewNode中,请使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d3p1="http:someUrl"
exclude-result-prefixes="d3p1"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="d3p1:string">
<NewNode>
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</NewNode>
</xsl:template>
</xsl:stylesheet>

最新更新