Xml 在处理/评估每个模板后应用常规模板
我有一个XSLT:Xml 在处理/评估每个模板后应用常规模板,xml,xslt,Xml,Xslt,我有一个XSLT: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="no" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="Person"> <xsl:a
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Person">
<xsl:apply-templates select ="/Person/FirstName"/>
<xsl:apply-templates select ="/Person/MiddleName"/>
<xsl:apply-templates select ="/Person/LastName"/>
<xsl:apply-templates select ="/Person/CompanyInfo/CompanyName"/>
</xsl:template>
<xsl:template match="Person/FirstName">
<FirstName>
<xsl:value-of select="." />
</FirstName>
</xsl:template>
<xsl:template match="Person/MiddleName">
<MiddleName>
<xsl:value-of select="." />
</MiddleName>
</xsl:template>
<xsl:template match="Person/LastName">
<LastName>
<xsl:value-of select="." />
</LastName>
</xsl:template>
<xsl:template match ="Person/CompanyInfo/CompanyName">
<CompanyInfo>
<CompanyName>
<xsl:value-of select="." />
</CompanyName>
</CompanyInfo>
</xsl:template>
<xsl:template match="/">
<xsl:if test="normalize-space(.) != '' or ./@* != ''">
<xsl:copy>
<xsl:copy-of select = "@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
然后我有一个示例XML:
<?xml version="1.0" encoding="utf-8"?>
<Information>
<Person>
<FirstName>Person 1</FirstName>
<MiddleName/>
<LastName>Last 1</LastName>
<CompanyInfo>
<CompanyName></CompanyName>
</CompanyInfo>
</Person>
<Person>
<FirstName>Person 2</FirstName>
<MiddleName>Mid 2<MiddleName/>
<LastName>Last 2</LastName>
<CompanyInfo>
<CompanyName>CompName 2</CompanyName>
</CompanyInfo>
</Person>
</Information>
等等
我需要这样的输出:
<Person>
<FirstName>Person 1</FirstName>
<LastName>Last 1</LastName>
</Person>
<Person>
<FirstName>Person 2</FirstName>
<MiddleName>Mid 2<MiddleName/>
<LastName>Last 2</LastName>
<CompanyInfo>
<CompanyName>CompName 2</CompanyName>
</CompanyInfo>
</Person>
但是我认为我的xslt并没有给我想要的输出,我需要的是处理每个特定的模板,然后还要处理我在这里得到的最后一个标识模板,请提供任何帮助:
我在这里试图做的是,如果里面的所有内容都是空的,则忽略根目录下的空标记,但是我已经创建了几个模板,除了空标记之外,它们都可以执行我想要输出的操作。我将从标识转换模板开始,然后添加一个模板,以防止复制您不想要的元素:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(node()[normalize-space()])]"/>
</xsl:stylesheet>
您好,它仍然显示空标记,顺便说一句,在一个XML文件中可能有多个Person实例,这就是模板的原因。如果实际输入比您发布的示例更复杂,那么是时候向我们展示一个具有代表性的示例了。我已经编辑了我的帖子,基本上,如果Person数据为空,我希望在Person级别上省略空标记,因此如果可能,我希望保留每个字段的模板,并在转换结束时处理/添加一个模板以省略空字段:@zyberjock,抱歉,但现在您有两个顶级Person元素,这甚至不是格式良好的XML,因为需要一个包含所有其他元素的根元素。嗨,Martin,很抱歉,我再次编辑了我的帖子:
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<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(node()[normalize-space()])]"/>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<Information>
<Person>
<FirstName>Person 1</FirstName>
<MiddleName/>
<LastName>Last 1</LastName>
<CompanyInfo>
<CompanyName></CompanyName>
</CompanyInfo>
</Person>
<Person>
<FirstName>Person 2</FirstName>
<MiddleName>Mid 2</MiddleName>
<LastName>Last 2</LastName>
<CompanyInfo>
<CompanyName>CompName 2</CompanyName>
</CompanyInfo>
</Person>
</Information>
<Information>
<Person>
<FirstName>Person 1</FirstName>
<LastName>Last 1</LastName>
</Person>
<Person>
<FirstName>Person 2</FirstName>
<MiddleName>Mid 2</MiddleName>
<LastName>Last 2</LastName>
<CompanyInfo>
<CompanyName>CompName 2</CompanyName>
</CompanyInfo>
</Person>
</Information>
<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="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[not(node()[normalize-space()])]"/>
</xsl:stylesheet>
<Person>
<FirstName>Person 1</FirstName>
<LastName>Last 1</LastName>
</Person>
<Person>
<FirstName>Person 2</FirstName>
<MiddleName>Mid 2</MiddleName>
<LastName>Last 2</LastName>
<CompanyInfo>
<CompanyName>CompName 2</CompanyName>
</CompanyInfo>
</Person>