1

I want to convert an XML into another XML using an XSLT stylesheet.

This is the part of my input XML where I have to use XSL template(input.xml):

 ...
    <extension>
        <og:image>http://www.example.com/images/logos/logo-example-PNG.png</og:image>
        <og:type>article</og:type>
        <resourceName>http://www.example.com/big-data-search-managed-services-questions</resourceName>
    </extension>
   .......

I want my XML(output.xml) to look like this:

....
<MT N="og:image" V="http://www.example.com/images/logos/logo-example-PNG.png"/>
<MT N="og:type" V="article"/>
<MT N="resourceName" V="http://www.example.com/big-data-search-managed-services-questions"/>
...

I am trying to use an XSLT for this. But I am stuck at the XSL template part. I want my XSLT to go in the XPATH of extension and specify a template for every element inside of the extension to convert into this form:

<MT N="" V=""/>

"N" with the name of the tag name and "V" with the value of the tag

What should I specify in my XSLT?

 <xsl:template match="/extension">
    ....
    </xsl:template>
1
  • Please post entire XML (or enough to know repeat pattern) as XSLT unlike its sibling XPath handles an entire document even if you only need to transform a part of it. Also, namespaces usually in root tags need too be known and seen. Commented Aug 19, 2016 at 0:05

1 Answer 1

1

Try this:

XML:

<extension xmlns:og="http://og.com">
 <og:image>http://www.example.com/images/logos/logo-example-PNG.png</og:image>
 <og:type>article</og:type>
 <resourceName>http://www.example.com/big-data-search-managed-services-questions</resourceName>
</extension>

XSLT:

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

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

<xsl:template match="extension">
    <xsl:for-each select="*">
        <xsl:element name="MT">
            <xsl:attribute name="N" select="name()"/>
            <xsl:attribute name="V" select="."/>
        </xsl:element>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

OutPut:

<MT N="og:image" V="http://www.example.com/images/logos/logo-example-PNG.png"/>
<MT N="og:type" V="article"/>
<MT N="resourceName" V="http://www.example.com/big-data-search-managed-services-questions"/>

Edited input XML:

<root  xmlns:og="http://og.com">
 <extension>
  <og:image>http://www.example.com/images/logos/logo-example-PNG.png</og:image>
  <og:type>article</og:type>
  <resourceName>http://www.example.com/big-data-search-managed-services-questions</resourceName>
 </extension>
</root>

Here 'root' is the root element, namespace can be declared there.

Sign up to request clarification or add additional context in comments.

9 Comments

More simply, you can replace the xsl:element above with <MT N="{name()}" V="{.}"/>
Thanks for the suggestion, I will update as suggested.
@MichaelKay Sir, <MT N="{name()}" V="{.}"/> will give the <MT xmlns:og="og.com" .../> og namespace text within MT element.
Thanks. I will try this and let you know
Thanks. This was really helpful
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.