我做了你的属性具有在形式elemN其中N = 1,2,3 ..., 名称的假设,他们相应地排序。
下面的XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" />
<xsl:template match="/SI">
<xsl:choose>
<xsl:when test="some $i in @* satisfies $i='DA'">
<xsl:text>DA</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="attr" select="concat('elem', xs:decimal(substring-after(@*[.='BA']/name(), 'elem')) + 1)" />
<xsl:value-of select="@*[name() = $attr]" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
应用于以下输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<SI elem1="TI" elem2="FN" elem3="4099450222" elem4="TM" elem5="4094110000" elem6="MT" elem7="SP" elem8="MC" elem9="DS" elem10="DA" elem11="16" />
给出DA
作为输出。
而且应用于下面的XML:
<?xml version="1.0" encoding="UTF-8"?>
<SI elem1="TI" elem2="FN" elem3="4099450222" elem4="TM" elem5="4094110000" elem6="MT" elem7="BA" elem8="03" elem9="DS" elem10="DAs" elem11="16" />
给出03
作为输出。
EDIT
这里的XSLT 1.0版本(下的Altova XMLSpy的测试):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates select="SI/@*" />
</xsl:template>
<xsl:template match="@*">
<xsl:choose>
<xsl:when test=". = 'DA'">
<xsl:text>DA</xsl:text>
</xsl:when>
<xsl:when test=".='BA'">
<xsl:variable name="attr" select="concat('elem', substring-after(name(), 'elem') + 1)" />
<xsl:value-of select="/SI/@*[name() = $attr]" />
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
嗯,依靠你输入XML中的属性顺序,通常是shakey。请参阅http://stackoverflow.com/questions/2287861/xslt-display-attributes-in-specific-order。在任何情况下,我都不相信XSL检查前/后节点的轴可以应用于属性。 – Utkanos
属性名称将永远是elem1,elem2等。有没有办法获得属性名称,然后在最后替换数字字符? – Jaloopa
要获得节点或属性的名称,请使用'name()' – Utkanos