基于多个条件替换属性值

huangapple go评论145阅读模式
英文:

Replacing attribute value based on multiple conditions

问题

<mydocument>
    <ol outputclass="abc">
        <li>
            <p>
                This is a regular ol,
                and outputclass= abc without linum.
            </p>
        </li>
        <li>
            <p>This is a regular ol,
                and outputclass= abc without linum.</p>
        </li>
    </ol>
    <ol outputclass="continue()">
        <li>
            <p>
                Here we use linum with an ol and outputclass="static".
            </p>
        </li>
        <li>
            <p>Here we use linum with an ol and outputclass="static".</p>
        </li>
    </ol>
    <ol outputclass="static">
        <li>
            <p>This is ol with outputclass as static without linum element</p>
        </li>
    </ol>
</mydocument>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@outputclass">
        <xsl:attribute name="outputclass">
            <xsl:choose>
                <xsl:when test=". = 'static'">
                    <xsl:text>continue()</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="." />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="ol/li/linum" />
</xsl:stylesheet>
英文:

xml file-

&lt;mydocument&gt;
	&lt;ol outputclass=&quot;abc&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;
				This is a regular ol,
				and outputclass= abc without linum.
			&lt;/p&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;p&gt;This is a regular ol,
				and outputclass= abc without linum.&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;static&quot;&gt;
		&lt;li&gt;
			&lt;linum&gt;3.&lt;/linum&gt;
			&lt;p&gt;
				Here we use linum with an ol and outputclass=”static”.				
			&lt;/p&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;linum&gt;4.&lt;/linum&gt;
			&lt;p&gt;Here we use linum with an ol and outputclass=”static”.
			&lt;/p&gt;
		&lt;/li&gt;
		
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;static&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;This is ol with outputclass as static without linum element
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
&lt;/mydocument&gt;

Xsl file-

&lt;xsl:stylesheet
	xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
	xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; version=&quot;2.0&quot;&gt;

	&lt;xsl:template match=&quot;node()|@*&quot;&gt;
		&lt;xsl:copy&gt;
			&lt;xsl:apply-templates select=&quot;node()|@*&quot; /&gt;
		&lt;/xsl:copy&gt;
	&lt;/xsl:template&gt;

	&lt;xsl:template match=&quot;@outputclass&quot;&gt;
		&lt;xsl:attribute name=&quot;outputclass&quot;&gt;
	        &lt;xsl:choose&gt;
	             &lt;xsl:when test=&quot;. =&#39;static&#39;&quot;&gt;	            
	        		&lt;xsl:text&gt;continue()&lt;/xsl:text&gt;	        		
	      		&lt;/xsl:when&gt;
	      		
	      		&lt;xsl:otherwise&gt;
                	&lt;xsl:value-of select=&quot;.&quot; /&gt;
            	&lt;/xsl:otherwise&gt;
	        &lt;/xsl:choose&gt;
	        
	    &lt;/xsl:attribute&gt;
	&lt;/xsl:template&gt;
	&lt;!-- drop linum elements and its value --&gt;
	&lt;xsl:template match=&quot;ol/li/linum&quot;&gt;
	&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

I am trying to replace outputclass value from static to continue() if there is linum element found inside ol/li tag and also want to remove linum tag completely. I am able to achieve this for my second ol but it is also replacing the third ol outputclass value which ideally should not because it has not linum element inside.

Output I am getting-

&lt;mydocument&gt;
	&lt;ol outputclass=&quot;abc&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;
				This is a regular ol,
				and outputclass= abc without linum.
			&lt;/p&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;p&gt;This is a regular ol,
				and outputclass= abc without linum.
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;continue()&quot;&gt;
		&lt;li&gt;

			&lt;p&gt;Here we use linum with an ol and outputclass=”static”.
			&lt;/p&gt;
		&lt;/li&gt;

		&lt;li&gt;
			&lt;p&gt;Here we use linum with an ol and outputclass=”static”.
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;continue()&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;This is ol with outputclass as static without linum element
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
&lt;/mydocument&gt;

Desired output-

&lt;mydocument&gt;
	&lt;ol outputclass=&quot;abc&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;
				This is a regular ol,
				and outputclass= abc without linum.
			&lt;/p&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;p&gt;This is a regular ol,
				and outputclass= abc without linum.
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;continue()&quot;&gt;
		&lt;li&gt;

			&lt;p&gt;Here we use linum with an ol and outputclass=”static”.
			&lt;/p&gt;
		&lt;/li&gt;

		&lt;li&gt;

			&lt;p&gt;Here we use linum with an ol and outputclass=”static”.
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
	&lt;ol outputclass=&quot;static&quot;&gt;
		&lt;li&gt;
			&lt;p&gt;This is ol with outputclass as static without linum element
			&lt;/p&gt;
		&lt;/li&gt;
	&lt;/ol&gt;
&lt;/mydocument&gt;

答案1

得分: 0

使用以下模板处理属性:

<xsl:template match="ol[.//linum]/@outputclass[. = 'static']">
    <xsl:attribute name="outputclass">continue()</xsl:attribute>
</xsl:template>

以及你已有的另外两个模板。

英文:

For the attribute use the template

&lt;xsl:template match=&quot;ol[.//linum]/@outputclass[. = &#39;static&#39;]&quot;&gt;
    &lt;xsl:attribute name=&quot;outputclass&quot;&gt;continue()&lt;/xsl:attribute&gt;
&lt;/xsl:template&gt;

together with the other two templates you have.

huangapple
  • 本文由 发表于 2020年9月23日 17:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64025244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定