Xml 多选值和多个限制
已获取此XML文件:Xml 多选值和多个限制,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,已获取此XML文件: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="equipos.xsl"?> <equipos> <equipo nombre="Los paellas" personas="2"/> <equipo nombre="Los arrocitos" personas="13"/> <
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="equipos.xsl"?>
<equipos>
<equipo nombre="Los paellas" personas="2"/>
<equipo nombre="Los arrocitos" personas="13"/>
<equipo nombre="Los gambas" personas="6"/>
<equipo nombre="Los mejillones" personas="3"/>
<equipo nombre="Los garrofones" personas="17"/>
<equipo nombre="Los limones" personas="7"/>
</equipos>
应用XSLT时,输出必须是:
- 属性“personas”被一个名为“categoria”的属性覆盖
- 如果人物角色<5,“categoria”必须有勇气1
- 如果人物角色介于5到10之间,“categoria”必须具有勇气2
- 如果人物角色>10,“categoria”必须有勇气3
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="equipos">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="equipo">
<xsl:copy>
<xsl:attribute name="nombre">
<xsl:value-of select="@nombre"/>
</xsl:attribute>
<xsl:attribute name="categoria">
<xsl:choose>
<xsl:when test="@personas < 5">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>2</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
1.
2.
您可以在中选择您喜欢的元素时拥有任意多的:
<xsl:choose>
<xsl:when test="@personas < 5">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:when test="@personas <= 10">
<xsl:text>2</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>3</xsl:text>
</xsl:otherwise>
</xsl:choose>
这里我们需要明确的优先级,因为默认情况下,模式@personas[.5]
和@personas[.=10]
被认为是同样特定的
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- copy everything unchanged except when overridden -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="@personas[. < 5]" priority="10">
<xsl:attribute name="categoria">1</xsl:attribute>
</xsl:template>
<xsl:template match="@personas[. <= 10]" priority="9">
<xsl:attribute name="categoria">2</xsl:attribute>
</xsl:template>
<xsl:template match="@personas" priority="8">
<xsl:attribute name="categoria">3</xsl:attribute>
</xsl:template>
</xsl:stylesheet>