我正在尝试基于文本进行排序 - XSLT [英] I am trying to sorting based on Text - XSLT
本文介绍了我正在尝试基于文本进行排序 - XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对基于文本的文本进行排序,第一个文本应为大写,然后其他以首字母大写或小写字母开头.
输入 XML
I am trying to sorting on text based first text should be upper case then other start with first letter caps or small letter.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p content-type="emCase"><named-content content-type="emEntry">B</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">BTS, USA, Inc. v Executive Perspectives, LLC (Conn. Super, Oct. 16, 2014, No. X10CV116010685) 2014 Conn Super Lexis 2644, aff’d (Conn App 2016) 142 A3d 342:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">buySAFE, Inc. v Google, Inc. (Fed Cir 2014) 765 F3d 1350:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Babcock v Butler County (3d Cir 2015) 806 F3d 153:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Buxbom v Smith (1944) 23 C2d 535:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Byrd v Roadway Express, Inc. (5th Cir 1982) 687 F2d 85:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">B.K.B. v Maui Police Dep’t (9th Cir 2002) 276 F3d 1091:</named-content></p>
</root>
预期产出
<root>
<p content-type="emCase"><named-content content-type="emEntry">B</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">B.K.B. v Maui Police Dep’t (9th Cir 2002) 276 F3d 1091:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">BTS, USA, Inc. v Executive Perspectives, LLC (Conn. Super, Oct. 16, 2014, No. X10CV116010685) 2014 Conn Super Lexis 2644, aff’d (Conn App 2016) 142 A3d 342:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Babcock v Butler County (3d Cir 2015) 806 F3d 153:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Buxbom v Smith (1944) 23 C2d 535:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">buySAFE, Inc. v Google, Inc. (Fed Cir 2014) 765 F3d 1350:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Byrd v Roadway Express, Inc. (5th Cir 1982) 687 F2d 85:</named-content></p>
</root>
我的 XSLT 代码
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each select="p[named-content[@content-type='emEntry'][matches(., '^([A-Za-z]+)')]]">
<xsl:sort select="if(named-content[@content-type='emEntry' and matches(., '^([A-Z]+)')]) then named-content[@content-type='emEntry' and matches(., '^([A-Z]+)')] else upper-case(named-content[@content-type='emEntry'])" data-type="text" order="ascending"/>
<xsl:text>
</xsl:text>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
谁能帮我看看我的错误代码在哪里?
Can any one help me that where is my wrong code?
推荐答案
以下是否符合您的要求?(需求很难理解.我假设有更简单、更快的 XSLT 代码来完成这个任务......)
Does the following meet your requirements? (Requirements were hard to understand. I assume there is more simple and faster XSLT code to accomplish this task ...)
它将给定输入评估为预期输出 https://xsltfiddle.liberty-development.net/pNmC4J7
It evaluates for the given input to the expected output https://xsltfiddle.liberty-development.net/pNmC4J7
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="psStartingWithChar" match="p" use="upper-case(substring(translate(named-content/text(), translate(named-content/text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', ''), ''),1,1))"/>
<xsl:template match="root">
<xsl:variable name="ctxt">
<xsl:copy-of select="."/>
</xsl:variable>
<xsl:variable name="temp">
<xsl:for-each select="string-to-codepoints('ABCDEFGHIJKLMNOPQRSTUVWXYZ')">
<xsl:variable name="ch" select="codepoints-to-string(.)"/>
<xsl:variable name="root">
<xsl:for-each select="$ctxt">
<root>
<xsl:for-each select="key('psStartingWithChar',$ch)">
<xsl:copy-of select="."/>
</xsl:for-each>
</root>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$root/root">
<xsl:copy>
<xsl:for-each select="p[named-content[@content-type='emEntry'][string-length(.)=1 or matches(translate(., translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', ''), ''), '^([^a-z]{2,})')]]">
<xsl:sort data-type="text" order="ascending"/>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="p[named-content[@content-type='emEntry'][not(string-length(.)=1 or matches(translate(., translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', ''), ''), '^([^a-z]{2,})'))]]">
<xsl:sort data-type="text" case-order= "upper-first" order="ascending"/>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<root>
<xsl:copy-of select="$temp/root/*"/>
</root>
</xsl:template>
</xsl:stylesheet>
这篇关于我正在尝试基于文本进行排序 - XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文