xslt 相关问题

XSLT是XML的一种转换语言,旨在将结构化文档转换为其他格式(如XML,HTML和纯文本,或者在XSLT 3,JSON中)。问题应该根据需要使用xslt-1.0,xslt-2.0或xslt-3.0标记之一。

XPATH 选择包含具有特定 href 的锚点的范围

我正在尝试编写一个 XPath 语句,该语句选择具有给定类的跨度,该类包含具有给定 href 的锚点。这就是我现在所拥有的: //span[.[contains(@class, 'versiontext')]/a[conta...

回答 1 投票 0

什么 XSLT 允许我跳过包含具有特定 href 值的 <li> 标签的 <a> 标签?

这是我上一个问题的后续问题。 我无法完全弄清楚 XSLT 来执行以下操作。我有一些带有一个或多个 标签的 HTML。 标签可能包含 标签。 ... 这是我之前的问题的后续问题。 我无法完全计算出 XSLT 来执行以下操作。我有一些带有一个或多个 <ul> 标签的 HTML。 <li> 标签可能包含 <a> 标签。如果任何 <li> 标签包含 href 符合特定模式的锚点,我想删除它。 示例: <ul> <li><a href="/some/old/path">One</a></li> <li><a href="/other/old/path">Two</a></li> <li><a href="/some/older/path">Three</a></li> <li><a href="/other/older/path">Four</a></li> </ul> 我希望删除 <li> 包含 href 的 older 行,因此结果将是: <ul> <li><a href="/some/old/path">One</a></li> <li><a href="/other/old/path">Two</a></li> </ul> 我希望删除的行可以按任何顺序排列,并分散在多个 <ul> 标签中。如果我最终得到一个空的 <ul></ul> 对,我很好(但如果可以轻松删除这样产生的空列表,那就加分了)。 <li> 不包含锚点或包含不匹配锚点的标签应保持原样。 我接近了以下几点: <xsl:template match="li/a[contains(@href, 'older')]"> </xsl:template> 但这留下了开口<li>: <ul> <li><a href="/some/old/path">One</a></li> <li><a href="/other/old/path">Two</a></li> <li> <li> </ul> 如何摆脱整条<li>线? 这是我正在使用的完整 HTML: <html> <head> <!-- lots of stuff I don't care about --> </head> <body> <div> <!-- lots of stuff I don't care about --> <div> <!-- lots of stuff I don't care about --> <div id="key_div"> <div id="ignore_this"> <!-- lots of stuff I don't care about --> </div> <p>More junk I don't want</p> <p>Even more junk I don't want</p> <h2><span class="someClass" id="someID">Header</span></h2> <p>Stuff I want to keep</p> <!-- A lot of stuff I want to keep --> <p>More stuff I want to keep</p> <ul> <li><a href="/some/old/path">One</a></li> <li><a href="/some/old/other">Two</a></li> <li><a href="/some/older/path">Three</a></li> <li><a href="/some/older/other">Four</a></li> </ul> <ul> <li>Leave this as-is</li> </ul> </div> <!-- lots of stuff I don't care about --> </div> <!-- lots of stuff I don't care about --> </div> </body> </html> 这是 XSLT: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="utf-8"/> <xsl:template match="/html"> <html> <head> <title></title> </head> <body> <xsl:apply-templates select="//div[@id='key_div']/h2"/> </body> </html> </xsl:template> <xsl:template match="h2"> <h1> <xsl:value-of select="." /> </h1> <xsl:apply-templates select="following-sibling::*"/> </xsl:template> <!-- My failed attempt to remove certain li lines --> <xsl:template match="li/a[contains(@href, 'older')]"> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 我目前的结果: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> </head> <body> <h1>Header</h1> <p>Stuff I want to keep</p> <p>More stuff I want to keep</p> <ul> <li><a href="/some/old/path">One</a></li> <li><a href="/some/old/other">Two</a></li> <li> <li> </ul> <ul> <li>Leave this as-is</li> </ul> </body> </html> 我只需要弄清楚如何删除匹配的 href 的完整 <li> 行。 与: match="li/a[contains(@href, 'older')]" 您正在选择 a 元素。 尝试将其更改为: match="li[contains(a/@href, 'older')]" (未经测试,老实说,我什至没有查看完整的 XSLT。)

