Home > Software engineering >  XML XSLT Transformation: Add Namespace with prefix to rss element
XML XSLT Transformation: Add Namespace with prefix to rss element

Time:09-21

XML Data:

<items>
  <item>
    <sku>123</sku>
    <name>abc</name>
  </item>
  <item>
    <sku>345</sku>
    <name>cde</name>
  </item>
</items>

Target Output XML:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
  <title>Data Feed</title>
  <link>http://www.example.com</link>
  <description>Description of data feed</description>
  <item>
    <sku>123</sku>
    <name>abc</name>
  </item>
  <item>
    <sku>345</sku>
    <name>cde</name>
  </item>
</channel>
</rss>

XSLT Transformation:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="items">
  <xsl:element name="rss" xmlns:g="http://base.google.com/ns/1.0">
    <xsl:attribute name="version">2.0</xsl:attribute>
      <xsl:element name="channel">
        <xsl:element name="title">Data Feed</xsl:element>
        <xsl:element name="link">https://www.example.com</xsl:element>
        <xsl:element name="description">Description of data feed</xsl:element>
        <xsl:for-each select="item">
          <xsl:element name="item">
            <sku><xsl:value-of select="sku"/></sku>
            <name><xsl:value-of select="name"/></name>
          </xsl:element>
        </xsl:for-each>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

What do I need to adjust in the XSLT transformation to get the namespace with prefix into the rss element?

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">

With the XSLT transfomration above the output misses the xmlns:g namespace in the rss element:

<rss version="2.0">
<channel>
  <title>Data Feed</title>
  <link>http://www.example.com</link>
  <description>Description of data feed</description>
  <item>
    <sku>123</sku>
    <name>abc</name>
  </item>
  <item>
    <sku>345</sku>
    <name>cde</name>
  </item>
</channel>
</rss>

Any help is appreciated!

CodePudding user response:

Use <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> literally instead of all those unnecessary xsl:element uses.

So the whole template would be simply

<xsl:template match="items">
  <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
    <channel>
      <title>Data Feed</title>
      <link>http://www.example.com</link>
      <description>Description of data feed</description>
      <xsl:for-each select="item">
          <xsl:copy>
            <sku><xsl:value-of select="sku"/></sku>
            <name><xsl:value-of select="name"/></name>
          </xsl:copy>
       </xsl:for-each>
      </channel>
    </rss>
</xsl:template>

or even

<xsl:template match="items">
  <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
    <channel>
      <title>Data Feed</title>
      <link>http://www.example.com</link>
      <description>Description of data feed</description>
      <xsl:copy-of select="item"/>
      </channel>
    </rss>
</xsl:template>
  • Related