首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xslt反转XML中的内容

使用xslt反转XML中的内容
EN

Stack Overflow用户
提问于 2014-12-24 15:24:35
回答 3查看 335关注 0票数 0

我正在为一次考试学习,在那里我们应该能够用XML来逆转事情。我正试图颠倒书籍的顺序,或颠倒这些书中元素的顺序。

输入的XML是:

代码语言:javascript
复制
   <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>

倒序书:

代码语言:javascript
复制
   <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>

颠倒书籍中物品的顺序:

代码语言:javascript
复制
   <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。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-24 15:57:41

试试这个:

第一类排序:

代码语言:javascript
复制
<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>

第二类:

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2014-12-24 15:38:48

最简单的方法是按位置()对节点进行降序( 排序 )。

票数 0
EN

Stack Overflow用户

发布于 2014-12-24 18:05:51

很简单:

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27639050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档