回答 1 投票 0

需要什么 XSLT 来提取和转换这个特定的 XHTML?

我正在尝试从较大的文件中提取一些 HTML 的子集,然后对结果执行一些转换。我已经取得了一些进展,但我缺少一两部分来使这项工作成为设计......

回答 1 投票 0

使用插件org.dita.pdf2进行转换时表格单元格中没有空格的换行问题

要将 dita 格式转换为 pdf 文件,我使用 dita-ot v.3.7.4 以及 org.dita.pdf2 插件。面临文本超出表格单元格的问题,如果此文本不包含

回答 1 投票 0

XSLT 映射以删除标头段

我正在尝试使用 XSLT 1.0 从我的输入 xml 中删除标头段,但无法获得所需的输出。请在这里帮助我。 输入 我尝试使用 XSLT 1.0 从输入 xml 中删除标头段,但无法获得所需的输出。请在这里帮助我。 输入 <?xml version="1.0" encoding="UTF-8"?> <HEADER> <CHILD1 BEGIN="1"> <EDI SEGMENT="1"> <FIELD1>EDI</FIELD1> <FIELD2>2</FIELD2> <FIELD3>HEADER</FIELD3> </EDI> </CHILD1> </HEADER> 输出(无 XML 声明和 HEADER 段) <CHILD1 BEGIN="1"> <EDI SEGMENT="1"> <FIELD1>EDI</FIELD1> <FIELD2>2</FIELD2> <FIELD3>HEADER</FIELD3> </EDI> </CHILD1> 我试过的代码 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="HEADER"> <CHILD BEGIN="1"> <xsl:copy-of select="."/> </CHILD> </xsl:template> </xsl:stylesheet> 使用 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/HEADER"> <xsl:copy-of select="*"/> </xsl:template> </xsl:stylesheet> 对于转换的序列化结果中没有XML声明的情况,可以声明<xsl:output method="xml" omit-xml-declaration="yes"/>。

回答 1 投票 0

XSLT 映射到输入文件中的正确 XML 格式

我正在检查使用 XSLT 1.0 映射更正输入 XML 文件语法的可能性,但找不到任何相关博客,这可能吗? 输入是多个文件的组合...

回答 1 投票 0

如何检测丢失的文件而不抛出错误

我提到了xsl中的xml文件路径,但我想暂时删除文件夹中的那个xml文件,并且我总是想保留在xsl中。但是当我删除它时显示警告。 我提到了xsl中的xml文件路径,但我想暂时删除文件夹中的那个xml文件,并且我总是想保留在xsl中。但是当我删除它时显示警告。 <xsl:apply-templates mode="replaceVariables" select="document('../Folder_1/Test.xml')"> </xsl:apply-templates> 我从Test.xml中删除了Folder_1。当我运行时,出现以下错误 Engine name: Saxon-PE 9.9.1.7 Severity: warning Description: I/O error reported by XML parser processing file:/C:/Work/Folder_1/Test.xml: C:/Work/Folder_1/Test.xml (The system cannot find the file specified) Start location: 674:48 Length: 1 即使 xml 文件在相应的提到的文件夹中删除,它在转换时也不应该显示错误。 在 XSLT 2.0 及更高版本中,有 XPath 2.0 及更高版本的函数 doc-available 例如doc-available('../Folder_1/Test.xml'),因此您可以在 xsl:if 周围使用 xsl:apply-templates 检查,或者您甚至可以在 select 表达式的谓词中使用该检查。

回答 1 投票 0

文件夹文件不可用,正在经历错误

我提到了xsl中的xml文件路径,但我想暂时删除文件夹中的那个xml文件,并且我总是想保留在xsl中。但是当我删除它时显示警告。 我提到了xsl中的xml文件路径,但我想暂时删除文件夹中的那个xml文件,并且我总是想保留在xsl中。但是当我删除它时显示警告。 <xsl:apply-templates mode="replaceVariables" select="document('../Folder_1/Test.xml')"> </xsl:apply-templates> 我从Test.xml中删除了Folder_1。当我运行时,出现以下错误 Engine name: Saxon-PE 9.9.1.7 Severity: warning Description: I/O error reported by XML parser processing file:/C:/Work/Folder_1/Test.xml: C:/Work/Folder_1/Test.xml (The system cannot find the file specified) Start location: 674:48 Length: 1 即使 xml 文件在相应的提到的文件夹中删除,它在转换时也不应该显示错误。 在 XSLT 2.0 及更高版本中,有 XPath 2.0 及更高版本的函数 doc-available 例如doc-available('../Folder_1/Test.xml'),因此您可以在 xsl:if 周围使用 xsl:apply-templates 检查,或者您甚至可以在 select 表达式的谓词中使用该检查。

回答 1 投票 0

计算输入xml中唯一字段的数量

我想获取以下 xml 中唯一的“id”字段。 测试配置文件订单 11 ...

回答 1 投票 0

使用 XSLT 转换将 XML 记录转换为 JSON - 数字格式问题

我正在开发一个项目,使用存储过程从数据库中获取数据。数据以 XML 格式返回,如下所示: 8f7e-25fbcc...

回答 1 投票 0

使用保留字符(<) in JS with XSLT

