xslt 相关问题

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

使用 XSLT 删除除最后一个之外的所有子节点

我有一个这样的XML文件: 我有一个这样的 XML 文件: <a> <b> <e /> <f /> </b> <c> <g> <j /> </g> <g> <j /> </g> <g> <j /> </g> </c> <d> <h> <i /> </h> <h> <i /> </h> <h> <i /> </h> </d> </a> 我想做的是应用XSL转换来only获取c和d的last节点(包括它们的子节点)以及文件的其余部分,结果是: <a> <b> <e /> <f /> </b> <c> <g> <j /> </g> </c> <d> <h> <i /> </h> </d> </a> 我对 XSLT 没有经验,非常感谢任何帮助。 通常最好从身份转换开始并添加例外,然后有时会在例外中添加例外。 在此转换中,第一个模板是恒等转换,第二个模板跳过 <c> 和 <d> 的子级,第三个覆盖该排除以包含每个 <c> 和 <d> 标签的最后一个子级。 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="c/*|d/*"/> <xsl:template match="c/*[last()]|d/*[last()]"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 我必须修改您的输入 xml 以删除一些空格。根据 规范(第 3.1 节开始标签、结束标签和空元素标签),构造 <x/ > 并不真正有效。 正如评论中所指出的,这可以变得更短,除了身份之外仅使用一个模板。我无法让 [not(last()] 工作,但这个较短的模板可以: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="c/*[position() &lt; last()]|d/*[position() &lt; last()]"/> </xsl:stylesheet> 并且情况可能会有改善。 哪个更好当然是口味问题。我发现我原来的回答稍微清晰一些。 谓语: select="*[not(last())]" 行不通!!!!!! 因为过滤器组会错误地输出位置零: not(123) == 0 select=*[ 0 ] 因为零位置不存在 最后它会抛出错误, 这个错误将拒绝所有谓词!!!

回答 2 投票 0

需要帮助使用 xslt 列出数组中的值

需要帮助使用 xslt 列出数组中的值。目前,它们显示为一行一行,需要值才能在数组中显示它们。 附件是源 xml 和 xslt 尝试过但不确定...

回答 1 投票 0

在 Schematron 中访问对序列

我正在尝试构建一个 Schematron,对 HTML/XML 文档中的段落进行非常简单的检查以查找“禁止术语”,并建议相应的“首选术语”。 示例文档片段: 一个 我正在尝试构建一个 Schematron,对 HTML/XML 文档中的段落进行非常简单的检查以查找“禁用术语”,并建议相应的“首选术语”。 示例文档片段: <p>A sentence containing the word Autobahn.</p> <p>A sentence containing the word client.</p> <p>A sentence containing the word newbie.</p> 我的 Schematron 模式如下所示: <sch:pattern id="termChecker"> <sch:let name="termPairs" value="( ('Autobahn', 'highway'), ('client', 'customer'), ('newbie', 'Beginner') )" /> <sch:rule context="//p"> <sch:let name="foundForbidden" value="(for $pair in $termPairs return if (contains(current(), $pair[1])) then $pair[1] else ())[1]" /> <sch:let name="foundRecommended" value="(for $pair in $termPairs return if ($pair[1] = $foundForbidden) then $pair[2] else ())[1]" /> <sch:report role="info" test="$foundForbidden"> Warning: This text contains the forbidden word “<sch:value-of select='$foundForbidden'/>”. Alternative: “<sch:value-of select='$foundRecommended'/>”. </sch:report> </sch:rule> </sch:pattern> 它“几乎”有效,我收到三个消息: Info: This text contains the forbidden word “Autobahn”. Alternative: “”. Info: This text contains the forbidden word “client”. Alternative: “”. Info: This text contains the forbidden word “Newbie”. Alternative: “”. 因此,它完美地找到了“禁止术语”并在消息中返回它们(<sch:value-of select='$foundForbidden'/>)。 但是,“首选术语”(<sch:value-of select='$foundRecommended'/>) 始终保持为空。 我还尝试了几种替代方法,例如使用索引访问: <sch:value-of select="(for $i in 1 to count($termPairs) return if (contains(current(), $termPairs[$i][1])) then $termPairs[$i][2] else ())[1]"/> 或者这个: <sch:value-of select="(for $pair in $termPairs return if (contains(current(), $pair[1])) then $pair[2] else ())[1]"/> 但我就是无法让它运行。 我在 oXygen (23.1) 中进行了尝试,其中 ISO Schematron 配置为支持 XSLT2(也测试了 XSLT3),并且还测试了配置为支持 XSLT 2 的 Schematron 1.5(也测试了 XSLT 3.1)。 我还尝试使用 ph-schematron 和 ph-schematron-xslt 模块,但结果完全相同。 由于某种原因, $termPairs 的结构作为对的序列,以及我访问这些对的方式 ($pair[1], $pair[2]) 仅适用于 $pair[1],但不适用于 $pair[2]。 有人有想法吗? 如果您想使用“平坦”序列,但认为它是成对构建的,则可以执行以下操作: <schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"> <pattern> <let name="termPairs" value="( ('Autobahn', 'highway'), ('client', 'customer'), ('newbie', 'Beginner') )" /> <rule context="p"> <let name="foundForbidden" value="$termPairs[position() mod 2 = 1][contains(current(), .)]"/> <report role="info" test="exists($foundForbidden)"> Warning: This text "<value-of select="."/>" contains the forbidden words “<value-of select='$foundForbidden'/>”. Alternatives: “<value-of select='for $pos in ($foundForbidden ! index-of($termPairs, .) + 1) return $termPairs[$pos]'/>”. </report> </rule> </pattern> </schema>

回答 1 投票 0

从主 XSLT 映射调用多个 XSLT 映射

我们可以从主 XSLT 映射中调用多个 XSLT 映射吗?基于条件匹配? 示例 XML: ...

回答 1 投票 0

从 XSLT 查找 JSON 对象中的键值对

我有一个 XSLT 实现,它按照乳胶模式对字符串进行连字符,即在字符串中插入软连字符。我这样称呼这个函数: 我有一个 XSLT 实现,它按照乳胶模式对字符串进行连字符,即在字符串中插入软连字符。我这样称呼这个函数: <xsl:template match="p[lang('en')]/text()"> <xsl:analyze-string select="." regex="\w+"> <xsl:matching-substring> <xsl:value-of select="fn:hyphenate(., '­', 'en')"/> <xsl:non-matching-substring> <xsl:value-of select="."/> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:template> 现在,在调用 hyphenate 函数之前,我想查阅异常列表。我对获取异常的格式相当灵活,但我也想利用这个机会学习如何利用 XSLT 3.0 中的 json 对象。 假设我有一个名为 exceptions.json 的 JSON 文件,如下所示: { "en" :{ "recognizance": "re-cog-ni-zance", "reformation": "ref-or-ma-tion", "retribution": "ret-ri-bu-tion", "table": "ta-ble" }, "de" : { //etc. } } (在上面的示例中我没有使用软连字符,因为它们不会显示在 Stackoverflow 上,但这与我提出的问题无关)。 如何将 JSON 文件的内容读取(并解析?)到名为 $exceptions 的变量中,以便我可以最有效地执行以下操作: <xsl:template match="p[lang('en')]/text()"> <xsl:analyze-string select="." regex="\w+"> <xsl:matching-substring> <xsl:choose> <xsl:when test=""> <!--test if matched string is a key in my json object and return the corresponding value. for instance, if the matched substring here is "table", I'd like to return something like <xsl:value-of select="$exceptions['en']['table']"/> , i.e. "ta-ble", using whatever notation may be correct for this kind of thing. --> </xsl:when> <xsl:otherwise> <xsl:value-of select="fn:hyphenate(., '­', 'en')"/> </xsl:otherwise> </xsl:choose> <xsl:non-matching-substring> <xsl:value-of select="."/> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:template> 提前非常感谢! 嗯,XPath 3.1 和 XSLT 3.0 规范已在线,因此您可以找到必要的函数和符号。 使用例如<xsl:variable name="exceptions" select="json-doc('exceptions.json')"/>,您可以访问,例如map:contains($exceptions?en, .) 检查当前单词是否在 en 地图中。 示例:(内联读取 JSON 以实现自包含性): <?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" xmlns:map="http://www.w3.org/2005/xpath-functions/map" exclude-result-prefixes="#all" expand-text="yes"> <xsl:output method="html" indent="yes" html-version="5"/> <xsl:template match="p[lang('en')]/text()"> <xsl:analyze-string select="." regex="\w+"> <xsl:matching-substring> <xsl:choose> <xsl:when test="map:contains($exceptions?en, .)"> <xsl:sequence select="$exceptions?en(.)"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:matching-substring> <xsl:non-matching-substring> <xsl:value-of select="."/> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:template> <xsl:mode on-no-match="shallow-copy"/> <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:param name="json-exceptions" as="xs:string" expand-text="no"> { "en" :{ "recognizance": "re-cog-ni-zance", "reformation": "ref-or-ma-tion", "retribution": "ret-ri-bu-tion", "table": "ta-ble" } } </xsl:param> <xsl:param name="exceptions" select="parse-json($json-exceptions)"/> </xsl:stylesheet> 在线小提琴.

回答 1 投票 0

统计XSLT中分组后的元素数量

我们有 XML: 我们有 XML: <?xml version="1.0" encoding="utf-8"?> <items> <element name="Name1" value="Text1"/> <element name="Name1" value="Text3"/> <element name="Name1" value="Text4"/> <element name="Name3" value="Text5"/> <element name="Name3" value="Text8"/> </items> 我们需要按名称对元素进行分组并计算它们的数量。 输出 XML: <?xml version="1.0"?> <goods> <group name="Name1" sum="3"> <item>Text1</item> <item>Text3</item> <item>Text4</item> </group> </group> <group name="Name3" sum="2"> <item>Text5</item> <item>Text8</item> </group> </goods> 我使用蒙克分组。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="PC" match="element" use="@name"/> <xsl:template match="items"> <goods> <xsl:apply-templates select="element[generate-id(.) = generate-id(key('PC',@name))]"/> </goods> </xsl:template> <xsl:template match="element"> <group name="{@name}"> <xsl:for-each select="key('PC',@name)"> <item> <xsl:value-of select="@value"/> </item> </xsl:for-each> </group> </xsl:template> </xsl:stylesheet> 但不知道如何统计分组后的元素个数。 只需在 count() 上使用 key()。 示例(将 count 属性添加到 group)... <xsl:template match="element"> <group name="{@name}" count="{count(key('PC',@name))}"> <xsl:for-each select="key('PC',@name)"> <item> <xsl:value-of select="@value"/> </item> </xsl:for-each> </group> </xsl:template>

回答 1 投票 0

如何使用 xslt 1.0 根据 xml 元素的子元素值拆分分组

在 XSL 文档中,我需要根据图像元素的值对它们进行分组: size=full 的元素应该单独出现在图像集中。 size=half 的元素应该在图像集中...... 成对出现,如果...

回答 1 投票 0

Framework 4.7.2 和 .NET 7 的不同行为

这是我的xslt转换代码 使用 (var stringReader = new StringReader(xmlString)) 使用 (var xmlReader = XmlReader.Create(stringReader)) { 使用 (var memoryStream = new MemoryStream())...

回答 1 投票 0

如果我只想在 <deleted> = false 时获取数据,那么 API 中应该使用什么 xpath 或查询参数?

这是 REST API:https://{ip}:{port}/fedsvc/objectstores 以下是 REST API 响应。 This is the REST API: https://{ip}:{port}/fedsvc/objectstores Below is the REST API response. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Objects> <Object> <Id>objct040</Id> <Name>longevity-large</Name> <status>Starting</status> <deleted>false</deleted> </Object> <Object> <Id>obs1040</Id> <Name>SRS-large</Name> <status>Starting</status> <deleted>true</deleted> </Object> </Objects> what should be the xpath or query-parametr in API to use if I want to get the data only if <deleted> = false. ? 我希望只提取以下值: objct040 长寿-大 开始 假 X路径: /Objects/Object[deleted='false']/*

回答 1 投票 0

String-join() 函数受 XML 结构(节点)顺序的影响

我通常广泛使用 String-join() 函数来处理和连接来自数据库的值,但是目前我观察到该函数的第二个变量是否首先获取/存在...

回答 1 投票 0

我在数据中得到像<p>这样的特殊字符,并且需要像行一样格式化

我已将数据组合到一个元素中,但获取带有特殊字符的数据。例如:在下面的 xml 中,我在字段中得到类似 。 这…… 我已将数据组合到一个元素中,但获取带有特殊字符的数据。例如:在下面的 xml 中,我在字段中获取类似 &lt;/p>&lt;p>&lt;span 的值:<PSQ2>。 这应该如下所示: 需要知道出现这种情况的原因。在上一步中,我使用 XSLT 将所有值合并为一个。 PSQ2的期望值: 超出工作时间以满足发布时间表和其他优先事项。 就技能而言,我觉得他技术非常好。举止方面,他非常友善,让人觉得他是家庭成员之一。他以个人和专业的方式为人们提供建议。到目前为止我没觉得他有三心二意。 他表现出了良好的分析问题、做出正确决策和克服问题的能力。始终努力提高代码质量。 输入XML: <?xml version='1.0' encoding='UTF-8'?> <wd:Report_Entry xmlns:wd="urn:com.workday/bsvc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions" xmlns:xdiff="urn:com.workday/esb/xdiff"> <empid>123</empid> <name>Ramu</name> <title>Software Architect</title> <manager>Ahmed</manager> <tempno>2</tempno> <MRQ2>&lt;p>Excellent technical knowledge, problem solving capabilities, team player&lt;/p></MRQ2> <DRQ2/> <PSQ2>&lt;p>Going beyond working hours to meet release timeline and other priority stuff.&lt;/p>&lt;p>Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn&amp;#39;t feel that he keeps double thoughts.&lt;/p>&lt;p>&lt;span>He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. &lt;/span>&lt;/p></PSQ2> <MRQ3>&lt;p>Evangelize the design thinking, groom junior devs, create technical vision&lt;/p></MRQ3> <DRQ3/> <PSQ3>&lt;p>Keep your momentum and work towards making Conga Platform UI a great success.&lt;/p>&lt;p>I think he should learn how to be visible till the highest level. Like, he does many better things, however, I feel his visibility is not that much. Credits which should come to him are somehow going to somebody else. So he should learn how to present what he has done in such a way so that all deserved credits goes to him, everything that he does are acknowledged by higher management. This can be possible through better communication and presentation skills.&lt;/p>&lt;p>He can learn and try out new technology and different tools which can provide alternative to solve a problem in the most ideal way.&lt;/p></PSQ3> <MRQ4>9</MRQ4> <DRQ4_Min/> <DRQ4_Max/> <DRQ4_Median/> <PSQ4_Min>8</PSQ4_Min> <PSQ4_Max>9</PSQ4_Max> <PSQ4_Median>8.0</PSQ4_Median> <MRQ6>9</MRQ6> <DRQ6_Min/> <DRQ6_Max/> <DRQ6_Median/> <PSQ6_Min>6</PSQ6_Min> <PSQ6_Max>8</PSQ6_Max> <PSQ6_Median>8.0</PSQ6_Median> </wd:Report_Entry> 为了获得上面的 xml,我使用下面的 XSLT 来组合数据。我不确定我在这个 XSLT 中是否做错了: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions" xmlns:xdiff="urn:com.workday/esb/xdiff" xmlns:wd="urn:com.workday/bsvc"> <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> <xsl:template match="/"> <Report_Data> <xsl:apply-templates select="wd:Report_Data"/> </Report_Data> </xsl:template> <xsl:template match="wd:Report_Data"> <xsl:for-each-group select="wd:Report_Entry" group-by="wd:empid"> <Report_Entry> <empid> <xsl:value-of select="wd:empid"/> </empid> <name> <xsl:value-of select="wd:name"/> </name> <title> <xsl:value-of select="wd:title"/> </title> <manager> <xsl:value-of select="wd:manager"/> </manager> <tempno> <xsl:value-of select="wd:tempno"/> </tempno> <MRQ2> <xsl:for-each select="current-group()[wd:category = 'Manager' and wd:questionno='2' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </MRQ2> <DRQ2> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='2' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </DRQ2> <PSQ2> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='2' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </PSQ2> <MRQ3> <xsl:for-each select="current-group()[wd:category = 'Manager' and wd:questionno='3' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </MRQ3> <DRQ3> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='3' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </DRQ3> <PSQ3> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='3' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </PSQ3> <MRQ4> <xsl:for-each select="current-group()[wd:category = 'Manager' and wd:questionno='4' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </MRQ4> <DRQ4_Min> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </DRQ4_Min> <DRQ4_Max> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </DRQ4_Max> <DRQ4_Median> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:variable name="pos" select="position()"/> <xsl:variable name="length" select="last()"/> <xsl:if test="$pos = floor($length div 2) + 1"> <xsl:value-of select="format-number(sum(wd:response),'#.###')"/> </xsl:if> </xsl:for-each> </DRQ4_Median> <PSQ4_Min> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </PSQ4_Min> <PSQ4_Max> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </PSQ4_Max> <PSQ4_Median> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='4']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:variable name="pos" select="position()"/> <xsl:variable name="length" select="last()"/> <xsl:if test="$pos = floor($length div 2) + 1"> <xsl:value-of select="format-number(sum(wd:response),'#.###')"/> </xsl:if> </xsl:for-each> </PSQ4_Median> <MRQ6> <xsl:for-each select="current-group()[wd:category = 'Manager' and wd:questionno='6' ]"> <xsl:value-of select="wd:response"/> </xsl:for-each> </MRQ6> <DRQ6_Min> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </DRQ6_Min> <DRQ6_Max> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </DRQ6_Max> <DRQ6_Median> <xsl:for-each select="current-group()[wd:category = 'Direct Report' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:variable name="pos" select="position()"/> <xsl:variable name="length" select="last()"/> <xsl:if test="$pos = floor($length div 2) + 1"> <xsl:value-of select="format-number(sum(wd:response),'#.###')"/> </xsl:if> </xsl:for-each> </DRQ6_Median> <PSQ6_Min> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </PSQ6_Min> <PSQ6_Max> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="wd:response"/> </xsl:if> </xsl:for-each> </PSQ6_Max> <PSQ6_Median> <xsl:for-each select="current-group()[wd:category = 'Peer/Stakeholder' and wd:questionno='6']"> <xsl:sort select="wd:response" data-type="number" order="ascending"/> <xsl:variable name="pos" select="position()"/> <xsl:variable name="length" select="last()"/> <xsl:if test="$pos = floor($length div 2) + 1"> <xsl:value-of select="format-number(sum(wd:response),'#.###')"/> </xsl:if> </xsl:for-each> </PSQ6_Median> </Report_Entry> </xsl:for-each-group> </xsl:template> </xsl:stylesheet> 考虑以下简化示例: XML <PSQ2>&lt;p>Going beyond working hours to meet release timeline and other priority stuff.&lt;/p>&lt;p>Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn&amp;#39;t feel that he keeps double thoughts.&lt;/p>&lt;p>&lt;span>He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. &lt;/span>&lt;/p></PSQ2> XSLT 3.0 <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="PSQ2"> <result> <xsl:value-of select="parse-xml-fragment(.)/p" /> </result> </xsl:template> </xsl:stylesheet> 结果 <?xml version="1.0" encoding="UTF-8"?> <result>Going beyond working hours to meet release timeline and other priority stuff. Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn't feel that he keeps double thoughts. He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. </result> parse-xml-fragment()功能需要支持XSLT 3.0的处理器。如果没有这个,从转义标记中提取文本将变得更加困难。 补充: 将转义标记解析为 XML 后,您可以按照您喜欢的任何方式将其处理为 XML - 例如: <xsl:template match="PSQ2"> <result> <xsl:for-each select="parse-xml-fragment(.)/p"> <xsl:number format="1. "/> <xsl:value-of select="." /> <xsl:text>&#10;</xsl:text> </xsl:for-each> </result> </xsl:template> 会产生: <result>1. Going beyond working hours to meet release timeline and other priority stuff. 2. Skill set wise, I feel he is technically very well sound. Behavior wise, he is very friendly, make people feel like he is one of a family member. He advices people in both personal and professional ways. So far I didn't feel that he keeps double thoughts. 3. He has shown good ability to analyze issues, make sound decisions and overcome problems. Always trying to improve code quality. </result>

回答 1 投票 0

如何使用XSLT将父元素的值复制到多个子节点?

我有一个 XML 文件需要进行转换才能导入到财务系统中。 “位置”在开始时只被引用一次,后面是记录(通常是几百条),我已经

回答 1 投票 0

在 XSLT 中简单创建新元素

我知道这应该很简单,但它不适合我。 我需要翻译 XML: 我知道这应该很简单,但它不适合我。 我需要翻译 XML: <?xml version="1.0" encoding="utf-8"?> <items> <element name="Name1" value="Text1"/> <element name="Name2" value="Text2"/> <element name="Name3" value="Text3"/> </items> 到 XML: <?xml version="1.0"?> <goods> <!-- some text --> <Name1>Text1</Name1> <Name2>Text2</Name1> <Name3>Text3</Name1> </goods> 我创建 XSLT: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" /> <xsl:template match="/"> <xsl:element name="goods"> <xsl:comment> some text </xsl:comment> </xsl:element> </xsl:template> <xsl:template match="/items/element"> <xsl:element name="{@name}"> <xsl:value-of select="@value"/> </xsl:element> </xsl:template> </xsl:stylesheet> 但这是错误的。我可以将模板放在其他模板上吗?或者还有其他解决办法吗? 你可以简单地做: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/items"> <goods> <xsl:comment>some text</xsl:comment> <xsl:for-each select="element"> <xsl:element name="{@name}"> <xsl:value-of select="@value"/> </xsl:element> </xsl:for-each> </goods> </xsl:template> </xsl:stylesheet>

回答 1 投票 0

不允许将多个项目的序列作为 'ne' 的第一个操作数 ("72686200", "72686200")

我试图用第一个字符串上的值替换第二个字符串上的第 13 个值。 726862 应替换为 72686200。该值来自其他元素。有什么办法可以做到这一点...

回答 1 投票 0

使用 Adobe Indesign 的 XSLT 对 XML 子节点进行分组

尝试使用XSLT按类别分组,但我是一名图形设计师,编程能力非常令人沮丧。 所需的输出应省略节点“类别” 谁能帮我解决这个问题...

回答 1 投票 0

xslt - 合并一些元素并对它们进行排序

我有这个xml代码 皇家马德里 罗纳尔多 我有这个 xml 代码 <realtime_result> <team> <team_name>real madrid</team_name> <events> <yellow_card time="28">ronaldo</yellow_card> <yellow_card time="31">ronaldo</yellow_card> <red_card time="39">benzema</red_card> . . </events> </team> <team> <team_name>Barcelona</team_name> <events> <goal time="25">messi</goal> <red_card time="44">messi</red_card> <yellow_card time="63">iniesta</yellow_card> . . </events> </team> </realtime_result> 我需要一个 xslt 代码将其转换为这样的表: <table> <tr> <th>real madrid</th> <th>time</th> <th>barcelona</th> </tr> <tr> <td></td> <td>25</td> <td>goal : messi</td> </tr> <tr> <td>yello card : ronaldo</td> <td>28</td> <td>barcelona</td> </tr> . . <table> 请注意,事件已排序(25、28、31、39、44 ...)。 有人可以帮助我吗? 您的示例输入 XML 无效 - 第一个结束 team 标记丢失,第二个团队的事件具有错误的结束标记。此外,所需的输出有点不清楚 - 第一个事件不显示团队名称,而第二个事件则显示团队名称。我猜团队名称不应该显示,因为它已经在标题中,遵循 XSLT <?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" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="realtime_result"> <table> <tr> <th> <xsl:value-of select="//team[1]/team_name"/> </th> <th>time</th> <th> <xsl:value-of select="//team[2]/team_name"/> </th> </tr> <xsl:apply-templates select="//events/*"> <xsl:sort select="@time"/> </xsl:apply-templates> </table> </xsl:template> <xsl:template match="*"> <tr> <td> <xsl:if test=" parent::events/preceding-sibling::team_name [ normalize-space()='real madrid' ]"> <xsl:value-of select="concat(local-name(),' : ', .)"/> </xsl:if> </td> <td><xsl:value-of select="@time"/></td> <td> <xsl:if test=" parent::events/preceding-sibling::team_name [ normalize-space()='Barcelona' ]"> <xsl:value-of select="concat(local-name(),' : ', .)"/> </xsl:if> </td> </tr> </xsl:template> </xsl:stylesheet> 当应用于更正后的输入 XML 时,会产生输出 <table> <tr> <th>real madrid</th> <th>time</th> <th>Barcelona</th> </tr> <tr> <td></td> <td>25</td> <td>goal : messi</td> </tr> <tr> <td>yellow_card : ronaldo</td> <td>28</td> <td></td> </tr> <tr> <td>yellow_card : ronaldo</td> <td>31</td> <td></td> </tr> <tr> <td>red_card : benzema</td> <td>39</td> <td></td> </tr> <tr> <td></td> <td>44</td> <td>red_card : messi</td> </tr> <tr> <td></td> <td>63</td> <td>yellow_card : iniesta</td> </tr> </table> 将模板应用于事件时,使用 <xsl:sort select="@time"/> 按时间对事件进行排序。生成行的模板使用检查团队名称 <xsl:if test="parent::events/preceding-sibling::team_name [ normalize-space()='Barcelona' ]"> 由于输入 XML 在 team_name 处没有任何空格,因此也可以写为 <xsl:if test="parent::events/preceding-sibling::team_name [ .='Barcelona' ]"> normalize-space()只会删除任何前导或尾随空白,这在检查相等性时有时是必要的。 事件单元格的值仅写入当前事件的团队,并使用 local-name() 检索事件名称。 供参考:xsl:sort、local-name()、normalize-space(),以及可能对 XPath 轴有用的可视化http://www.xmlplease.com/axis

回答 1 投票 0

根据某些条件将元素移动到父元素下

我是 XSLT 新手,因此需要一些有关此问题的帮助 我有以下输入 XML 我是 XSLT 新手,因此需要有关此问题的一些帮助 我有以下输入 XML <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SubmitImportJobRequest xmlns="/cp/bdtbeans"> <User>abc</User> <Client>qwe</Client> <Password>00000</Password> <JobName>CMBX_TEST</JobName> <UserAssets Owner="abc" EffectiveStartDate="2023-04-03"> <CDSwaps> <CDS ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" BARRAID="28881002_8" StartDate="2023-12-22" ExpirationDate="2028-12-22" ContractSize="1" AccrualBasis="ACT/360" PriceCurrency="USD" UnderlierID="US037833EJ59" UnderlierIDType="ISIN" DealSpread="100"/> </CDSwaps> <CMBXs> <CMBX ID="32083535_8" IDType="LOCALID" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" BARRAID="32083535_8" ContractSize="1" RecoveryRate="40" PriceCurrency="USD" StartDate="2022-12-20" ExpirationDate="2028-03-09" UnderlierID="137BENAO4" UnderlierIDType="MIP" DealSpread="50"/> </CMBXs> </UserAssets> </SubmitImportJobRequest> </s:Body> </s:Envelope> 当前输出XML <Logical> <UserInstruments Effective_Start_Date="2023-04-03" Owner="paqa"> <CDSwaps xmlns="/cp/bdtbeans"> <CDS AccrualBasis="ACT/360" Priority="BARRA" BARRAID="28881002_8" ContractSize="1" CouponFrequency="3M" DealSpread="100" ExpirationDate="2028-12-22" ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" PriceCurrency="USD" RecoveryRate="4.0E1" StartDate="2023-12-22" Und_ID="US037833EJ59" Und_IDType="ISIN"/> <CDS Priority="BARRA" BARRAID="32083535_8" ContractSize="1" DealSpread="50" ExpirationDate="2028-03-09" ID="32083535_8" IDType="LOCALID" IsCMBX="true" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" PriceCurrency="USD" RecoveryRate="40" StartDate="2022-12-20" Und_ID="137BENAO4" Und_IDType="MIP"/> </CDSwaps> </UserInstruments> </Logical> 所需输出 <Logical> <UserInstruments Effective_Start_Date="2023-04-03" Owner="paqa"> <CDSwaps xmlns="/cp/bdtbeans"> <CDS AccrualBasis="ACT/360" Priority="BARRA" BARRAID="28881002_8" ContractSize="1" CouponFrequency="3M" DealSpread="100" ExpirationDate="2028-12-22" ID="28881002_8" IDType="LOCALID" Name="Buy Protection on Apple Inc" PriceCurrency="USD" RecoveryRate="4.0E1" StartDate="2023-12-22" Und_ID="US037833EJ59" Und_IDType="ISIN"/> <CDS Priority="BARRA" BARRAID="32083535_8" ContractSize="1" DealSpread="50" ExpirationDate="2028-03-09" ID="32083535_8" IDType="LOCALID" IsCMBX="true" Name="Sell Protection on CMBX.NA.AAA Indices Series 15 V1" PriceCurrency="USD" RecoveryRate="40" StartDate="2022-12-20" Und_ID="137BENAO4" Und_IDType="MIP"/> </CDSwaps> </UserInstruments> </Logical> 用于此转换的 XSLT <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bdt="/cp/bdtbeans"> <xsl:template match="node() | @*" > <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <xsl:template match="bdt:CDSwaps"> <xsl:copy> <xsl:apply-templates select="node() | @*" /> <xsl:apply-templates select="//bdt:CMBX"/> </xsl:copy> </xsl:template> <xsl:template match="bdt:CMBXs"/> <xsl:template match="bdt:CMBX"> <xsl:element name="CDS"> <xsl:apply-templates select="node() | @*"/> </xsl:element> </xsl:template> </xsl:stylesheet> 如何在 XSLT 中应用条件,以便仅当输入 XML 中存在 CDSwaps 和 CMBX 标签时才发生所有这些复制和重命名 CMBX 元素的语句? 将此模板添加到您的 xslt: <xsl:template match="/*[not(.//bdt:CDSwaps and .//bdt:CMBXs)]"/>

回答 1 投票 0

XSLT 将现有 !ENTITY 引用放入 XML <!DOCTYPE > 中

我已经查看了现有的问题/答案,但没有一个问题/答案完全相同,或者没有给出答案。 我有一个现有的 XML 文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE thing PUBLIC "XXX//DTD XML DTD for things//EN" "C:\work\thing.dtd" [<!ENTITY abreve "&#x103;"><!ENTITY agr "&#945;"><!ENTITY amacr "&#x101;"><!ENTITY bgr "&#946;">]> <thing> [...] </thing> 我使用此 XSLT 以另一种不相关的方式更改文档,效果很好: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/asset" priority="10"> [...] <output method="xml" omit-xml-declaration="no" indent="no" encoding="UTF-8" doctype-public="XXX//DTD XML DTD for things//EN" doctype-system="C:\work\thing.dtd"/> [...] 但是输出没有给出 ENTITY 项,只是这样: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE thing PUBLIC "XXX//DTD XML DTD for things//EN" "C:\work\thing.dtd"> <thing> [...] </thing> 如果需要,解决方案可以从文件中读取 ENTITY 内容,但不是必须这样做,因为它在所有实例中都是相同的,因此它可以重新提供 ENTITY 内容。 谢谢。 恐怕 XSLT 不能很好地与 DTD 配合使用。标准 XSLT 无法读取输入文档的 DTD 或写入输出文档的 DTD,也无法阻止输入中的实体和字符引用被扩展(从而丢失)。有一些技巧可以解决其中一些限制,但没有一个非常令人满意。

回答 1 投票 0

如何限制使用 XSLT 拉入文本文件的文本?

我正在使用 XSLT 脚本将 HTML 文件中的内容提取到 .txt 文件中。在本例中,它将文本拉入 我使用 XSLT 脚本将 HTML 文件中的内容提取到 .txt 文件中。在本例中,它会拉出 <li property="ktp:answer" 中的文本以及 <section class="ktp-explanation jasper-exclude" property="ktp:explanation" typeof="ktp:Explanation"> 中的文本。我只想将文本拉到<li>中。我怎样才能做到这一点? 这是 HTML 输入的片段: <li property="ktp:answer" typeof="ktp:Answer">Encourage the client to increase fluid intake. <section class="ktp-explanation jasper-exclude" property="ktp:explanation" typeof="ktp:Explanation"> <section class="ktp-explanation-section jasper-exclude" data-title="Feedback" property="ktp:explanation-section" typeof="ktp:feedback"> <p>While increasing fluid intake is important for both prevention and treatment of urinary tract infections (UTIs), it is not a priority over obtaining assessment data to confirm the suspected UTI. Fluids may be encouraged by the nurse after bacteria are determined to be present in the urine. </p> </section> </section> </li> 这是我需要修改的 XSLT 脚本部分: <xsl:when test="$colname = 'Answers'"> <xsl:for-each select="../../../xhtml:ol[contains(@class, 'ktp-answer-set')]/xhtml:li[@property = 'ktp:answer']"> <xsl:value-of select="concat(' (', string(position()), ') ', normalize-space(.), '|')" /> </xsl:for-each> </xsl:when> 我的输出如下所示: (1) 鼓励服务对象增加液体摄入量。虽然增加液体摄入量对于预防和治疗尿路感染 (UTI) 很重要,但它并不比获取评估数据来确认可疑的 UTI 更重要。在确定尿液中存在细菌后,护士可能会鼓励补充液体。 我只需要这样: (1) 鼓励服务对象增加液体摄入量。 感谢您的帮助! 尝试更改以下中的 normalize-space(): <xsl:value-of select="concat(' (', string(position()), ') ', normalize-space(.), '|')" /> 至: <xsl:value-of select="concat(' (', string(position()), ') ', normalize-space(text()[1]), '|')" /> 因此,您仅标准化第一个直接子节点的空间,而不是上下文中的整个元素。

回答 1 投票 0

回答 1 投票 0

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