1

我正在学习 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]不工作...

4

2 回答 2

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>
于 2013-08-09T14:56:42.357 回答
0

XPath 地址/ROWSET/ROW/DAYS/*将为您提供所有子元素,并且您可以按文档顺序获取它们。你有什么理由需要用名字称呼他们吗?

于 2013-08-09T14:53:53.823 回答