我需要改编一个旧的 XSLT 文件,将 XML 文件转换为 HTML/JS。它不再起作用了。 以下是 XSLT 文件中生成的原始 JS 代码: 函数 processVisibility(项目,可见){...

回答 1 投票 0

如何在 XSLT 3.0 中运行 XQuery?

我正在尝试找到一种在 XSLT 中运行 XQuery 的方法,因为我有一个大型 XQuery 逻辑库,我一直在使用它来丰富 Oracle 数据库中的 XML 有效负载,但现在我需要迁移......

回答 1 投票 0

XSLT 对独特项目进行排序

我需要对一些 XML 进行排序并获取 HTML。 我的 XML: 我需要对一些 XML 进行排序并获取 HTML。 我的 XML: <?xml version="1.0" encoding="utf-8" ?> <global> <item university="Harvard" staff="Matt Dillon" course="American History"/> <item university="Harvard" staff="Matt Dillon" course="Philosophy"/> <item university="Harvard" staff="Matt Dillon" course="Social Psychology"/> <item university="Harvard" staff="Simon Pegg" course="Government"/> <item university="Harvard" staff="Simon Pegg" course="Sociobiology"/> <item university="Harvard" staff="Christian Bale" course="Astronomy"/> <item university="Harvard" staff="Christian Bale" course="Economics"/> <item university="Stanford" staff="Jean Reno" course="Chemistry"/> <item university="Stanford" staff="Jean Reno" course="Natural Sciences"/> <item university="Stanford" staff="Jean Reno" course="Atmospheric Chemistry"/> <item university="Stanford" staff="Simon Pegg" course="Social Psychology"/> <item university="Stanford" staff="Simon Pegg" course="Political Philosophy"/> <item university="Stanford" staff="Adrien Brody" course="Mathematics"/> <item university="Stanford" staff="Adrien Brody" course="Economics"/> <item university="Stanford" staff="Adrien Brody" course="Computer Science"/> <item university="Stanford" staff="Adrien Brody" course="Astrophysics"/> </global> 我创建了一个 XSLT,其中应用了两次 Munch 排序: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:key name="key_univ" match="/global/item" use="@university"/> <xsl:key name="key_staff" match="/global/item" use="@staff"/> <xsl:template match="global"> <html> <body> <ul> <xsl:apply-templates select="item"/> </ul> </body> </html> </xsl:template> <xsl:template match="item"> <xsl:variable name="key1" select="key('key_univ',@university)"/> <xsl:if test="generate-id(.)=generate-id($key1)"> <li><h3><xsl:value-of select="@university"/></h3></li> <p>Count: <xsl:value-of select="count($key1[@course])"/></p> <ul> <xsl:for-each select="$key1"> <xsl:variable name="key2" select="key('key_staff',@staff)"/> <xsl:if test="generate-id(.)=generate-id($key2)"> <li><h4><xsl:value-of select="@staff"/></h4></li> <p>Count: <xsl:value-of select="count($key2[@course])"/></p> <ul> <xsl:for-each select="$key2"> <li><xsl:value-of select="@course"/></li> </xsl:for-each> </ul> </xsl:if> </xsl:for-each> </ul> </xsl:if> </xsl:template> 代码有效并且结果良好。但我不明白为什么在排序时,西蒙·佩吉最终两次进入哈佛大学,尽管他应该出现在哈佛大学一次和斯坦福大学一次。 我认为我们需要在某处注册一个局部变量。但什么也没想到。请帮我解决这个问题。 以下是 XSLT 2 或更高版本中的嵌套分组如何使这变得简单: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all"> <xsl:output method="html" indent="yes" version="5.0"/> <xsl:template match="global"> <html> <head> <title>nested grouping example</title> </head> <body> <ul> <xsl:for-each-group select="item" group-by="@university"> <li><h3><xsl:value-of select="@university"/></h3></li> <p>Count: <xsl:value-of select="count(current-group())"/></p> <ul> <xsl:for-each-group select="current-group()" group-by="@staff"> <li><h4><xsl:value-of select="@staff"/></h4></li> <p>Count: <xsl:value-of select="count(current-group())"/></p> <ul> <xsl:for-each select="current-group()"> <li><xsl:value-of select="@course"/></li> </xsl:for-each> </ul> </xsl:for-each-group> </ul> </xsl:for-each-group> </ul> </body> </html> </xsl:template> </xsl:stylesheet> 通过 XSLT 1 中的两个级别键和 Muenchian 分组,您可以做到 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:key name="key_univ" match="/global/item" use="@university"/> <xsl:key name="key_staff" match="/global/item" use="concat(@university, '|', @staff)"/> <xsl:template match="global"> <html> <body> <ul> <xsl:apply-templates select="item[generate-id() = generate-id(key('key_univ', @university)[1])]"/> </ul> </body> </html> </xsl:template> <xsl:template match="item"> <li><h3><xsl:value-of select="@university"/></h3></li> <p>Count: <xsl:value-of select="count(key('key_univ', @university))"/></p> <ul> <xsl:for-each select="key('key_univ', @university)[generate-id() = generate-id(key('key_staff', concat(@university, '|', @staff))[1])]"> <li><h4><xsl:value-of select="@staff"/></h4></li> <p>Count: <xsl:value-of select="count(key('key_staff', concat(@university, '|', @staff)))"/></p> <ul> <xsl:for-each select="key('key_staff', concat(@university, '|', @staff))"> <li><xsl:value-of select="@course"/></li> </xsl:for-each> </ul> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet>

回答 1 投票 0

如何在xslt从html到xml的转换中将文本和某个元素的元素节点分成两部分而不丢失标记?

我有以下 html 标记。 这是斜体标签。该文本属于 x 元素。这里的文本应该 2 属于 y 元素。 我有以下 html 标记。 <p> <i>This is italic tag</i>. This text belongs to x element. The text from here should <sub>2</sub> belong to y element. </p> 这是我正在使用的模板规则: `<xsl:template match="p/text()[not(parent::i)]"> <xsl:variable name="tokens" select="tokenize(.,'\.')"/> <xsl:element name="x"> <xsl:value-of select="normalize-space($tokens[2])"/> </xsl:element> <xsl:element name="y"> <xsl:value-of select="normalize-space(string-join($tokens[position() > 2 and position() != last()], '.'))"/> </xsl:element> </xsl:template>` `<xsl:template match="sub"> <xsl:element name="SUBSCRIPT"> <xsl:apply-templates/> </xsl:element> </xsl:template>` `<xsl:template match="p"> <xsl:element name="PARA"> <xsl:apply-templates/> </xsl:element> </xsl:template>` `<xsl:template match="i"> <xsl:element name="EMPHASIS"> <xsl:apply-templates/> </xsl:element> </xsl:template>` 运行转换时,输出中缺少一些文本。 这就是我现在得到的: `<PARA> <EMPHASIS>This is italic tag</EMPHASIS> <X>This text belongs to x element</X> <Y/> <SUBSCRIPT>2</SUBSCRIPT> <X/> <Y/> </PARA>` 我的期待: `<PARA> <EMPHASIS>This is italic tag</EMPHASIS> <X>This text belongs to x element</X> <Y>The text from here should <SUBSCRIPT>2</SUBSCRIPT> belong to y element</Y> </PARA>` 如何确保在分割p标签的文本节点及其元素节点(除i子元素之外的子元素节点)时不会出现内容丢失且子元素也能正确转换? 解决方法如下: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" expand-text="yes"> <xsl:output method="html" indent="no" html-version="5"/> <xsl:mode on-no-match="shallow-copy"/> <xsl:mode name="markers" on-no-match="shallow-copy"/> <xsl:mode name="analyze"/> <xsl:template match="p"> <xsl:variable name="markers" as="element(p)"> <xsl:apply-templates select="." mode="markers"/> </xsl:variable> <xsl:apply-templates select="$markers" mode="wrap"/> </xsl:template> <xsl:template mode="markers" match="p//text()"> <xsl:apply-templates select="analyze-string(., '\.')" mode="analyze"/> </xsl:template> <xsl:template mode="analyze" match="*:match"> <dot/> </xsl:template> <xsl:template mode="wrap" match="p"> <PARA> <xsl:for-each-group select="node()" group-ending-with="dot"> <xsl:choose> <xsl:when test="position() = 2"> <x> <xsl:apply-templates select="current-group()"/> </x> </xsl:when> <xsl:when test="position() = 3"> <y> <xsl:apply-templates select="current-group()"/> </y> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </PARA> </xsl:template> <xsl:template match="dot"/> <xsl:template match="i"> <EMPHASIS> <xsl:apply-templates/> </EMPHASIS> </xsl:template> <xsl:template match="sub"> <SUBSCRIPT> <xsl:apply-templates/> </SUBSCRIPT> </xsl:template> <xsl:template match="/" name="xsl:initial-template"> <xsl:copy> <xsl:apply-templates/> <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} at {current-dateTime()}</xsl:comment> </xsl:copy> </xsl:template> </xsl:stylesheet> 在线小提琴.

回答 1 投票 0

如何将表格从 XML 匹配到 XSL

我想用正确的 XMl 标签填充表...我已经用标签编写了代码,但不知何故,当我将 XML 和 XSl 进行转换时,它没有用数据填充表...

回答 1 投票 0

创建新 XML 结构时出现 XSLT 代码问题

我正在尝试为以下要求编写XSLT,其中 /parent/queryCompoundEmployeeResponse/CompoundEmployee/person/person_id_external = /parent/row/PERSON_ID_EXTERNAL 然后 /paren 的内容...

回答 1 投票 0

XSLT 为输入 XML 中的每个增量分隔符位置分配变量

对于输入 XML: +本地_职能+小组_职能+小组_所有者+小组_经理+小组_领导+临时_服务 对于输入 XML: <EmpJob> <EmpJob> <ConcatDepartment>+Local_Functions+Group_Functions+Group_Owners+Group_Managers+Group_Leads+Interim_Services</ConcatDepartment> </EmpJob> </EmpJob> 我希望将值分配给 XSLT 中的变量,如下所示。分隔符是 +,它应该在将值分配给 XSLT 中的每个部门变量时递增,以便我可以使用它们获得以下预期输出.. <xsl:value-of select="$departments[1]" /> should return the string "" <xsl:value-of select="$departments[2]" /> should return the string "Local_Functions" <xsl:value-of select="$departments[3]" /> should return the string "Local_Functions/Group_Functions" <xsl:value-of select="$departments[4]" /> should return the string "Local_Functions/Group_Functions/Group_Owners" <xsl:value-of select="$departments[5]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers" <xsl:value-of select="$departments[6]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads" <xsl:value-of select="$departments[7]" /> should return the string "Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads/Interim_Services" <xsl:value-of select="$departments[8]" /> should return the string "" <xsl:value-of select="$departments[9]" /> should return the string "" <xsl:value-of select="$departments[10]" /> should return the string "" 我尝试过 <xsl:variable name="departments" select="tokenize(/EmpJob/EmpJob/ConcatDepartment, '\+')"/> <xsl:value-of select="$departments[1]" /> 返回字符串“” <xsl:value-of select="$departments[2]" /> 返回字符串“Local_Functions” <xsl:value-of select="$departments[3]" /> 返回字符串“Group_Functions” <xsl:value-of select="$departments[4]" /> 返回字符串“Group_Owners”等等...XSLT 的任何帮助都将非常有帮助 要获得您想要的行为,请使用如下表达式: <xsl:value-of select="$departments[position() le 4]" separator="/"/> 返回字符串: /Local_Functions/Group_Functions/Group_Owners 注意表达式: <xsl:value-of select="$departments[position() le 10]" separator="/"/> 将返回: /Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads/Interim_Services 而不是空字符串。我不确定应该使用什么逻辑来产生您显示的确切结果。

回答 1 投票 0

XSLT 为输入 XML 中的每个分隔符位置分配变量

在我的 XSLT 中,我需要将值分配给名为 FODepartment_LX 的变量,其中 X 表示从最左侧分隔符开始的分隔符 (+) 的位置,最大位置为 10。如果...

回答 1 投票 0

带递归的 XSLT 转换

我需要将 XML 文档转换为 HTML。 输入 XML: 大家好! 我需要将 XML 文档转换为 HTML。 输入XML: <items> <item no="1" par_no="0" user="Mike">Hi all!</item> <item no="2" par_no="1" user="Jane">Hi...</item> <item no="3" par_no="2" user="Pit"> says: wasap man?</item> <item no="4" par_no="2" user="Karol">are you ok?</item> <item no="5" par_no="4" user="Nick">I think he's not in the mood.</item> <item no="6" par_no="5" user="Karol">I just ask a question.</item> <item no="7" par_no="1" user="Pit">says: how are you?</item> <item no="8" par_no="7" user="Mike">fine!</item> </items> 输出 HTML: <?xml version="1.0" encoding="UTF-8"?> <ul> <li><b>Mike</b> says: Hi all! <ul> <li><b>Jane</b> says: Hi... <li><b>Pit</b> says: wasap man? <li><b>Karol</b> says: are you ok? <ul> <li><b>Nick</b> says: I tihnk he's not in the mood. <ul> <li><b>Karol</b> says: I just ask a question. </ul> </li> </ul> </li> </ul> </li> <li><b>Pit</b> says: how are you? <ul> <li><b>Mike</b> says: fine! </ul> </li> </ul> </li> </ul> 我试图解决这个问题。我得到了代码。正在对行进行排序,但我无法获取附件。我的 XSL: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="items"> <html> <ul> <xsl:apply-templates select="item[@par_no=0]"/> </ul> </html> </xsl:template> <xsl:template match="item"> <xsl:variable name="varNo" select="@no"/> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> </li> <xsl:apply-templates select="//items/item[@par_no=$varNo]"> <xsl:sort select="@par_no" data-type="number"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet> 我不知道在哪里插入标签才能得到这样的嵌套结构。请帮忙! 在 XSLT 3 中,您可以按如下方式解决: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all"> <xsl:output method="html" version="5.0" indent="yes"/> <xsl:template match="/"> <html> <head> <title>Test</title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:key name="child" match="item" use="@par_no"/> <xsl:template match="items"> <ul> <xsl:apply-templates select="key('child', '0')"/> </ul> </xsl:template> <xsl:template match="item"> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <xsl:where-populated> <ul> <xsl:apply-templates select="key('child', @no)"/> </ul> </xsl:where-populated> </li> </xsl:template> </xsl:stylesheet> 密钥和模板在以前的版本中的工作方式相同,您需要将 xsl:where-populated 替换为检查 key('child', @no) 是否选择元素以输出 ul: <xsl:template match="item"> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <xsl:variable name="children" select="key('child', @no)"/> <xsl:if test="$children"> <ul> <xsl:apply-templates select="$children"/> </ul> </xsl:if> </li> </xsl:template> 我认为你可以简单地做(仅使用 XSLT 1.0): <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:key name="chld" match="item" use="@par_no" /> <xsl:template match="/items"> <ul> <xsl:apply-templates select="item[@par_no='0']"/> </ul> </xsl:template> <xsl:template match="item"> <li> <b> <xsl:value-of select="@user"/> </b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <ul> <xsl:apply-templates select="key('chld', @no)"/> </ul> </li> </xsl:template> </xsl:stylesheet> 浏览器中显示的结果是: 是的,我致力于 XSLT 1.0。但我没有得到这个结果。在我的 Chrome 浏览器中,结果有所不同。不幸的是我无法发布图片(( 我需要像你的照片一样的结果! HTML: <ul> <li><b>Mike</b> says: Hi all! <ul> <li> <b>Jane</b> says: Hi... <ul> <li><b>Pit</b> says: asap man?</li> <li><b>Karol</b> says: are you ok? <ul> <li><b>Nick</b> says: I think he's not in the mood. <ul> <li><b>Karol</b> says: I just ask a question.</li> </ul> </li> </ul> </li> </ul> </li> <li><b>Pit</b> says: how are you? <ul> <li><b>Mike</b> says: fine!</li> </ul> </li> </ul> </li> </ul> 为什么说 Pit 会重复两次?

回答 3 投票 0

XSLT - 将单个属性分离为具有属性的多个元素

我有一些源 XML,其中包含对一个属性内多个值的交叉引用。我需要将它们分成具有属性值的自己不同的元素。 输入 XML: B... 我有一些源 XML,其中包含对一个属性内多个值的交叉引用。我需要将它们分成具有属性值的自己不同的元素。 输入XML: <p>Blah blah blah <xref ref-type="bibr" rid="ref004 ref005 ref006 ref007">4&#x2013;7</xref>.</p> 所需的输出 XML: <p>Blah blah blah <xref ref-type="bibr" rid="ref004">4</xref><xref ref-type="bibr" rid="ref005"/><xref ref-type="bibr" rid="ref006"/>&#x2013;<xref ref-type="bibr" rid="ref007">7</xref>.</p> 注意中间的 2 个交叉引用变成了空元素。 我有以下 XSLT,但它没有给我正确的输出 - 它将属性值作为元素内容插入。 <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="xref"> <xsl:variable name="rid" select="@rid"/> <xsl:choose> <xsl:when test="contains($rid, ' ')"> <!-- Multiple references --> <xsl:variable name="refs" select="tokenize($rid, ' ')"/> <xsl:for-each select="$refs"> <xsl:if test="position() > 1"> <xsl:text> </xsl:text> </xsl:if> <xsl:variable name="refNum" select="substring-after(., 'ref')"/> <xsl:element name="xref"> <xsl:attribute name="ref-type">bibr</xsl:attribute> <xsl:attribute name="rid"> <xsl:value-of select="concat('ref', $refNum)"/> </xsl:attribute> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> </xsl:when> <xsl:otherwise> <!-- Single reference --> <xsl:element name="xref"> <xsl:attribute name="ref-type">bibr</xsl:attribute> <xsl:attribute name="rid"> <xsl:value-of select="$rid"/> </xsl:attribute> <xsl:value-of select="."/> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:template> 我想我通过使用得到了预期的结果: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="xref[contains(@rid, ' ')]"> <xsl:variable name="ref" select="text()"/> <xsl:for-each select="tokenize(@rid, ' ')"> <xsl:choose> <xsl:when test="position()=1"> <xref ref-type="bibr" rid="{.}"> <xsl:value-of select="substring-before($ref, '&#x2013;')"/> </xref> </xsl:when> <xsl:when test="position()=last()"> <xsl:text>&#x2013;</xsl:text> <xref ref-type="bibr" rid="{.}"> <xsl:value-of select="substring-after($ref, '&#x2013;')"/> </xref> </xsl:when> <xsl:otherwise> <xsl:text> </xsl:text> <xref ref-type="bibr" rid="{.}"/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet> 但这主要是基于根据您的单个示例猜测规则是什么......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.