您可以檢查處理指令通過增加一個簡單的條件
<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="'), '"')) 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="'), '"')) 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>
這是因爲e如果父節點已經有子節點輸出,則不能向父節點添加屬性。
如果目的是試圖將屬性添加到父那裏就可以了,但如果不創建span標記代替,那麼你可以更改模板的處理指令到該匹配的格式爲:
<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="'), '"')) 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,但我仍然得到這個沒有跨度的錯誤:「一個屬性節點(樣式)不能在包含元素的子元素之後創建。」我在樣式表的頂部有''。 –
Caroline
2014-10-01 19:00:35
謝謝@Tim C,這對我有用。我感謝您花時間糾正我的錯誤。我還有其他處理指令,我忽略了它們的相關性,但我將它們添加到處理指令模板中。 – Caroline 2014-10-03 14:55:48