簡體   English   中英

如何在XSLT中獲取沒有子節點的文本?

[英]How to get text without child nodes in XSLT?

我有以下XML

<title>Products</title>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="products.xsd">
  <product>
    The product called <name>Lorem</name> is available for <price>€ 80</price> only.
  </product>
</products>

現在,我要格式化上面的XML。 例如, name應為紅色, price應為藍色。

我嘗試了以下方法:

<xsl:for-each select="products/product">
      <div>
        <p style="font-weight:bold">
          <xsl:value-of select="text()"/>
          <span style="color:red"><xsl:value-of select="name"/></span>
          <span style="color:blue"><xsl:value-of select="price"/></span>
        </p>
      </div>
</xsl:for-each>

但這根本不起作用...

我也想格式化標題,但是我不確定如何獲取XSLT文件中的內容。

我建議您按照以下方式嘗試另一種方法:

<xsl:template match="product">
    <div>
        <p style="font-weight:bold">
            <xsl:apply-templates/>
        </p>
    </div>
</xsl:template>

<xsl:template match="name">
    <span style="color:red">
        <xsl:apply-templates/>
    </span>
</xsl:template> 

<xsl:template match="price">
    <span style="color:blue">
        <xsl:apply-templates/>
    </span>
</xsl:template>

請注意,這是不完整的-但是您提供的代碼也是如此。

我也想格式化標題

以相同的方式,添加匹配title的模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM