0

我有 XML 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<childrens>
  <child entity_id="1" value="Root Catalog" parent_id="0">
    <child entity_id="2" value="Apparel" parent_id="1">
      <child entity_id="4" value="Shirts" parent_id="2"/>
      <child entity_id="5" value="Pants" parent_id="2"/>
    </child>
    <child entity_id="3" value="Accessories" parent_id="1">
      <child entity_id="6" value="Handbags" parent_id="3"/>
      <child entity_id="7" value="Jewelry" parent_id="3"/>
    </child>
     .
     .
     .
    <child entity_id='1110' value="test" parent_is="1109">
       <child entity_id='1111' value="test0" parent_is="1110">
           <child entity_id="1005" value="test1" parent_is="1111"/>
           <child entity_id="1006" value="test12" parent_is="1111"/>
           <child entity_id="1007" value="test123" parent_is="1111"/>
        <child>
    </child>
  <child >
</childrens>

现在我已经编写了这个 XSL 代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
  <xsl:param name="selected" />

  <xsl:template match="/*">
    <xsl:apply-templates select="*[not($selected)] | //*[@entity_id = $selected]/*" />
  </xsl:template>

  <xsl:template match="*">
    <span onclick="displayResult('{@entity_id}');">
      <xsl:value-of select="concat(@value, '&#xA0;')"/><br/>
    </span>
  </xsl:template>
</xsl:stylesheet>

HTML是

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Region</title>
    <link href='style.css' rel='stylesheet' type='text/css'/>
    <script>
        var xml = loadXMLDoc("tree.xml");
        var xsl = loadXMLDoc("try.xsl");

        function loadXMLDocActiveX(location) {
            var doc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
            doc.async = false;
            doc.load(location);
            return doc;
        }

        function loadXMLDocOther(location) {
            xhttp = new XMLHttpRequest();
            xhttp.open("GET", location, false);
            xhttp.send("");
            return xhttp.responseXML;
        }

        function loadXMLDoc(dname) {
            if (window.ActiveXObject) {
                return loadXMLDocActiveX(dname);
            }
            else if (window.XMLHttpRequest) {
                return loadXMLDocOther(dname);
            }
        }

        function transformActiveX(xml, xsl, target, selected) {
            var transform = new ActiveXObject("MSXML2.XSLTemplate");
            transform.stylesheet = xsl;
            var processor = transform.createProcessor();
            processor.input = xml;

            if (selected) {
                processor.addParameter("selected", selected);
            }

            processor.transform();
            target.innerHTML = processor.output;
        }

        function transformOther(xml, xsl, target, selected) {
            var xsltProcessor = new XSLTProcessor();
            xsltProcessor.importStylesheet(xsl);

            if (selected) {
                xsltProcessor.setParameter(null, "selected", selected);
            }

            var resultDocument = xsltProcessor.transformToFragment(xml, document);
            target.innerHTML = "";
            target.appendChild(resultDocument);
        }

        function displayResult(selected) {
            var targetElement = document.getElementById("load");

            // code for IE
            if (window.ActiveXObject) {
                transformActiveX(xml, xsl, targetElement, selected);
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && 
                     document.implementation.createDocument) {
                transformOther(xml, xsl, targetElement, selected);
            }
        }
    </script>
</head>
<body onload="displayResult()">
    <div id="load">
    </div>
</body>
</html>

我正在使用此代码制作树现在我想要其中的 xsl

1)我想从特定节点开始这个,例如test这里entity_id=1110

2) 叶元素有一个类child,例如这里叶元素是test1, test12, test123在元素的情况下,其中 value = test 并且 value = test0

4

1 回答 1

1

这个怎么样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
  <xsl:param name="defaultValue" select="'test'" />
  <xsl:param name="selected" select="//*[@value = $defaultValue]/@entity_id" />

  <xsl:template match="/*">
    <xsl:apply-templates select="*[not($selected)] | //*[@entity_id = $selected]/*" />
  </xsl:template>

  <xsl:template match="*">
    <span onclick="displayResult('{@entity_id}');">
      <xsl:if test="not(*)">
         <xsl:attribute name="class">leaf</xsl:attribute>
      </xsl:if>
      <xsl:value-of select="concat(@value, '&#xA0;')"/><br/>
    </span>
  </xsl:template>
</xsl:stylesheet>

如果向 中添加select属性,则xsl:param可以指示entity_id要用作默认起始项的项。

第二xsl:if个模板中的 将class="leaf"属性添加到叶节点。

于 2013-02-13T10:46:07.690 回答