2013-06-23 34 views
0

我可以通过三个节点之一获取xml。XSLT在元素名称的情况下获取变量

<root> 
    <headerA> 
    <!-- Content --> 
    </headerA> 
</root> 

代替<headerA>可以有<headerB>或具有类似内容<headerС>节点。在这种情况下,我想有HTML输出为:

<span> Type [X] </span> 

其中[X]是A,B或C取决于元素的标签名称。

+0

请更精确。目前还不清楚您的样本输入和样本输出是如何相关的。 – Tomalak

+0

对不起,我在格式化时遇到了一些错误。现在清楚了吗? – Bujaka

回答

1

正如Tomalak已经注意到的,您的问题相当模糊。听起来好像你可能会问如何写无论是三种头的一个模板:

<xsl:template match="headerA | headerB | headerC"> 
    <span> 
    <xsl:apply-templates/> 
    </span> 
</xsl:template> 

要不怎么写类似,但不同的模板,为他们:

<xsl:template match="headerA"> 
    <span> Type A </span> 
</xsl:template> 
<xsl:template match="headerB"> 
    <span> Type B </span> 
</xsl:template> 
<!--* Template for headerC left as an exercise for the reader *--> 

要不然如何写一个模板,做基于它匹配什么略有不同的东西:

<xsl:template match="headerA | headerB | headerC"> 
    <span> 
    <xsl:choose> 
     <xsl:when test="self::headerA"> 
     <xsl:text> Type A </xsl:type> 
     </xsl:when> 
     <xsl:when test="self::headerB"> 
     <xsl:text> Type B </xsl:type> 
     </xsl:when> 
     <!--* etc. *--> 
     <xsl:otherwise> 
     <xsl:message terminate="yes" 
      >I thought this could not happen.</xsl:message> 
     </xsl:otherwise> 
    </xsl:choose> 
    <xsl:apply-templates/> 
    </span> 
</xsl:template> 

如果你搞清楚其中哪些可以帮助你,你会更进一步地理解什么你试图问的问题。

+0

谢谢,第二个是我寻找的 – Bujaka

0
<xsl:template match="*[starts-with(name(), 'header')]"> 
    <xsl:variable name="type" select="substring-after(name(), 'header')" /> 
    <span> 
    <xsl:value-of select="concat(' Type ', $type, ' ')" /> 
    </span> 
</xsl:template> 

这将工作命名headerX任何元素,在这里可以X任何字符串。

相关问题