几天前,我发现了一个与我类似的问题,并有一个可能的答案,但我找不到了。
我成功地从Access输出了所需的XML,但有一个例外。
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2019- 02-03T16:38:41">
<CD-Export>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.9</price>
<CD-Years-Export>
<title>Empire Burlesque</title>
<year>1985</year>
</CD-Years-Export>
<CD-Years-Export>
<title>Empire Burlesque</title>
<year>1986</year>
</CD-Years-Export>
<CD-Years-Export>
<title>Empire Burlesque</title>
<year>1987</year>
</CD-Years-Export>
</CD-Export>
我相信我应该使用XSLT文件删除外键
<title>
from the <CD-Years-Export> segment
有人可以提供XSLT转换示例,该转换可用于 从子节点中删除外键?
答案 0 :(得分:0)
考虑以下XSLT脚本的任意:
空模板
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- EMPTY TEMPLATE TO REMOVE NODE -->
<xsl:template match="CD-Years-Export/title"/>
</xsl:stylesheet>
排除节点
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CD-Years-Export">
<xsl:copy>
<!-- EXCLUDE title NODE IN TREE REWRITE -->
<xsl:apply-templates select="year"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
重写父节点
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- WALK DOWN TREE FROM ROOT -->
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="CD-Export"/>
</xsl:copy>
</xsl:template>
<!-- COPY ALL BUT PARENT -->
<xsl:template match="CD-Export">
<xsl:copy>
<xsl:copy-of select="*[name()!='CD-Years-Export']"/>
<xsl:apply-templates select="CD-Years-Export"/>
</xsl:copy>
</xsl:template>
<!-- RE-WRITE PARENT NODE -->
<xsl:template match="CD-Years-Export">
<xsl:copy>
<xsl:copy-of select="year"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Muenchian Method (按不同标题分组)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="title_key" match="CD-Years-Export" use="title"/>
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="CD-Export"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CD-Export">
<xsl:copy>
<xsl:copy-of select="*[name()!='CD-Years-Export']"/>
<!-- ITERATE THROUGH EACH DISTINCT title -->
<xsl:for-each select="CD-Years-Export[generate-id() =
generate-id(key('title_key', title)[1])]">
<!-- RE-WRITE NODE FOR EACH DISTINCT title -->
<xsl:for-each select="key('title_key', title)">
<xsl:copy>
<xsl:copy-of select="year"/>
</xsl:copy>
</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Muenchian方法 (不使用模式模板使用xsl:for-each
)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="title_key" match="CD-Years-Export" use="title"/>
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="CD-Export"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CD-Export">
<xsl:copy>
<xsl:copy-of select="*[name()!='CD-Years-Export']"/>
<!-- ITERATE THROUGH EACH DISTINCT title -->
<xsl:apply-templates select="CD-Years-Export[generate-id() =
generate-id(key('title_key', title)[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CD-Years-Export">
<!-- RE-WRITE NODE FOR EACH DISTINCT title -->
<xsl:apply-templates select="key('title_key', title)" mode="group"/>
</xsl:template>
<xsl:template match="CD-Years-Export" mode="group">
<xsl:copy>
<!-- RE-WRITE NODE FOR EACH DISTINCT title -->
<xsl:copy-of select="year"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>