Need XSLT for the mentioned scenario (refer questi

2019-09-21 06:52发布

Input XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="trans.xsl"?>
<rootecf>
<XBRL>
<Name>Akhil</Name>
</XBRL>
<PI20>
<Address>Villa</Address>
</PI20>
</rootecf>

Output XML:

<?xml version="1.0"?>
<rootecf>
<XBRL>
<Name>Akhil</Name>
<PI20Addres>Villa</PI20Addres>
</XBRL>

What I am trying to do here is that in the output XML i am creating a new tag called <PI20Addres> with the value of the PI20/Address tag. The important point here is that I do not want the XBRL tag to be hardoced in the output XML as i have hardcoded PI20Adress tag, but instead it should be read from the input XML and coped as it is.

The reason i am saying is that the XBRL tag will be having namespaces that will change randomly in the incoming XML, so I have to copy the tag rather than hardcoding it.

Can anyone please tell me how can I achieve this with XSL?

标签: xml xslt
2条回答
贪生不怕死
2楼-- · 2019-09-21 07:25

trans.xsl can look like this:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<rootecf>
  <XBRL>
    <Name><xsl:value-of select="/rootecf/XBRL/Name"/></Name>
    <PI20Address><xsl:value-of select="/rootecf/PI20/Address"/></PI20Address>
  </XBRL>
</rootecf>
</xsl:template>

</xsl:stylesheet>
查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-21 07:37

This will return the requested output when applied to the single example you have provided:

XSLT 1.0

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/rootecf">
    <rootecf>
        <xsl:element name="{local-name(*)}">
            <Name><xsl:value-of select="*/Name"/></Name>
            <PI20Addres><xsl:value-of select="*/Address"/></PI20Addres>
        </xsl:element>
    </rootecf>
</xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答