2014-02-13 62 views
1

我有以下XML。解析XSLT中的处理指令

<sec xmlns:xlink="http://www.w3.org/1999/xlink" id="fm2" sec-type="other"> 
    <title /> 
    <?SAE page="ii"?> 
    <p><bold>Other SAE books of interest:</bold></p> 
    <p><bold>Electric and Hybrid-Electric Vehicles</bold></p> 
    <p>Edited by Ronald K. Jurgen</p> 
    <p>(Product Code: PT-143.SET)</p> 
    <p><bold>Diesel Emissions and Their Control</bold></p> 
    <p>By Magdi K. Khair and W. Addy Majewski</p> 
    <p>(Product Code: R-303)</p> 
    <p><bold>Hybrid Powered Vehicles, Second Edition</bold></p> 
    <p>By John M. German</p> 
    <p>(Product Code: T-125)</p> 
    <p>For more information or to order a book, contact SAE International at</p> 
    <p>400 Commonwealth Drive, Warrendale, PA 15096-0001, USA;</p> 
    <p>phone 877-606-7323 (U.S. and Canada only) or 724-776-4970 (outside U.S. and Canada);</p> 
    <p>fax 724-776-0790;</p> 
    <p>email <email>[email protected]</email>;</p> 
    <p>website <uri xlink:href="http://books.sae.org">http://books.sae.org</uri>.</p> 
</sec> 

我必须为它创建XSLT。我这样做:

<xsl:for-each select="book-front/sec"> 
    <xsl:value-of select="title"/> 
    <xsl:value-of select="p"/> 
</xsl:for-each>  

但它不解析任何东西。

主要问题是解析<?SAE page="ii"?>它不允许其进一步的属性解析。我不知道如何解析它,然后它的相应XSLT会是什么。

我的XML是

<sec id="ch1.4"> 
<label><bold>1.4</bold></label> 
<title><bold>Energy Consumption of Commercial Vehicles</bold></title> 
<p>Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B;<xref ref-type="bibr" rid="R3">1.3</xref>&#x005D;. According to the U.S. Department of Energy, 72&#x0025; <?SAE page="8"?>of the U.S. petroleum consumption is for transportation. Commercial vehicles consumed up to 18.7&#x0025; of the total energy consumption in transportation in the United States. In other words, commercial vehicles in the United States alone consumed over 3&#x0025; of the global petroleum production in 2009 &#x005B;<xref ref-type="bibr" rid="R3">1.3</xref>, <xref ref-type="bibr" rid="R34">1.34</xref>&#x005D;.</p></sec> 

将我们的代码后,我得到了

Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B; 

但我想

**Energy Consumption of Commercial Vehicles** 
Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B;1.3 &#x005D;. According to the U.S. Department of Energy, 72&#x0025;Page 8 of the U.S. petroleum consumption is for transportation. Commercial vehicles consumed up to 18.7&#x0025; of the total energy consumption in transportation in the United States. In other words, commercial vehicles in the United States alone consumed over 3&#x0025; of the global petroleum production in 2009 &#x005B; 

现在解释如何得到这个输出?我希望现在我的问题对你更容易理解。

+0

,这只是一个处理指令,可能是你没有在你的根节点 –

回答

1

您可以使用

<xsl:template match="processing-instruction('SAE')"> 
    <xsl:value-of select="." /> 
</xsl:template> 

匹配名为SAE处理指令。 (和<xsl:apply-templates select="processing-instruction('SAE')" />,自然)

该指令的值不是真正的XML,即使它在这种情况下看起来像常规属性。

处理指令实际上只包含纯文本,因为它们可能是任何东西 - 从简单的信息位到非XSLT语言的完整程序。

你只能得到指令的内容,在你的情况下字符串'page="ii"',然后你必须手动解析。

你可以做它的XPath这样的:

<xsl:template match="processing-instruction('SAE')"> 
    <xsl:variable name="start-token">page="</xsl:variable> 
    <xsl:variable name="end-token">"</xsl:variable> 
    <xsl:variable name="temp" select="substring-after(., $start-token)" /> 

    <xsl:value-of select="substring-before($temp, $end-token)" /> 
    <!-- output: "ii" --> 
</xsl:template> 
+0

谢谢。这是一个很大的帮助。请你也告诉我如何解析xref标签?因为在我的情况下,找到xref标签的地方,它不会写出写在xref标签后面的内容。我希望我的问题对您来说是可以理解的,或者我应该更详细地解释一下吗? – user

+0

您可能想尝试首先搜索。其他人在你之前也会遇到同样的问题,我相信你会找到解释它的例子。 – Tomalak

0

如果你想简单的复制全部内容,只要做到以下几点:

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

这就是所谓的身份模板。之后,创建模板匹配以覆盖复制的节点。在外部参照的情况下,你可以尝试。

<xsl:template match="xref"> 
    <a href="{@rid}"> 
     <xsl:apply-templates/> 
    </a> 
</xsl:template> 
+0

M. Lamsen空间声明我已经在我的根加入这个命名空间标签。但仍然不工作..我的根标记是 user

+0

我试着在这里运行你的代码看起来没问题。你能编辑你的帖子来添加一些关于你的样式表的更多信息吗? –

+0

当我试图运行此代码,在我的情况下,具有粗体值的p标签现在不解析。只解析简单的p标签。但我想解析这个p标签,并且想要在输出中加粗内容。怎么做 ?并且我希望在写入时按顺序输出。 – user