0

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

4

2 回答 2

0

这很有效,如果没有@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>

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

于 2014-10-01T19:23:22.423 回答
0

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

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

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

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

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

例如,这会出错

<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>

编辑:响应您的评论,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>
于 2014-10-01T17:21:36.177 回答