xslt - 使用 XSLT 1.0 的 xml 消息中的元素对相邻段进行排序
问题描述
我是 XSLT 的新手。我需要帮助来实现以下输出。我将使用以下示例解释我的要求:
输入:
<library>
<Name>aaaaa</Name>
<Street>wwww</Street>
<Country>qqqq</Country>
<stock>
<book>
<Details>
<Ranking>3</Ranking>
<Title>abc3</Title>
<Author>hhhhh3</Author>
</Details>
</book>
<book>
<Details>
<Ranking>2</Ranking>
<Title>abc2</Title>
<Author>hhhhh2</Author>
</Details>
</book>
<book>
<Details>
<Ranking>1</Ranking>
<Title>abc1</Title>
<Author>hhhhh1</Author>
</Details>
</book>
<book>
<Details>
<Ranking>4</Ranking>
<Title>abc4</Title>
<Author>hhhhh4</Author>
</Details>
</book>
</stock>
输出:
<library>
<Name>aaaaa</Name>
<Street>wwww</Street>
<Country>qqqq</Country>
<stock>
<book>
<Details>
<Ranking>1</Ranking>
<Title>abc1</Title>
<Author>hhhhh1</Author>
</Details>
</book>
<book>
<Details>
<Ranking>2</Ranking>
<Title>abc2</Title>
<Author>hhhhh2</Author>
</Details>
</book>
<book>
<Details>
<Ranking>3</Ranking>
<Title>abc3</Title>
<Author>hhhhh3</Author>
</Details>
</book>
<book>
<Details>
<Ranking>4</Ranking>
<Title>abc4</Title>
<Author>hhhhh4</Author>
</Details>
</book>
</stock>
“书”段需要使用 XSLT 1.0 基于“排名”按升序排序。
我尝试了排序功能,但如果所有“详细信息”都在一本“书”中,则可以使用。我尝试了很多其他的东西,但没有成功。有人可以帮帮我吗 ?
解决方案
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="stock">
<xsl:copy>
<xsl:for-each select="book">
<xsl:sort order="ascending" select="Details/Ranking"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
Try it
推荐阅读
- python - 查找数组是否在 str.split() 之后不包含值
- angular - 从 Angular 到不同服务器的 GET 调用中不包含 Cookie?
- reactjs - 挂钩回调时的状态不正确
- aws-sdk - 覆盖 AWS 开发工具包 API 调用
- python - 如何针对 openml.org 进行身份验证
- mysql - 删除 MySQL 中所有列匹配的重复行。保留最新行
- sqlite - Flutter No SQL DB - 是否有任何稳定的 no-sql 数据库可以对嵌套 JSON 对象进行有效查询
- javascript - 未捕获的类型错误:无法在 Vue.js 上执行“drawImage”
- gwt - Eclipse / launch / Java Classpath 括在引号中 / 中断 3rd 方工具 / 阻止开发
- angular - Angular 登录表单让我可以使用任何用户名和密码对