xml – xslt 1.0合并空元素名称
关于使用xslt 1.0的一个快速问题,你可以帮助我.我有一个输入xml,如下所示
<Root> <FirstName>Bob</FirstName> <LastName>Marley</LastName> <ID>BM1234</ID> <Songs> <Song> <EmptyElements></EmptyElements> <SongName>No woman no cry</SongName> <Year>1974</Year> <album></album> <studio></studio> <rating></rating> </Song> </Songs> </Root> 输出需要看起来像 <Root> <FirstName>Bob</FirstName> <LastName>Marley</LastName> <ID>BM1234</ID> <Songs> <Song> <EmptyElements>album,studio,rating</EmptyElements> <SongName>No woman no cry</SongName> <Year>1974</Year> </Song> </Songs> </Root> 所以基本上将所有空元素的逗号分隔列表放入EmptyElements标记中. 解决方法
这种转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="EmptyElements" priority="5"> <xsl:copy> <xsl:apply-templates mode="enumerate" select= "../*[not(self::EmptyElements) and not(node())]" /> </xsl:copy> </xsl:template> <xsl:template match="Songs/Song/*" mode="enumerate"> <xsl:value-of select="substring(',',not(position() = 1),1)"/> <xsl:value-of select="name()"/> </xsl:template> <xsl:template match="Songs/Song/*[not(node())]"/> </xsl:stylesheet> 应用于提供的源XML文档时: <Root> <FirstName>Bob</FirstName> <LastName>Marley</LastName> <ID>BM1234</ID> <Songs> <Song> <EmptyElements></EmptyElements> <SongName>No woman no cry</SongName> <Year>1974</Year> <album></album> <studio></studio> <rating></rating> </Song> </Songs> </Root> 产生想要的,正确的结果: <Root> <FirstName>Bob</FirstName> <LastName>Marley</LastName> <ID>BM1234</ID> <Songs> <Song> <EmptyElements>album,rating</EmptyElements> <SongName>No woman no cry</SongName> <Year>1974</Year> </Song> </Songs> </Root> 说明: >选择执行时,标识规则将“按原样”复制匹配的节点 更新: 亲爱的读者, 而不是告诉你这个答案比简单地简单:-answer,我总结了一些与简单相关的事实,你可以自己做出结论.在下表中,每个左子列中的值都是针对此的当前解决方案.每个右侧子列中的值仅适用于Or:-solution: 除此之外,简单地说: – 解决方案也具有潜在的性能和一定的可流动性问题 – 请参阅此片段: <xsl:if test="position()!=last()"> <xsl:text>,</xsl:text> </xsl:if> 与当前解决方案使用的内容相比: not(position() = 1) 见Dr. Michael Kay’s recommendation,后者是“比这更好的编码方式”,他解释了为什么:
结论:每当有人告诉我们:“或者简单地说:”,在将他们的陈述视为理所当然之前采取一些指标是很好的… (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |