我有以下xslt。我注意到有時元素名稱「元組」有一個屬性。我想刪除該屬性並將其添加爲元素。我添加了測試來驗證'元組'是否有一個屬性,但它返回一個空白'ecatalogue'元素。xslt測試是否存在屬性
<?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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="atom">
<xsl:element name="{@name}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="table">
<xsl:element name="{@name}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!--this test doesn't work properly -->
<xsl:template match="tuple">
<xsl:choose>
<xsl:when test="@name">
<xsl:apply-templates />
</xsl:when>
<xsl:otherwise>
<!-- nothing to do
the node should stay the same
-->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- end test -->
</xsl:stylesheet>
結果我用上面的這個模板。
<ecatalogue>
</ecatalogue>
https://gist.github.com/guinslym/5ce47460a31fe4c4046b
如果您提供了示例源XML文件以及您希望將其轉換爲什麼,這將會很有幫助。 – Mitch 2015-02-07 23:44:53
感謝米奇!我的要點鏈接沒有工作... – 2015-02-07 23:48:36
@PapoucheGuinslyzinho將屬性始終是'@名稱',不能有另一個名字? – 2015-02-08 03:24:54