通过XSLT将XML转换为另一个XML
我有一个如下所示的XML文件,我想把它转换成另一个XML文件通过XSLT将XML转换为另一个XML,xml,xslt,Xml,Xslt,我有一个如下所示的XML文件,我想把它转换成另一个XML文件 <body> <outline text="A"> <outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" i
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<opml version="1.0">
<head>
<title>tmp</title>
<expansionState></expansionState>
</head>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="H
下面是我想转换成的XML格式
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine
Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
<year />
</book>
阿贝尔森,哈罗德
哈罗德·阿贝尔森
计算机编程人员的结构和解释。埃内
艾因富隆信息
斯普林格·维拉格
3540520430
1991
亚伯拉罕,保罗W。
保罗·W·亚伯拉罕
不耐烦的人
艾迪生卫斯理酒吧公司
0201513757
2000
巴赫,弗雷德
弗雷德巴赫
UNIX Handbuch zur程序Wicklung
汉瑟·法赫布什维拉格
3446151036
这是我的密码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="body">
<list> <xsl:apply-templates select="outline"/> </list>
</xsl:template>
<xsl:template match="outline">
<books text= "{@text}">
<book><xsl:apply-templates select="outline"/>
<text><xsl:value-of select="@text" /></text>
<author><xsl:value-of select="@author" /></author>
<title><xsl:value-of select="@title" /></title>
<publisher><xsl:value-of select="@publisher" /></publisher>
<isbn><xsl:value-of select="@isbn" /></isbn>
<year><xsl:value-of select="@year" /></year>
</book>
</books>
</xsl:template>
</xsl:stylesheet>
这是我的代码的输出
<list>
<books text="A">
<book>
<books text="Abelson, Harold">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine
Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
</books>
阿贝尔森,哈罗德
哈罗德·阿贝尔森
计算机编程人员的结构和解释。埃内
艾因富隆信息
斯普林格·维拉格
3540520430
1991
我的输出中有两个额外的元素
<books text="Abelson, Harold">
<book>
据我所知,这可能是由这行代码引起的。我尝试了几种不同的方法,但没有成功
<xsl:template match="outline">
<books text= "{@text}">
补充问题:
如果原始XML文件包含标题。如何消除标题和标题。我当前的代码在新的XML文件中生成“tmp”
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<opml version="1.0">
<head>
<title>tmp</title>
<expansionState></expansionState>
</head>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="H
tmp
您的做法是正确的,但您需要两个大纲
模板-一个用于顶层大纲
模板,另一个用于子级大纲
请将您的大纲
模板替换为以下三个:
<xsl:template match="head" />
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates select="outline" />
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book>
<text>
<xsl:value-of select="@text" />
</text>
<author>
<xsl:value-of select="@author" />
</author>
<title>
<xsl:value-of select="@title" />
</title>
<publisher>
<xsl:value-of select="@publisher" />
</publisher>
<isbn>
<xsl:value-of select="@isbn" />
</isbn>
<year>
<xsl:value-of select="@year" />
</year>
</book>
</xsl:template>
如果可以安全地假设源文档属性的名称将与输出文档中元素的名称匹配,则可以使用这两个更短、更精简的模板替换第二个模板:
<xsl:template match="outline/outline">
<book>
<xsl:apply-templates select="@text" />
<xsl:apply-templates select="@author" />
<xsl:apply-templates select="@title" />
<xsl:apply-templates select="@publisher" />
<xsl:apply-templates select="@isbn" />
<xsl:apply-templates select="@year" />
</book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
这里有一个稍微面向推送的解决方案,它不太依赖于手动将元素从源XML拉入结果XML。特别是,请注意最后一个模板
当此XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
…应用于提供的源XML:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
…生成所需结果:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
阿贝尔森,哈罗德
哈罗德·阿贝尔森
计算机编程人员的结构和解释。
这是我的信息
斯普林格·维拉格
3540520430
1991
亚伯拉罕,保罗W。
保罗·W·亚伯拉罕
不耐烦的人
艾迪生卫斯理酒吧公司
0201513757
2000
巴赫,弗雷德
弗雷德巴赫
UNIX Handbuch zur程序Wicklung
汉瑟·法赫布什维拉格
3446151036
巴赫,莫里斯J。
莫里斯·J·巴赫
UNIX操作系统的设计
普伦蒂斯大厅酒店
0132017997
1986
一个更短、完整的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
在提供的XML文档上应用此转换时:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
生成所需的正确结果:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="outline[outline]">
<books text="{@text}">
<xsl:apply-templates />
</books>
</xsl:template>
<xsl:template match="outline[not(*)]">
<book>
<xsl:apply-templates select="@*" />
</book>
</xsl:template>
<xsl:template match="outline[not(*)]/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen.
Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
</list>
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book><xsl:apply-templates select="@*"/></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<body>
<outline text="A">
<outline text="Abelson, Harold" author="Harold Abelson" title="Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung" publisher="Springer Verlag" isbn="3540520430" year="1991"/>
<outline text="Abrahams, Paul W." author="Paul W. Abrahams" title="Tex for the Impatient" publisher="Addison-Wesley Pub Co" isbn="0201513757" year="2000"/>
</outline>
<outline text="B">
<outline text="Bach, Fred" author="Fred Bach" title="UNIX Handbuch zur Programmentwicklung" publisher="Hanser Fachbuchverlag" isbn="3446151036"/>
<outline text="Bach, Maurice J." author="Maurice J. Bach" title="Design of the UNIX Operating System" publisher="Prentice Hall PTR" isbn="0132017997" year="1986"/>
</outline>
</body>
<list>
<books text="A">
<book>
<text>Abelson, Harold</text>
<author>Harold Abelson</author>
<title>Struktur und Interpretation von Computerprogrammen. Eine Informatik-Einführung</title>
<publisher>Springer Verlag</publisher>
<isbn>3540520430</isbn>
<year>1991</year>
</book>
<book>
<text>Abrahams, Paul W.</text>
<author>Paul W. Abrahams</author>
<title>Tex for the Impatient</title>
<publisher>Addison-Wesley Pub Co</publisher>
<isbn>0201513757</isbn>
<year>2000</year>
</book>
</books>
<books text="B">
<book>
<text>Bach, Fred</text>
<author>Fred Bach</author>
<title>UNIX Handbuch zur Programmentwicklung</title>
<publisher>Hanser Fachbuchverlag</publisher>
<isbn>3446151036</isbn>
</book>
<book>
<text>Bach, Maurice J.</text>
<author>Maurice J. Bach</author>
<title>Design of the UNIX Operating System</title>
<publisher>Prentice Hall PTR</publisher>
<isbn>0132017997</isbn>
<year>1986</year>
</book>
</books>
<list>
阿贝尔森,哈罗德
II。如果需要生成元素的特定顺序(因为无法保证源属性的顺序),请使用此轻微修改:
<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="/*"><list><xsl:apply-templates/></list></xsl:template>
<xsl:template match="outline">
<books text="{@text}">
<xsl:apply-templates/>
</books>
</xsl:template>
<xsl:template match="outline/outline">
<book>
<xsl:apply-templates select="@*">
<xsl:sort data-type="number" select=
string-length(substring-before('text|author|title|publisher|isbn|year',name()))"/>
</xsl:apply-templates></book>
</xsl:template>
<xsl:template match="outline/outline/@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
或者浏览这里的XSLT问题。这里有很多很棒的东西。不客气。:)谢谢,您知道如何消除XML中的html头吗?是的,您可以将
添加到XSLT中。我已经更新了我的答案。我建议使用Stylus Studio工具来实现这一点。为什么?这不是编辑的问题。或者它提供了解决方案?Michael,您可能对一个更短的解决方案感兴趣。您的输出缺少外部的
元素,并且您的方法假设属性总是按相同的顺序排列,这可能是一个危险的假设。@JLRishe,我很想知道:我在哪里假设属性的顺序?就在这里:
。尝试交换源XML中文本
和作者
属性的顺序,输出XML将在文本
元素之前有作者
元素。虽然模式不能要求属性的顺序,但元素的顺序通常是。@JLRishe,哦,我明白了。我添加了一个“第二部分”,它按照通缉令执行处理。只添加了两行。:)聪明的把戏。我一定会记下这个技巧。