Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找xslt中同时包含子元素和字符串的元素_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

查找xslt中同时包含子元素和字符串的元素

查找xslt中同时包含子元素和字符串的元素,xslt,xslt-1.0,xslt-2.0,Xslt,Xslt 1.0,Xslt 2.0,我的消息来源是: <content> <caption>text 1</caption> <element1>Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text <bold>file</bold> is a <a>file</a> type

我的消息来源是:

<content>
  <caption>text 1</caption>
  <element1>Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text <bold>file</bold> is a <a>file</a> type typically identified by the .txt file name extension.</element1>
  <section1>
     <element2>Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text file is a file type typically identified by the .txt file name extension.</element2>
   </section1>
 </content>

文本1
记事本是一个基本的文本编辑程序,它最常用于查看或编辑文本文件。文本文件是通常由.txt文件扩展名标识的文件类型。
记事本是一个基本的文本编辑程序,它最常用于查看或编辑文本文件。文本文件是通常由.txt文件扩展名标识的文件类型。
我试图为既有子元素(字符元素)又有文本的元素(可能是任何元素)以及只有文本的元素提取并创建唯一的ID。
元素不应分开

  <caption id="id1">Text 1</caption>
  <element1 id="id2">Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text <bold>file</bold> is a <a>file</a> type typically identified by the .txt file name extension.</element1>
  <element2 id="id3">Notepad....</element2>
Text 1
记事本是一个基本的文本编辑程序,它最常用于查看或编辑文本文件。文本文件是通常由.txt文件扩展名标识的文件类型。
便条簿。。。。

如果您有任何想法,我将不胜感激……

我不太确定您是想保留层次结构,还是想输出您所描述的那些元素的平面列表;下面简单地将所描述的元素提取为一个平面列表(尽管保留了它们的内容),
id
s由XSLT处理器生成:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*[not(*) and text()[normalize-space()]] | *[* and text()[normalize-space()]]">
  <xsl:copy>
    <xsl:attribute name="id" select="generate-id()"/>
    <xsl:apply-templates select="@* , node()" mode="copy"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="copy">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当应用于输入样本时,Saxon 9输出

<?xml version="1.0" encoding="UTF-8"?>
<caption id="d1e2">text 1</caption>
<element1 id="d1e4">Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text <bold>file</bold> is a <a>file</a> type typically identified by the .txt file name extension.</element1>
<element2 id="d1e13">Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text file is a file type typically identified by the .txt file name extension.</element2>

文本1
记事本是一个基本的文本编辑程序,它最常用于查看或编辑文本文件。文本文件是通常由.txt文件扩展名标识的文件类型。
记事本是一个基本的文本编辑程序,它最常用于查看或编辑文本文件。文本文件是通常由.txt文件扩展名标识的文件类型。

输出格式中的和在哪里?是否要跳过它们?是的,我只想提取包含字符串/字符串+子(字符)元素的元素。。不需要考虑只有子元素但没有PCDATA的元素。示例中的caption和element2元素没有子元素,但仍然具有id属性。这似乎与您所说的要从既有子元素又有文本的元素创建ID相矛盾。这是正确的吗?谢谢它用于包含子元素和文本的两个元素。也适用于只有文本的元素。所以我给了标题和元素2Hi@Martin一个ID,谢谢你的代码。实际上,子元素应该是粗体或a以外的任何元素。如何处理?请详细解释如何识别需要
id
属性的元素和不需要属性的元素。我看不出
粗体
a
元素与
标题
元素(都是纯文本内容)之间有什么区别,除了通过名称区分它们。嗨@Martin。是的,你是对的。但在本例中,caption的父元素没有PCDATA,因此它只包含子元素。这些元素可以删除,id应该包含在子ie中。但是在
元素中有一个父元素,它包含PCDATA(字符串)因此,在父元素中,id可以不包含在子元素中。我认为这有点令人困惑。因此,一旦父元素或祖先元素被转换为
id
属性值,子元素和子元素(如
bold
a
元素)就可以简单地复制,无需查找元素即可获得
id
s?对吗?