Rename the root node in xml using xslt

南笙酒味 提交于 2021-01-28 09:08:16

问题


I am new to xslt and was trying to find a solution to my problem below. I have a xml, and using xslt I need to do the following

  1. just rename the root node to something else (without copying the entire structure of root node into another tag)
  2. Get rid of the namespaces information in nodes/elements/attributes

Below is my XML

<lbl:ABCD>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:lbl="http://abc.xyz.com/abc/xsd/sdf/aaaa" 
    xsi:schemaLocation="http://abc.ayx.com/sdf/xsd/label/efr efr.xsd">
    <lbl:Title>title</lbl:Title>
    <lbl:date>01/Aug/2018</lbl:date>
    <lbl:id>12345</lbl:id>
    <lbl:location>0362</lbl:location>
    <lbl:Status>aaaa</lbl:status>
    <lbl:hashnum>11801792113759009</lbl:hashnum>
</lbl:ABCD>

The output I want to achieve is

<shp>
        <Title>title</Title>
        <date>01/Aug/2018</date>
        <id>12345</id>
        <location>0362</location>
        <Status>aaaa</status>
        <hashnum>11801792113759009</hashnum>
    </shp

>

But so far I have been able to manage below (which is not how I need it to be)

<?xml version="1.0" encoding="utf-16"?>
<shp xmlns:lbl="http://abc.xyz.com/abc/xsd/sdf/aaaa">
    <lbl:ICNLabel 
        xsi:schemaLocation="http://abc.ayx.com/sdf/xsd/label/efr efr.xsd"        
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <lbl:Title>title</lbl:Title>
        <lbl:date>01/Aug/2018</lbl:date>
        <lbl:id>12345</lbl:id>
        <lbl:location>0362</lbl:location>
        <lbl:Status>aaaa</lbl:Status>
        <lbl:hashnum>11801792113759009</lbl:hashnum>
    </lbl:ICNLabel>
</shp>

Can someone guide me to resolve this?


回答1:


XML: (namespaces needs to address at root element)

<lbl:ABCD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lbl="http://abc.xyz.com/abc/xsd/sdf/aaaa" 
xsi:schemaLocation="http://abc.ayx.com/sdf/xsd/label/efr efr.xsd">
 <lbl:Title>title</lbl:Title>
 <lbl:date>01/Aug/2018</lbl:date>
 <lbl:id>12345</lbl:id>
 <lbl:location>0362</lbl:location>
 <lbl:Status>aaaa</lbl:Status>
 <lbl:hashnum>11801792113759009</lbl:hashnum>
</lbl:ABCD>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lbl="http://abc.xyz.com/abc/xsd/sdf/aaaa">

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

<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="comment()">
<xsl:comment>
    <xsl:value-of select="."/>
</xsl:comment>
</xsl:template>

<xsl:template match="/*">
    <xsl:element name="shp">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

OutPut:

<?xml version="1.0" encoding="UTF-8"?><shp>
<Title>title</Title>
<date>01/Aug/2018</date>
<id>12345</id>
<location>0362</location>
<Status>aaaa</Status>
<hashnum>11801792113759009</hashnum>
</shp>


来源:https://stackoverflow.com/questions/59043764/rename-the-root-node-in-xml-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!