使用xslt创建列表
我有下面的XML使用xslt创建列表,xslt,Xslt,我有下面的XML <?xml version="1.0" encoding="UTF-8"?> <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <FirstName>John</FirstName> <LastName>Peter</LastName> <Initial>T</Initial> </E
<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<FirstName>John</FirstName>
<LastName>Peter</LastName>
<Initial>T</Initial>
</Employee>
约翰
彼得
T
在XSLT1.0中,我想编写一个XSLT,从上面的XML生成下面的XML。有人能帮我写这个xslt吗
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
名字
约翰
姓氏
彼得
首字母
T
遵循XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="no"
encoding="UTF-8" indent="yes" />
<xsl:template match="Employee">
<ArrayOfstringVariable>
<xsl:apply-templates select="*"/>
</ArrayOfstringVariable>
</xsl:template>
<xsl:template match="*">
<stringVariable>
<name>
<xsl:value-of select="local-name()"/>
</name>
<value>
<xsl:value-of select="."/>
</value>
</stringVariable>
</xsl:template>
</xsl:stylesheet>
当应用于问题中的示例输入XML时,将生成以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable>
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
名字
约翰
姓氏
彼得
首字母
T
如果您希望在输出XML中的
ArrayOfStringVariable
元素处具有名称空间,可以通过两个调整来实现:addxmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
到xsl:stylesheet
声明和调整您的第一个模板和/*
中的/
是多余的。@JLRishe感谢您的提及,已经删除了第二个冗余,并将向答案中添加关于第一个模板的信息,而不仅仅是删除它。用于从根开始,但此处不需要。为什么要匹配node()
而不是*
?@MathiasMüller感谢您的提醒。这两种方法都适用于提供的示例,但是*
可能更好。调整。如果有人注意到这些评论,想知道它们之间的区别,作为参考