如何读取节点 XSLT 的文本值



如何读取节点/元素的值,忽略了很少的子标签。我有需要忽略的标签列表,

例:

一) 输出:标题 Txt a

<Title>
     <Comment>Comment code</Comment>Title Txt a
 </Title>  

b) 输出:标题 Txt b

 <Title>
     <Ignore1>Comment code</Ignore1>Title Txt b
 </Title> 

c) 输出:注释代码标题 Txt c

 <Title>
     <includethis>Comment code</includethis>Title Txt c
 </Title>

您只需匹配Title元素:

<xsl:template match="Title">

并输出其文本内容:

<xsl:value-of select="."/>

然后,依次处理子节点:

<xsl:template match="*[parent::Title and starts-with(.,'Ignore')]"/>
<xsl:template match="includethis">
  <xsl:value-of select="."/>
</xsl:template>

上面,第一个模板匹配名称以"忽略"开头的元素。这是因为我认为还有其他名为Ignore2Ignore3等元素。

最后,匹配includethis元素并输出其文本内容,与Title元素相同。

现在,总结一下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Title">
  <xsl:value-of select="."/>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[parent::Title and starts-with(.,'Ignore')]"/>
<xsl:template match="includethis">
  <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>

感谢您的帮助。但我没有提到引发问题的重要条件,

在开始任何处理之前,我需要检查"标题"是否为空。

以下示例标记被视为空,当子标记来自忽略列表中的子标记时。

<Title>
 <Comment>Comment code</Comment>
</Title> 

示例代码 :

<xsl:choose>
  <xsl:when test="Title and normalize-space(Title) != ''">
   <xsl:apply-templates select="Title" mode="xyz"/>                    
  </xsl:when>
  <xsl:otherwise>
    <xsl:call-templates name="getalternative_label" mode="xyz"/>
  </xsl:otherwise>
</xsl:choose>

最新更新