XML資料:
<items>/span>
<item>/span>
<sku>123</sku>/span>
<name>abc</name>/span>
</item>/span>
<item>/span>
<sku>/span>345</sku>/span>
<name>/span>cde</name>
</item>/span>
</items>
目標輸出XML:
<rss xmlns:g="http://base. google.com/ns/1.0" version="2.0">。
<channel>
<title>Data Feed</title>
<link>/span>http://www.example.com</link>
<description>資料源的描述</description>
<item>/span>
<sku>123</sku>/span>
<name>abc</name>/span>
</item>/span>
<item>/span>
<sku>/span>345</sku>/span>
<name>/span>cde</name>
</item>/span>
</channel>/span>
</rss>
XSLT轉換:
<?xml version="1.0" encoding="UTF-8"? >
<xsl:styleheet version="1.0"/span> 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"/> indent="yes"
<xsl:template match=" items">/span>
<xsl:element name="rss"/span> xmlns:g="http://base. google.com/ns/1.0">
<xsl:attributee name="version">2. 0</xsl:attributee>。
<xsl:element name="channel">/span>
<xsl: element name="title">Data Feed</xsl:element>
<xsl:element name="link">/span>https://www. example.com</xsl: element>
<xsl: element name="description">資料源的描述</xsl:element>
<xsl:for-each select="item"/span>>
<xsl:element name="item">/span>
<sku><xsl: value-of select="sku"/></sku>
<name><xsl: value-of select="name"/></name>
</xsl:element>/span>
</xsl:for-each>
</xsl:element>/span>
</xsl:element>/span>
</xsl:template>
</xsl:styleheet>/span>
我需要在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>/span>http://www.example.com</link>
<description>資料源的描述</description>
<item>/span>
<sku>123</sku>/span>
<name>abc</name>/span>
</item>/span>
<item>/span>
<sku>/span>345</sku>/span>
<name>/span>cde</name>
</item>/span>
</channel>/span>
</rss>
歡迎任何幫助!
uj5u.com熱心網友回復:
使用<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>/span>http://www.example.com</link>
<description>資料源的描述</description>
<xsl:for-each select="item"/span>>
<xsl:copy>
<sku><xsl: value-of select="sku"/></sku>
<name><xsl: value-of select="name"/></name>
</xsl:copy>
</xsl:for-each>
</channel>
</rss>/span>
</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>/span>http://www.example.com</link>
<description>資料源的描述</description>
<xsl:copy-of select=" item"/>
</channel>/span>
</rss>/span>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/324038.html
標籤:
