使用XSLT 1.0的xml消息中的元素对相邻段进行排序
我是XSLT新手。我需要帮助实现以下输出。我将使用以下示例解释我的要求: 输入:使用XSLT 1.0的xml消息中的元素对相邻段进行排序,xslt,Xslt,我是XSLT新手。我需要帮助实现以下输出。我将使用以下示例解释我的要求: 输入: <library> <Name>aaaaa</Name> <Street>wwww</Street> <Country>qqqq</Country> <stock> <book> <Details> <Ranking>3</Rank
<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>
AAAA
WW
qqqq
3.
abc3
hhhhh3
2.
abc2
hhhh2
1.
abc1
hhhhh1
4.
abc4
hhhhh4
输出:
<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>
AAAA
WW
qqqq
1.
abc1
hhhhh1
2.
abc2
hhhh2
3.
abc3
hhhhh3
4.
abc4
hhhhh4
“book”段需要使用XSLT1.0根据“排名”按升序排序
我尝试了排序功能,但是如果所有的“细节”都在一本“书”中,它就可以工作。我尝试了很多其他的事情,但没有成功。有人能帮我一下吗?分享XSLT总是一个很好的做法,它已经尝试过实现期望的结果。这有助于解决方案提供商解决您面临的问题,而不是从头开始 以下模板将帮助您根据
对
进行排序
试试看
我的朋友帮助我解决了这个问题。下面的代码是我问题的答案。感谢Imran和Aniket的回复
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- suppress all but the last node; see next template -->
<xsl:template match="book[*]" />
<!-- sort by package id -->
<xsl:template match="book[last()]">
<xsl:for-each select="../book">
<xsl:sort order="ascending" select="Details/Ranking"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Paging Dimitre Novatchev…谢谢Imran,我的输入消息有点复杂,它有更多的元素,属性介于“book”和“Details”之间。当我运行示例输入时,它工作正常,除了元素,“book”和“Details”之间的属性不会在输出中产生。当我对消息应用相同的逻辑时,我得到一个空标记。即。所有数据都丢失了。把你的输入文件给我。我知道发生了什么。我使用了不同的XSLT解析器,并得到了输出。如何在输出中获得介于“Stock”和“book”之间的元素和其他属性?嗨,Aniket,谢谢你的评论。我会记下来的。我也试过你的逻辑。我的输入消息有点复杂,它有更多的元素,属性介于“book”和“Details”之间。当我运行示例输入时,它工作正常,除了元素,“book”和“Details”之间的属性不会在输出中产生。当我对消息应用相同的逻辑时,我得到一个空标记。即。所有数据都丢失了。我不知道出了什么问题。请编辑问题并共享正确的输入XML。@Bhaskar听起来您已经简化了您的问题,但您对XSLT了解不够,无法推断简化问题的答案以使其与原始问题一起工作。这对我来说意味着你需要花更多的时间阅读该语言并理解其概念。@Michael Kay,当我使用不同的XSLT解析器时,它也解决了我的问题。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- suppress all but the last node; see next template -->
<xsl:template match="book[*]" />
<!-- sort by package id -->
<xsl:template match="book[last()]">
<xsl:for-each select="../book">
<xsl:sort order="ascending" select="Details/Ranking"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>