xsl is there a way to generate a unique id for containing element and get it to the javascript

天大地大妈咪最大 提交于 2019-12-25 14:46:08

问题


Find the following line below in the xsl:

var tdElem = document.getElementById('???') <!-- Would like to get td here, if possible -->

Is it possible to generate a unique id for the containing td and concat that into my javascript function?

xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th style="text-align:left">Title</th>
            <th style="text-align:left">Artist</th>
          </tr>
          <xsl:for-each select="catalog/cd">
            <tr>
              <td style="background-color: 'can_be_any_color_here'">
                <xsl:value-of select="title"/>
                <script>
                  var tdElem = document.getElementById('???') <!-- Would like to get td here, if possible -->
                  var bgColor = tdElem.style.backgroundColor;
                  var textColor = mycolorcontrastfx(bgcolor);
                  tdElem.style.color = textColor;
                </script>
              </td>
              <td>
                <xsl:value-of select="artist"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

xml

<?xml-stylesheet type="text/xsl" href="blackorwhite.xslt"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
  </cd>
  <cd>
    <title>Hide your heart</title>
  </cd>  
</catalog>

回答1:


You can use generate-id to generate a unique ID for an XML node

<xsl:variable name="id" select="concat('CDTableCell', generate-id())" />

Or, in this particular case, you make do with using just the position of the cd selected

<xsl:variable name="id" select="concat('CDTableCell', position())" />

To assign the id to the td node, you can use Attribute Value Templates

<td id="{$id}" style="..." />

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
          <script>
              function mycolorcontrastfx(bgColor)
              {
                return '#000000';
              }
          </script>
      </head>
      <body>
        <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th style="text-align:left">Title</th>
            <th style="text-align:left">Artist</th>
          </tr>
          <xsl:for-each select="catalog/cd">
            <tr>
              <xsl:variable name="id" select="concat('CDTableCell', position())" />
              <td id="{$id}" style="background-color:#FFFFFF">
                <xsl:value-of select="title"/>
                <script>
                  var tdElem = document.getElementById('<xsl:value-of select="$id" />') 
                  var bgColor = tdElem.style.backgroundColor;
                  var textColor = mycolorcontrastfx(bgColor);
                  tdElem.style.color = textColor;
                </script>
              </td>
              <td>
                <xsl:value-of select="artist"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/43546131/xsl-is-there-a-way-to-generate-a-unique-id-for-containing-element-and-get-it-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!