开发者

Need to read the previous node's child values to format the current node's value using xsl?

I need to use the formats tag while displaying the contents of rowdata, in the rowdata tags.

the xml is follows: "temp.xml"

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Temp.xsl"?>
<ALL_DATA>
    <TITLES>
        <VALUE1>Title1</VALUE1>
        <VALUE2>Title2</VALUE2>
        <FVALUE1>Title3</FVALUE1>
    </TITLES>
    <FORM开发者_JS百科ATS>
        <VALUE1>I5</VALUE1>
        <VALUE2>I3</VALUE2>
        <FVALUE1>F1.1</FVALUE1>
    </FORMATS>
    <MY_DATA>
        <ROWDATA>
            <VALUE1>5</VALUE1>
            <VALUE2>33</VALUE2>
            <FVALUE1>2.11</FVALUE1>
        </ROWDATA>
        <ROWDATA>
            <VALUE1>34</VALUE1>
            <VALUE2>12</VALUE2>
            <FVALUE1>239.81</FVALUE1>
        </ROWDATA>
    </MY_DATA>
</ALL_DATA>

and i tried xsl is: "temp.xsl"

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <xsl:template name="MY_TEMPLATE" match="/">
    <html>
      <body>
        <table border="1">
          <TR border="1">
            <xsl:for-each select="/ALL_DATA/TITLES/*">
              <th border="1">
                <xsl:value-of select="."/>
              </th>
            </xsl:for-each>
          </TR>
          <xsl:for-each select="/ALL_DATA/MY_DATA/ROWDATA">
            <TR>
              <xsl:for-each select="*">
                <TD width ="130">
                  <xsl:value-of select="."/>:-:
                  <xsl:variable name="cur_node_name" select="name(.)"/>
                  <xsl:for-each select="/ALL_DATA/FORMATS[name(.)]">
                   <!--<xsl:template match="$cur_node_name"> -->
                  <xsl:value-of select="."/>
                   <!--</xsl:template>-->
                </xsl:for-each>
                </TD>
              </xsl:for-each>
            </TR>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I am sorry to post entire content. I feel i can get some help. In above xml, i want to use formats/value1 while displaying the Mydata/rowdata/value1. The above Xsl iterates the titles block and display the titles in <th>. and then the second for-each block will iterates through rowdata, and its child items. In displaying child items, i need to use formats tags to display rowdata's childs.

The above xsl is giving output for 3rd tag FValue1 as : "2.11:-: I5 I3 F1.1" ; But my expectation is : "2.11:-: F1.1". If i get this solution, i can do the rest. I know just by modifying inner for loop, that can be possible. but could not get that

Please help if any one have suggestion how to. thanks.


If you sample data isn't oversimplified your xslt could drop the for-each altogether and look like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <xsl:apply-templates select="TITLES" />
                    <xsl:apply-templates />
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="TITLES">
        <tr>
            <th><xsl:value-of select="./VALUE1" /></th>
            <th><xsl:value-of select="./VALUE2" /></th>
            <th><xsl:value-of select="./FVALUE1" /></th>
        </tr>
    </xsl:template>

    <xsl:template match="ROWDATA">
        <tr>
            <td>
                <xsl:value-of select="./VALUE1" />:-:<xsl:value-of select="//FORMATS/VALUE1" />
            </td>
            <td>
                <xsl:value-of select="./VALUE2" />:-:<xsl:value-of select="//FORMATS/VALUE2" />
            </td>
            <td>
                <xsl:value-of select="./FVALUE1" />:-:<xsl:value-of select="//FORMATS/FVALUE1" />
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="MY_DATA">
        <xsl:apply-templates />
    </xsl:template>

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