次の 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>
main-content
現在、 div と divのクラス属性を、子孫 (CMS アイコンではない) があるrelated
かどうかに基づいて調整しようとしています。related
ここに私が持っているものがあります:
<xsl:template match="div[@id='main-content']">
<xsl:copy>
<!-- copy the current body node contents -->
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="//div[@id='related']/descendant::* and name(//div[@id='related']/*[1]) != 'img' or count(//div[@id='related']/descendant::* > 1) and name(//div[@id='related']/*[1]) != 'img'">span6</xsl:when>
<!-- left nav but no right col -->
<xsl:when test="not(//div[@id='related']/descendant::*) or (count(//div[@id='related']/descendant::* = 1) and name(//div[@id='related']/*[1]) = 'img')">span9</xsl:when>
<!-- no left nav and populated right col -->
<xsl:when test="//div[@id='related']/descendant::* and (count(//div[@id='related']/descendant::* = 1) and name(//div[@id='related']/*[1]) != 'img') and not( //div[@class='data-entry wide'])">span9</xsl:when>
<xsl:otherwise>span12</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:attribute name="class">
<xsl:choose>
<xsl:when test="count(* = 0) or (count(* = 1) and name(*[1]) = 'img')">hidden</xsl:when>
<xsl:when test="descendant::*">span3</xsl:when>
<xsl:otherwise>hidden</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
<!-- output the rest -->
</xsl:copy>
</xsl:template>
そして、related
hidden のクラスが指定されている場合は、後で削除して、帯域幅や DOM スペースなどを占有しないようにします。
xpathで正しい値を取得しているように見えるので、これは正しく機能すると思いましたが、本来のように要素を削除しません。私が知る必要があるので、それは少し奇妙です:
related
CMS 内にないビューの子孫はありますか- そして、CMS 内のビューの場合、特定の画像ではない子孫があります (他の画像は常に div やリンクなどでラップされます)。
何かご意見は?
ありがとう、ジョナサン