简体   繁体   English

如何在XSLT中获取没有子节点的文本?

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

I have the following XML 我有以下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>

Now I want to format the XML above. 现在,我要格式化上面的XML。 The name should have, for instance, a red color and the price should be blue. 例如, name应为红色, price应为蓝色。

I have tried the following: 我尝试了以下方法:

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

But this doesn't work at all... 但这根本不起作用...

Also I want to format the title but I am not sure how to get the content in the XSLT file. 我也想格式化标题,但是我不确定如何获取XSLT文件中的内容。

I suggest you try a different approach, along the lines of: 我建议您按照以下方式尝试另一种方法:

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

Note that this is incomplete - but so is the the code you have provided. 请注意,这是不完整的-但是您提供的代码也是如此。

Also I want to format the title 我也想格式化标题

In the same manner, add a template matching title . 以相同的方式,添加匹配title的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM