迭代通过增加数字命名的节点

时间:2013-08-09 14:28:38

标签: xslt

我正在学习XSLT,并且我有以下XML示例:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>

<ROWSET>
 <nazwa>nazwa d</nazwa>
 <ROW>
  <NAME>Kwota bieżąca</NAME>
  <SCHEDULE>0</SCHEDULE>
  <UNDISPOSED>0</UNDISPOSED>
  <FSUM>0</FSUM>
  <DAYS>
      <DAY1>5</DAY1>
      <DAY2>4</DAY2>
      <DAY3>3</DAY3>
      <DAY4>2</DAY4>
      <DAY5>1</DAY5>
  </DAYS>
 </ROW>
</ROWSET>

所以节点名称正在改变我需要遍历所有日子,所以我在输出中有5 4 3 2 1。我不知道该怎么做。我想我必须以某种方式使用xsl:for-each。

此模板可以使用吗?

<xsl:template name="dni_miesiaca_dane">
        <xsl:param name="count"/>

        <xsl:call-template name="drukuj_liczbe">
            <xsl:with-param name="wartosc"
                select="/ROWSET/ROW/DAYS/DAY[$count]" />
        </xsl:call-template>

        <xsl:if test="$count &lt; 31">
            <xsl:call-template name="dni_miesiaca_dane">
                <xsl:with-param name="count" select="$count+1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

我不知道如何更换DAY1,DAY2等以使其正常工作。 DAY[$count]不起作用......

2 个答案:

答案 0 :(得分:1)

在xslt 1中你可以这样做:

<?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="/">
        <!-- ... just continue in processing... -->
        <xsl:apply-templates select="ROWSET/ROW/DAYS/*"/>
    </xsl:template>

    <!-- ... if you find node with name starting with DAY put its content to the output -->
    <xsl:template match="node()[starts-with(name(),'DAY')]">
        <xsl:value-of select="." />
        <xsl:text> </xsl:text>
    </xsl:template>
</xsl:stylesheet>

在xslt 2中,它可能更容易

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text" />

    <xsl:template match="/">
        <xsl:value-of select="/ROWSET/ROW/DAYS/node()[starts-with(name(),'DAY')]" separator=" " />
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

XPath地址/ROWSET/ROW/DAYS/*将为您提供所有子元素,并按文档顺序获取它们。你需要通过名字来解决它们吗?

相关问题