我有一个xml的以下模式
<?xml version="1.0" encoding="UTF-8"?> <Person> <FirstName>Ahmed</FirstName> <MiddleName/> <LastName>Aboulnaga</LastName> <CompanyInfo> <CompanyName>IPN Web</CompanyName> <Title/> <Role></Role> <Department> </Department> </CompanyInfo> </Person>
<xsl:template match="@*|node()"> <xsl:if test=". != '' or ./@* != ''"> <xsl:copy> <xsl:copy-of select = "@*"/> <xsl:apply-templates /> </xsl:copy> </xsl:if>
<Title/> <Role></Role>
…但是当空标签位于两行时失败,例如:
<Department> </Department>
有没有解决这个问题?
此转换根本不需要任何条件XSLT指令,并且不使用明确的优先级:
<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= "*[not(@*|*|comment()|processing-instruction()) and normalize-space()='' ]"/> </xsl:stylesheet>
当应用于提供的XML文档时:
<Person> <FirstName>Ahmed</FirstName> <MiddleName/> <LastName>Aboulnaga</LastName> <CompanyInfo> <CompanyName>IPN Web</CompanyName> <Title/> <Role></Role> <Department> </Department> </CompanyInfo> </Person>
它产生想要的,正确的结果:
<Person> <FirstName>Ahmed</FirstName> <LastName>Aboulnaga</LastName> <CompanyInfo> <CompanyName>IPN Web</CompanyName> </CompanyInfo> </Person>