我正在为一次考试学习,在那里我们应该能够用XML来逆转事情。我正试图颠倒书籍的顺序,或颠倒这些书中元素的顺序。
输入的XML是:
<library>
<book>
<author>James Joyce</author>
<title>Ulysses</title>
<year>1922</year>
</book>
<book>
<author>Alois Jirasek</author>
<title>Psohlavci</title>
<year>1910</year>
</book>
<book>
<author>Tomas Hruska</author>
<title>Pascal pro zacatecniky</title>
<year>1989</year>
</book>
</library>倒序书:
<library>
<book>
<author>Tomas Hruska</author>
<title>Pascal pro zacatecniky</title>
<year>1989</year>
</book>
<book>
<author>Alois Jirasek</author>
<title>Psohlavci</title>
<year>1910</year>
</book>
<book>
<author>James Joyce</author>
<title>Ulysses</title>
<year>1922</year>
</book>
</library>颠倒书籍中物品的顺序:
<library>
<book>
<year>1922</year>
<title>Ulysses</title>
<author>James Joyce</author>
</book>
<book>
<year>1910</year>
<title>Psohlavci</title>
<author>Alois Jirasek</author>
</book>
<book>
<year>1989</year>
<title>Pascal pro zacatecniky</title>
<author>Tomas Hruska</author>
</book>
</library>有人告诉我,这应该很容易,所以不要尝试太花哨的东西。我被允许使用基本循环通过-每一个或递归通过应用-模板和值-of。
发布于 2014-12-24 15:57:41
试试这个:
第一类排序:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="library">
<library>
<xsl:for-each select="book">
<xsl:sort select="position()" order="descending"/>
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:for-each>
</library>
</xsl:template>
</xsl:stylesheet>第二类:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:for-each select="*">
<xsl:sort select="position()" order="descending"/>
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:for-each>
</book>
</xsl:template>
</xsl:stylesheet>发布于 2014-12-24 15:38:48
最简单的方法是按位置()对节点进行降序( 排序 )。
发布于 2014-12-24 18:05:51
很简单:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/library">
<library>
<xsl:for-each select="book">
<xsl:sort select="author" order="descending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</library>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/27639050
复制相似问题