xml – 不同的元素和分组
发布时间:2020-12-16 07:50:19 所属栏目:百科 来源:网络整理
导读:给出以下xml片段: Problems Problem Filefile1/File Descriptiondesc1/Description /Problem Problem Filefile1/File Descriptiondesc2/Description /Problem Problem Filefile2/File Descriptiondesc1/Description /Problem/Problems 我需要制作类似的东西
给出以下xml片段:
<Problems> <Problem> <File>file1</File> <Description>desc1</Description> </Problem> <Problem> <File>file1</File> <Description>desc2</Description> </Problem> <Problem> <File>file2</File> <Description>desc1</Description> </Problem> </Problems> 我需要制作类似的东西 <html> <body> <h1>file1</h1> <p>des1</p> <p>desc2</p> <h1>file2</h1> <p>des1</p> </body> </html> 我试过用一把钥匙 <xsl:key name="files" match="Problem" use="File"/> 但我真的不明白如何把它带到下一步,或者如果这是正确的方法.
以下是我使用Muenchean方法的方法.谷歌’xslt muenchean’获取更多信息来自更聪明的人.可能有一种聪明的方式,但我会把它留给别人.
请注意,我避免在xml元素名称的开头使用大写字母,例如’File’,但这取决于你. <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:key name="files" match="/Problems/Problem/File" use="./text()"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="Problems"/> </body> </html> </xsl:template> <xsl:template match="Problems"> <xsl:for-each select="Problem/File[generate-id(.) = generate-id(key('files',.))]"> <xsl:sort select="."/> <h1> <xsl:value-of select="."/> </h1> <xsl:apply-templates select="../../Problem[File=current()/text()]"/> </xsl:for-each> </xsl:template> <xsl:template match="Problem"> <p> <xsl:value-of select="Description/text()"/> </p> </xsl:template> </xsl:stylesheet> 这个想法是,使用它的文本值键入每个File元素.然后只显示文件值,如果它们与键控元素相同.要检查它们是否相同,请使用generate-id.有一种类似的方法可以比较匹配的第一个元素.我不能告诉你哪个更有效率. 我已经使用Marrowsoft Xselerator测试了这里的代码,这是我最喜欢的xslt工具,虽然已经不再可用了afaik.我得到的结果是: <html> <body> <h1>file1</h1> <p>desc1</p> <p>desc2</p> <h1>file2</h1> <p>desc1</p> </body> </html> 这是使用msxml4. 我按文件对输出进行了排序.我不确定你是否想要那个. 我希望这有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |