XML:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="displaySpecNav" match="/">
<xsl:for-each select="/root">
<div class="hidOverflow vertAlignT setCenter smallPadBottom">
<div id="dvExpProvHolder" class="hidOverflow innerDivCenter">
<ul class="uSPStyle" id="uSPStyle">
<!-- <xsl:sort select="mOrder" /> -->
<li class="setRelative">
<a class="tfLink clickMe current" title="{grp1/tfLink01}" data-toggle=".tfLink1" href="javascript:void(0);">
<xsl:value-of select="grp1/tfLink01" />
</a>
</li>
<li>
<a class="tfLink clickMe" title="{grp2/tfLink02}" data-toggle=".tfLink2" href="javascript:void(0);">
<xsl:value-of select="grp2/tfLink02" />
</a>
</li>
<li>
<a class="tfLink clickMe" title="{grp3/tfLink03}" data-toggle=".tfLink3" href="javascript:void(0);">
<xsl:value-of select="grp3/tfLink03" />
</a>
</li>
</ul>
</div>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<grp1>
<tfLink01>Link #2</tfLink01>
<mOrder>2</mOrder>
<tfText01 />
<cfSM1>false</cfSM1>
<tf1SLink01 />
<tf1SText01 />
<tf2SLink01 />
<tf2SText01 />
<tf3SLink01 />
<tf3SText01 />
</grp1>
<grp2>
<tfLink02>Link #1</tfLink02>
<mOrder>1</mOrder>
<tfText02 />
<cfSM2>false</cfSM2>
<tf1SLink02 />
<tf1SText02 />
<tf2SLink02 />
<tf2SText02 />
<tf3SLink02 />
<tf3SText02 />
</grp2>
<grp3>
<tfLink03>Link #3</tfLink03>
<mOrder>3</mOrder>
<tfText03 />
<cfSM3>false</cfSM3>
<tf1SLink03 />
<tf1SText03 />
<tf2SLink03 />
<tf2SText03 />
<tf3SLink03 />
<tf3SText03 />
</grp3>
</root>
abode XML / XSLT显示以下无序列表:
Link #2
Link #1
Link #3
如何修改XSLT,以便<xsl:sort select="mOrder" />
正常工作并显示以下内容:
Link #1 (although this is the second entry)
Link #2 (although this is the first entry)
Link #3
此外,在此示例中添加了grp1
,grp2
和grp3
,因此我可以多次添加mOrder
选项,如果我没有该组,该怎么办?并拥有以下XML(我目前的设置,并希望保持这种方式):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tfLink01>Link #2</tfLink01>
<mOrder1>2</mOrder1>
<tfText01 />
<cfSM1>false</cfSM1>
<tf1SLink01 />
<tf1SText01 />
<tf2SLink01 />
<tf2SText01 />
<tf3SLink01 />
<tf3SText01 />
<tfLink02>Link #1</tfLink02>
<mOrder2>1</mOrder2>
<tfText02 />
<cfSM2>false</cfSM2>
<tf1SLink02 />
<tf1SText02 />
<tf2SLink02 />
<tf2SText02 />
<tf3SLink02 />
<tf3SText02 />
<tfLink03>Link #3</tfLink03>
<mOrder3>3</mOrder3>
<tfText03 />
<cfSM3>false</cfSM3>
<tf1SLink03 />
<tf1SText03 />
<tf2SLink03 />
<tf2SText03 />
<tf3SLink03 />
<tf3SText03 />
</root>
我如何处理XML格式。
答案 0 :(得分:1)
给出以下输入:
<强> XML 强>
<root>
<tfLink01>Link #2</tfLink01>
<mOrder>2</mOrder>
<tfText01 />
<cfSM1>false</cfSM1>
<tf1SLink01 />
<tf1SText01 />
<tf2SLink01 />
<tf2SText01 />
<tf3SLink01 />
<tf3SText01 />
<tfLink02>Link #1</tfLink02>
<mOrder>1</mOrder>
<tfText02 />
<cfSM2>false</cfSM2>
<tf1SLink02 />
<tf1SText02 />
<tf2SLink02 />
<tf2SText02 />
<tf3SLink02 />
<tf3SText02 />
<tfLink03>Link #3</tfLink03>
<mOrder>3</mOrder>
<tfText03 />
<cfSM3>false</cfSM3>
<tf1SLink03 />
<tf1SText03 />
<tf2SLink03 />
<tf2SText03 />
<tf3SLink03 />
<tf3SText03 />
</root>
以下stylehseet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<ul>
<xsl:for-each select="mOrder">
<xsl:sort/>
<li>
<xsl:value-of select="preceding-sibling::*[1]"/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
将返回:
<ul>
<li>Link #1</li>
<li>Link #2</li>
<li>Link #3</li>
</ul>
注意:
您可以捕获变量中的序号,例如:
<xsl:variable name="ordinal" select="substring-after(name(preceding-sibling::*[1]), 'tfLink')" />
并使用它来按名称来寻址其他节点,而不是它们在“块”中的位置。
对于mOrder
节点也被编号的输入,即:
<强> XML 强>
<root>
<tfLink01>Link #2</tfLink01>
<mOrder1>2</mOrder1>
<tfText01 />
<cfSM1>false</cfSM1>
<tf1SLink01 />
<tf1SText01 />
<tf2SLink01 />
<tf2SText01 />
<tf3SLink01 />
<tf3SText01 />
<tfLink02>Link #1</tfLink02>
<mOrder2>1</mOrder2>
<tfText02 />
<cfSM2>false</cfSM2>
<tf1SLink02 />
<tf1SText02 />
<tf2SLink02 />
<tf2SText02 />
<tf3SLink02 />
<tf3SText02 />
<tfLink03>Link #3</tfLink03>
<mOrder3>3</mOrder3>
<tfText03 />
<cfSM3>false</cfSM3>
<tf1SLink03 />
<tf1SText03 />
<tf2SLink03 />
<tf2SText03 />
<tf3SLink03 />
<tf3SText03 />
</root>
你可以使用:
<xsl:template match="/root">
<ul>
<xsl:for-each select="*[starts-with(name(), 'tfLink')]">
<xsl:sort select="following-sibling::*[1]"/>
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
答案 1 :(得分:1)
最好像这样构造XML(为简洁起见,省略了未使用的字段):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<grp>
<tfLink>Link #2</tfLink>
<mOrder>2</mOrder>
</grp>
<grp>
<tfLink>Link #1</tfLink>
<mOrder>1</mOrder>
</grp>
<grp>
<tfLink>Link #3</tfLink>
<mOrder>3</mOrder>
</grp>
</root>
然后您的XSLT变为:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<div class="hidOverflow vertAlignT setCenter smallPadBottom">
<div id="dvExpProvHolder" class="hidOverflow innerDivCenter">
<ul class="uSPStyle" id="uSPStyle">
<xsl:for-each select="grp">
<xsl:sort select="mOrder" />
<li>
<a class="tfLink clickMe" title="{tfLink}" href="javascript:void(0);"
data-toggle='collapse' data-target="#{generate-id()}">
<xsl:value-of select="tfLink" />
</a>
</li>
</xsl:for-each>
</ul>
</div>
</div>
</xsl:template>
</xsl:stylesheet>
我还修改了data-toggle
(我假设它的Bootstrap)来命名应切换的类(collapse
为例)和{{1}使用data-target
,在生成要执行的内容时应该使用它。
上面得到的XML是:
generate-id()
如果需要在xsl:for-each中的第一个元素上设置属性,可以在XSLT中使用它:
<?xml version="1.0"?>
<div class="hidOverflow vertAlignT setCenter smallPadBottom">
<div id="dvExpProvHolder" class="hidOverflow innerDivCenter">
<ul class="uSPStyle" id="uSPStyle">
<li>
<a class="tfLink clickMe" title="Link #1" data-toggle="collapse" data-target="#idp31396560" href="javascript:void(0);">Link #1</a>
</li>
<li>
<a class="tfLink clickMe" title="Link #2" data-toggle="collapse" data-target="#idp31377232" href="javascript:void(0);">Link #2</a>
</li>
<li>
<a class="tfLink clickMe" title="Link #3" data-toggle="collapse" data-target="#idp31398000" href="javascript:void(0);">Link #3</a>
</li>
</ul>
</div>
</div>
和<xsl:for-each ...>
<xsl:sort .../>
<li>
<xsl:if test="position()=1">
<xsl:attribute name="class">setCurrent</xsl:attribute>
</xsl:if>
类似。