Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:基于元素子体设置类_Xslt_Xpath_Xslt 1.0 - Fatal编程技术网

XSLT:基于元素子体设置类

XSLT:基于元素子体设置类,xslt,xpath,xslt-1.0,Xslt,Xpath,Xslt 1.0,我正在对以下HTML进行XSL转换: <div id="context"> <p>Sometimes, there is content here.</p> </div> <div id="main-content"> <p>There is always content here.</p> </div> <div id="related"> <img s

我正在对以下HTML进行XSL转换:

<div id="context">
    <p>Sometimes, there is content here.</p>
</div>

<div id="main-content">
    <p>There is always content here.</p>
</div>

<div id="related">
    <img src="CMS PREVIEW ICON - ADMIN ONLY"/>
    <p>Sometimes, there is content here.</p>
    <p>The image is always the first child only if the user is inside the CMS, but it should be ignored if there is not other content present.</p>
</div>
然后,如果给了
related
一个隐藏类,我会在以后删除它,这样它就不会占用带宽、DOM空间等

我认为这会正常工作,因为它似乎在xpath中获得了正确的值,但它只是没有像应该的那样删除元素。这有点奇怪,因为我需要知道:

  • 对于不在CMS内的视图,
    related
    中是否有子体
  • 而且,对于CMS内部的视图,是否存在不是特定图像的子体(其他图像将始终包装在div、link等中)
  • 有什么想法吗

    谢谢,
    乔纳森

    即使我不能完全确定我是否正确理解你的要求,我也会尝试一下

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
    
            <xsl:template match="div[@id='main-content']">
        <xsl:copy>
            <!-- copy the current body node contents -->
            <xsl:attribute name="class">
                <xsl:choose>
                    <!-- "related" has any descendants (that are not the CMS icon). -->
                    <xsl:when test="//div[@id='related']/* and 
                              count(//div[@id='related']/img)  !=  count(//div[@id='related']/*) ">span6</xsl:when>
                    <xsl:otherwise>span9</xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
            <!-- output the rest -->
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="div[@id='related']">
        <xsl:copy>
            <!-- copy the current body node contents -->
            <xsl:apply-templates select="@*"/>
            <xsl:choose>
                    <!-- it should be ignored if there is not other content present -->
                    <xsl:when test=" count(img) = count(*)">
                        <xsl:attribute name="class">hidden</xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:attribute name="class">span3</xsl:attribute>
                        <xsl:apply-templates select="node()"/>
    
                    </xsl:otherwise>
                </xsl:choose>
            <!-- output the rest -->
        </xsl:copy>
    </xsl:template>
    
        <xsl:template match="node() | @*">
            <xsl:copy>
                <xsl:apply-templates  select="node() | @*" />
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    
    
    span6
    span9
    隐藏的
    span3
    
    首先,
    count(*=0)
    不是有效的XPath
    count(*)=0
    是有效的方法,但最好使用
    not(*)
    。同样,对于
    count(*=1)
    可以使用
    not(*[2])
    。我在破译您的第一组
    时遇到一些问题。你能帮我们把它们分解一下吗?
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
    
            <xsl:template match="div[@id='main-content']">
        <xsl:copy>
            <!-- copy the current body node contents -->
            <xsl:attribute name="class">
                <xsl:choose>
                    <!-- "related" has any descendants (that are not the CMS icon). -->
                    <xsl:when test="//div[@id='related']/* and 
                              count(//div[@id='related']/img)  !=  count(//div[@id='related']/*) ">span6</xsl:when>
                    <xsl:otherwise>span9</xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
            <!-- output the rest -->
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="div[@id='related']">
        <xsl:copy>
            <!-- copy the current body node contents -->
            <xsl:apply-templates select="@*"/>
            <xsl:choose>
                    <!-- it should be ignored if there is not other content present -->
                    <xsl:when test=" count(img) = count(*)">
                        <xsl:attribute name="class">hidden</xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:attribute name="class">span3</xsl:attribute>
                        <xsl:apply-templates select="node()"/>
    
                    </xsl:otherwise>
                </xsl:choose>
            <!-- output the rest -->
        </xsl:copy>
    </xsl:template>
    
        <xsl:template match="node() | @*">
            <xsl:copy>
                <xsl:apply-templates  select="node() | @*" />
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>