英文:
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-
<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="static">
<li>
<linum>3.</linum>
<p>
Here we use linum with an ol and outputclass=”static”.
</p>
</li>
<li>
<linum>4.</linum>
<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 file-
<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>
<!-- drop linum elements and its value -->
<xsl:template match="ol/li/linum">
</xsl:template>
</xsl:stylesheet>
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-
<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="continue()">
<li>
<p>This is ol with outputclass as static without linum element
</p>
</li>
</ol>
</mydocument>
Desired output-
<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>
答案1
得分: 0
使用以下模板处理属性:
<xsl:template match="ol[.//linum]/@outputclass[. = 'static']">
<xsl:attribute name="outputclass">continue()</xsl:attribute>
</xsl:template>
以及你已有的另外两个模板。
英文:
For the attribute use the template
<xsl:template match="ol[.//linum]/@outputclass[. = 'static']">
<xsl:attribute name="outputclass">continue()</xsl:attribute>
</xsl:template>
together with the other two templates you have.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论