对于以下输入,我有条件递增的问题。
条件: 在我预期的输入中,将重复5。我想在元素重复的情况下(仅当其值等于5时)添加版本控制
输入
<?xml version="1.0"?><?xml-stylesheet type="text/xsl"?> <data> <rec>
<Pt>1</Pt>
</rec>
<rec>
<Pt>3</Pt>
</rec>
<rec>
<Pt>5</Pt>
</rec>
<rec>
<Pt>5</Pt>
</rec>
<rec>
<Pt>5</Pt>
</rec>
<rec>
<Pt>6</Pt>
</rec>
</data>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<PT>1</PT>
<DerivedPT>1</DerivedPT>
</Record>
<Record>
<PT>3</PT>
<DerivedPT>3</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.1</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.2</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.3</DerivedPT>
</Record>
<Record>
<PT>6</PT>
<DerivedPT>6</DerivedPT>
</Record>
我已经尝试过此XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:for-each select="data/rec">
<Record>
<PT>
<xsl:value-of select="Pt" />
</PT>
<DerivedPT>
<xsl:variable name="pt" select="Pt" />
<xsl:choose>
<xsl:when test="contains($pt,'5')">
<xsl:value-of
select="concat($pt,'.',count(preceding-sibling::rec/Pt[1])+1)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pt" />
</xsl:otherwise>
</xsl:choose>
</DerivedPT>
</Record>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但是,我得到的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<PT>1</PT>
<DerivedPT>1</DerivedPT>
</Record>
<Record>
<PT>3</PT>
<DerivedPT>3</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.3</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.4</DerivedPT>
</Record>
<Record>
<PT>5</PT>
<DerivedPT>5.5</DerivedPT>
</Record>
<Record>
<PT>6</PT>
<DerivedPT>6</DerivedPT>
</Record>
这是因为它正在拾取所有标签。我必须进行哪些更改,因此仅需要5的PT。
<xsl:value-of
select="concat($pt,'.',count(preceding-sibling::rec/Pt[1])+1)" />
答案 0 :(得分:1)
我在变量中设置了Subversion编号,以提高可读性,但是以下代码可以很好地满足您的要求。将<xsl:choose>
的内容替换为以下内容:
<xsl:when test="contains($pt,'5')">
<xsl:variable name="thisSubversion" select="count(preceding-sibling::rec[Pt='5']) + 1" />
<xsl:value-of select="concat($pt,'.', $thisSubversion)" />
</xsl:when>
这使您只能查看值为“ 5”的rec/Pt
个节点的值。我希望这会有所帮助!
答案 1 :(得分:1)
尽管可能仅重复5次,但这可能是使用称为Muenchian Grouping的技术的机会,因为它是一种学习的好技术(假设您限于XSLT 1.0)
在这种情况下,您可以以此方式重新编写XSLT ...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="recs" match="rec" use="Pt" />
<xsl:template match="data">
<xsl:copy>
<xsl:for-each select="rec[generate-id() = generate-id(key('recs', Pt)[1])]">
<xsl:apply-templates select="key('recs', Pt)" />
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="rec">
<Record>
<xsl:copy-of select="Pt" />
<DerivedPT>
<xsl:value-of select="Pt" />
<xsl:if test="Pt = '5'">
<xsl:value-of select="concat('.',position())" />
</xsl:if>
</DerivedPT>
</Record>
</xsl:template>
</xsl:stylesheet>
这里的优势是,如果您确实希望所有记录都具有逻辑,而不仅仅是“ 5”,那么您只需删除xsl:if
条件。
而且,如果您的结构较大,则与不断计算前面的同级兄弟的数量相比,这应该更有效。
答案 2 :(得分:1)
我也宁愿避免重复倒数,而改为执行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="rec">
<Record>
<PT>
<xsl:value-of select="Pt" />
</PT>
<DerivedPT>
<xsl:choose>
<xsl:when test="Pt=5">
<xsl:text>5.</xsl:text>
<xsl:number count="rec[Pt=5]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Pt" />
</xsl:otherwise>
</xsl:choose>
</DerivedPT>
</Record>
</xsl:template>
</xsl:stylesheet>