English 中文(简体)
XSLT在我接手XML时正在重复描述
原标题:XSLT is duplicating a Description when I recurse XML
  • 时间:2010-09-21 23:07:36
  •  标签:
  • html
  • xml
  • xslt

我正在使用SLT,以回收一些XML,然后对产出适用一些超文本。 它将收回数据,但它重复了上级项目说明,我不清楚为什么? 我相信,这在我面前是正确的,但我看不到。 在XML的下一个层次上插入<ul> tag。

XML 例:

<root>
    <filters>
        <filter ID="My Test">
            <item id="1">
                <description>MyTest Descrip</description>
                <item id="1">
                    <description>Sub Level - 1</description>
                </item>
                <item id="2">
                    <description>Sub Level - 2</description>
                </item>
                <item id="3">
                    <description>Sub Level - 3</description>
                <item id="4">
                <description>Sub Level 2 - 1</description>
                </item>
                    <item id="5">
                    <description>Sub Level 2 - 2</description>
                    </item>
                </item>
            </item>
        </filter>
    </filters>
</root>

XSLT 例:

<?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" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="filter">
    <xsl:variable name="dataID" select="@ID"/>
        <ul class="searchdata">
            <xsl:apply-templates select="item"/>
        </ul>
    </xsl:template>
    <xsl:template match="item">
    <li>
        <xsl:variable name="searchID" select="@id"/>
        <input id="{$searchID}" type="checkbox"/>
        <label for="{$searchID}">
            <xsl:value-of select="description"/>
        </label>
        <xsl:if test="item">
        <ul>
            <xsl:apply-templates />
        </ul>
        </xsl:if>
    </li>
</xsl:template>
</xsl:stylesheet>

传真:

<?xml version="1.0" encoding="utf-8"?>

    <ul class="searchdata"><li><input id="1" type="checkbox" /><label for="1">MyTest Descrip</label><ul>
    MyTest Descrip
     <li><input id="1" type="checkbox" /><label for="1">Sub Level - 1</label></li>
     <li><input id="2" type="checkbox" /><label for="2">Sub Level - 2</label></li>
          <li><input id="3" type="checkbox" /><label for="3">Sub Level - 3</label><ul>
              Sub Level - 3
              <li><input id="4" type="checkbox" /><label for="4">Sub Level 2 - 1</label></li>

              <li><input id="5" type="checkbox" /><label for="5">Sub Level 2 - 2</label></li>
          </ul></li>
     </ul></li></ul>

任何建议都将受到高度赞赏。

感谢。

最佳回答

问题是,你没有注意到built-in Rules,特别是文本节和内容的内在规则。

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

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

因此,你需要添加这一条文字内容规则:

<xsl:template match="text()"/>

之后,你的产出将是:

<ul class="searchdata">
    <li>
        <input id="1" type="checkbox" />
        <label for="1">MyTest Descrip</label>
        <ul>
            <li>
                <input id="1" type="checkbox" />
                <label for="1">Sub Level - 1</label>
            </li>
            <li>
                <input id="2" type="checkbox" />
                <label for="2">Sub Level - 2</label>
            </li>
            <li>
                <input id="3" type="checkbox" />
                <label for="3">Sub Level - 3</label>
                <ul>
                    <li>
                        <input id="4" type="checkbox" />
                        <label for="4">Sub Level 2 - 1</label>
                    </li>
                    <li>
                        <input id="5" type="checkbox" />
                        <label for="5">Sub Level 2 - 2</label>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

此外,这一风格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="item"/>
    <xsl:template match="item[1]">
        <ul class="searchdata">
            <xsl:apply-templates select="../item" mode="li"/>
        </ul>
    </xsl:template>
    <xsl:template match="item" mode="li">
        <li>
            <input id="{@id}" type="checkbox"/>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
    <xsl:template match="description">
        <label for="{../@id}">
            <xsl:value-of select="."/>
        </label>
    </xsl:template>
</xsl:stylesheet>

<说明>:这条规则配对代码<>描述>/代码>要素明确表明,对于要素(适用于儿童节点的模板)而言,“称管”比“在模板中”的规则更为优先。

最后,这一风格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="node()">
        <xsl:apply-templates select="node()[1]|following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="item[1]">
        <ul class="searchdata">
            <xsl:call-template name="item"/>
        </ul>
    </xsl:template>
    <xsl:template match="item" name="item">
        <li>
            <input id="{@id}" type="checkbox"/>
            <xsl:apply-templates select="node()[1]"/>
        </li>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="description">
        <label for="{../@id}">
            <xsl:value-of select="."/>
        </label>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
</xsl:stylesheet>

<说明>:这一用途不是采用复读模板。

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签