XML XSLT 转换:将带前缀的命名空间添加到 rss 元素

XML XSLT Transformation: Add Namespace with prefix to rss element

XML数据:

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

目标输出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 转换:

<?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>

我需要在 XSLT 转换中进行哪些调整才能将带前缀的命名空间放入 rss 元素中?

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

在上面的 XSLT 转换中,输出缺少 rss 元素中的 xmlns:g 命名空间:

<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>

感谢任何帮助!

字面上使用 <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> 而不是所有那些不必要的 xsl:element 使用。

所以整个模板就是

<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>

甚至

<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>