【问题标题】:Check if container element for processing instruction exists检查处理指令的容器元素是否存在
【发布时间】:2014-11-26 12:28:38
【问题描述】:

我正在处理这个处理指令:<?Pub _kern Amount="-25pt"?>

与:

<xsl:template match="processing-instruction('Pub')">
        <xsl:choose>
            <xsl:when test="starts-with(., '_kern')">
                <xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                </xsl:attribute>
            </xsl:when>
        </xsl:choose>
</xsl:template>

但这仅在 PI 位于 &lt;div&gt; 等容器元素内时才有效。我收到一个错误,因为 XSLT 试图将样式标记添加到不存在的父元素。如果我在&lt;xsl:attribute name="style"&gt; 之前包含&lt;span&gt;,那么当PI 位于容器元素内时,代码将不起作用。如何检测是否存在容器元素,以便知道是否添加跨度?除非有更好的方法来做到这一点,否则我是 XSLT 的初学者。

【问题讨论】:

    标签: xml xslt processing-instruction


    【解决方案1】:

    这很有效,如果没有@Tim C 的帮助,我不会得到它:

    <xsl:template match="processing-instruction('Pub')">
        <xsl:choose>
            <xsl:when test="starts-with(., '_kern') and processing-instruction()[parent::*]">
                <xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                </xsl:attribute>
            </xsl:when>
            <xsl:when test="starts-with(., '_kern')">
                <span><xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                </xsl:attribute></span>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>
    

    这可能并不完全正确,但它完成了工作。

    【讨论】:

    • 我认为您的第一个xsl:when 语句将始终返回false,因为在您执行processing-instruction()[parent::*]"&gt; 的地方,这是在寻找第二个处理指令,它是当前处理指令的子级,而不是可能的!您可以将其更改为 &lt;xsl:when test="starts-with(., '_kern') and parent::*"&gt; 但您可能会收到错误消息。也许在所有情况下都输出新的span 是最简单的?
    • 我把这个留在这里作为不应该做的例子,我使用了上面@Tim C 的编辑解决方案。
    【解决方案2】:

    可以通过添加简单条件来检查处理指令是否有父元素

     <xsl:template match="processing-instruction('Pub')[parent::*]">
    

    但要小心,如果您的 XML 看起来像这样:

    <div>
       <?Pub _kern Amount="-25pt"?>    
    </div>
    

    如果先匹配并复制了空白文本节点,您仍然可能会收到错误消息。您可能需要在 XSLT 中包含 xsl:strip-space 命令。

    例如,这会出错

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="processing-instruction('Pub')[parent::*]">
                <xsl:choose>
                    <xsl:when test="starts-with(., '_kern')">
                        <xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                        <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                        </xsl:attribute>
                    </xsl:when>
                </xsl:choose>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    但这不是....

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:strip-space elements="*" />
    
        <xsl:template match="processing-instruction('Pub')[parent::*]">
                <xsl:choose>
                    <xsl:when test="starts-with(., '_kern')">
                        <xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                        <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                        </xsl:attribute>
                    </xsl:when>
                </xsl:choose>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    编辑:响应您的 cmets,即使使用 xsl:strip-space,如果在处理指令之前有一个前置兄弟,您仍然会收到错误

    <div>
        <text></text>
        <?Pub _kern Amount="-25pt"?>    
    </div>
    

    这是因为如果父元素已经有子节点输出,则无法将属性添加到父元素。

    如果打算尝试将属性添加到父级,但如果不创建跨度标记,则可以将匹配处理指令的模板格式更改为:

    <xsl:template match="processing-instruction('Pub')">
        <xsl:choose>
            <xsl:when test="not(parent::*) or preceding-sibling::node()">
                <span>
                    <!-- Add attribute -->
                </span>
            </xsl:when>
            <xsl:otherwise>
                <!-- Add attribute -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

    并且属性的添加可以在模板中完成,避免重复编码。试试这个 XSLT:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:strip-space elements="*" />
    
        <xsl:template match="processing-instruction('Pub')">
            <xsl:choose>
                <xsl:when test="not(parent::*) or preceding-sibling::node()">
                    <span>
                        <xsl:call-template name="processing-instruction"/>
                    </span>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="processing-instruction"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="processing-instruction">
            <xsl:choose>
                <xsl:when test="starts-with(., '_kern')">
                    <xsl:attribute name="style"><xsl:text>padding-left: </xsl:text>
                    <xsl:value-of select="if (contains(.,'Amount')) then (substring-before(substring-after(., 'Amount=&quot;'), '&quot;')) else '12pt'"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 谢谢@Tim C,但没有跨度我仍然收到此错误:“无法在包含元素的子元素之后创建属性节点(样式)。”我确实在样式表的顶部有&lt;xsl:strip-space elements="*" /&gt;
    • 感谢@Tim C,这对我有用。感谢您花时间纠正我的错误。我还有其他处理指令,为了相关性我省略了它们,但我将它们添加到处理指令模板中。
    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多