xslt 相关问题

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

如何生成唯一字符串

我想将 XML 文档转换为 HTML。某些 XML 元素具有指向其他文档的链接,例如: 在 HTML 输出中,我想得到: 我想将 XML 文档转换为 HTML。某些 XML 元素具有指向其他文档的链接,例如: <link href="1.html"> 在 HTML 输出中,我想得到: <a href="1.html&no_cache={unique_id}"> 如何生成这个独特的相当大的ID? 首先,我假设由于某种未知的原因,您无法使用link中的绝对URL作为所需的UID——这是最简单、最自然的解决方案。 如果我的假设是正确的,那么: 对于 XSLT 来说这是一项简单的任务。 因为OP希望多次转换时生成的id相同,所以不适合使用generate-id()函数。 这是生成稳定 id 的一种简单方法: <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="link[@href]"> <xsl:variable name="vUid"> <xsl:number level="any" count="link[@href]"/> </xsl:variable> <a href="{@href}&amp;no_cache={{{$vUid}}}"/> </xsl:template> </xsl:stylesheet> 当此转换应用于以下 XML 文档时(无论多少次): <t> <link href="1.html"/> <a> <link href="2.html"/> <b> <link href="3.html"/> <c> <link href="4.html"/> </c> <link href="5.html"/> </b> <link href="6.html"/> <d> <link href="7.html"/> </d> </a> <link href="8.html"/> <e> <link href="9.html"/> </e> <link href="10.html"/> </t> 每次都会产生想要的、相同的、正确的结果: <t> <a href="1.html&amp;no_cache={1}"/> <a> <a href="2.html&amp;no_cache={2}"/> <b> <a href="3.html&amp;no_cache={3}"/> <c> <a href="4.html&amp;no_cache={4}"/> </c> <a href="5.html&amp;no_cache={5}"/> </b> <a href="6.html&amp;no_cache={6}"/> <d> <a href="7.html&amp;no_cache={7}"/> </d> </a> <a href="8.html&amp;no_cache={8}"/> <e> <a href="9.html&amp;no_cache={9}"/> </e> <a href="10.html&amp;no_cache={10}"/> </t> 请注意:使用 <xsl:number> 来生成 id。 如果同一个链接可以在文档中出现多次,并且我们需要所有出现都使用相同的 id,这里是此问题的解决方案: <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:key name="kHrefByVal" match="link/@href" use="."/> <xsl:variable name="vUniqHrefs" select= "//link/@href [generate-id() = generate-id(key('kHrefByVal',.)[1]) ] "/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="link[@href]"> <xsl:variable name="vthisHref" select="@href"/> <xsl:variable name="vUid"> <xsl:for-each select="$vUniqHrefs"> <xsl:if test=". = $vthisHref"> <xsl:value-of select="position()"/> </xsl:if> </xsl:for-each> </xsl:variable> <a href="{@href}&amp;no_cache={{{$vUid}}}"/> </xsl:template> </xsl:stylesheet> 当此转换应用于以下 XML 文档时: <t> <link href="1.html"/> <a> <link href="2.html"/> <b> <link href="1.html"/> <c> <link href="3.html"/> </c> <link href="2.html"/> </b> <link href="1.html"/> <d> <link href="3.html"/> </d> </a> <link href="4.html"/> <e> <link href="2.html"/> </e> <link href="4.html"/> </t> 产生了想要的正确结果: <t> <a href="1.html&amp;no_cache={1}"/> <a> <a href="2.html&amp;no_cache={2}"/> <b> <a href="1.html&amp;no_cache={1}"/> <c> <a href="3.html&amp;no_cache={3}"/> </c> <a href="2.html&amp;no_cache={2}"/> </b> <a href="1.html&amp;no_cache={1}"/> <d> <a href="3.html&amp;no_cache={3}"/> </d> </a> <a href="4.html&amp;no_cache={4}"/> <e> <a href="2.html&amp;no_cache={2}"/> </e> <a href="4.html&amp;no_cache={4}"/> </t> 尝试generate-id()。 <xsl:value-of select="generate-id(.)"/> 这里有进一步的解释: XSLT 生成 id() 纯 XSLT 是不可能的,但一些替代选项可能是: 添加扩展命名空间,以便您可以调用非 XSLT 代码:<a href="1.html&no_cache={myns:unique_id()}">。这将为您提供所需的结果,但确实取决于您用于执行转换的框架的支持。 使用 JavaScript 将唯一 ID 添加到客户端的链接中。仅当您的客户端启用了 JavaScript 时才有效,但如果您知道会出现这种情况,则可能是可接受的折衷方案。 在页面上设置 HTTP 标头以防止缓存。从语义的角度来看,这可能是最好的选择,并且您不会冒搜索引擎使用每个唯一 ID 重复抓取您的页面的风险。 XSLT 是一种函数式语言,这意味着对于给定的输入,它将始终产生相同的输出,因此根据定义,guid 方法或任何其他随机生成器不会成为设计规范的一部分。如果您受客户端限制,最好的选择是使用与时间相关的方法作为生成 id 的伪随机种子的一部分,但是由于您的目标似乎是强大的脱缓存,因此您应该放弃这种方法,而只专注于应用正确的方法您要保护的资源的反缓存标头。

回答 4 投票 0

如何为 Apache FOP 目录中的每个图像创建页面序列

假设我有一个包含 n 张图像的目录。我想每页添加一张图像。 例如 : 在名为“Images”的目录中,我有以下两张图像: newpage0.jpg 和 newpage1.jpg 我...

回答 1 投票 0

XSLT - 根据兄弟节点的值检索节点

我有以下示例 XML: 我有以下示例 XML: <?xml version="1.0"encoding="UTF-8"?> <Root> <Record> <JobProfile> <Status> <Code>Pending</Code> </Status> <ID> <Code>007</Code> </ID> </JobProfile> <JobProfile> <Status> <Code>Active</Code> </Status> <ID> <Code>003</Code> </ID> </JobProfile> <JobProfile> <Status> <Code>Terminated</Code> </Status> <ID> <Code>004</Code> </ID> </JobProfile> </Record> </Root> 我需要得到如下输出: <Root> <Record> <ID>003</ID> </Record> </Root> 逻辑是 - 读取输入 XML,找到 JobProfile/Status/Code =“Active”的节点。找到后,读取兄弟级别的 JobProfile/ID/Code 并将其写入输出中。 尝试了下面的 XSLT 但它不起作用 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/Root/Record"> <ID> <xsl:for-each select="Record/JobProfile"> <xsl:if test="Status/Code='Active'"> <xsl:value-of select="ID/Code"/> </xsl:if> </xsl:for-each> </ID> </xsl:template> </xsl:stylesheet> 使用 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/Root/Record/JobProfile[Status/Code != 'Active']"/> <xsl:template match="/Root/Record/JobProfile[Status/Code = 'Active']"> <ID> <xsl:value-of select="ID/Code"/> </ID> </xsl:template> </xsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <!-- Identity template : copy all text nodes, elements and attributes --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- filter JobProfile which is not Active and all Status elements--> <xsl:template match="JobProfile[not(Status/Code = 'Active')]|Status"/> <!-- skip JobProfile|Code but apply templates to use ID--> <xsl:template match="JobProfile|Code"> <xsl:apply-templates select="@*|node()"/> </xsl:template> </xsl:stylesheet>

回答 2 投票 0

如何通过位置验证值以避免在 xslt 中打印标题?

是 xslt 的新手,我正在使用 xslt 1.0 从 xml 代码创建 PDF。 我的源数据如下。 是 xslt 新手,我正在使用 xslt 1.0 从 xml 代码创建 PDF。 我的源数据如下。 <STATEMENT> <STATEMENT_AGING> <AGING> <AGING_LABEL>Current</AGING_LABEL> <AGING_AMOUNT>$28,927.43</AGING_AMOUNT> </AGING> <AGING> <AGING_LABEL>1 - 30</AGING_LABEL> <AGING_AMOUNT>$0.00</AGING_AMOUNT> </AGING> <AGING> <AGING_LABEL>31 - 60</AGING_LABEL> <AGING_AMOUNT>$0.00</AGING_AMOUNT> </AGING> <AGING> <AGING_LABEL>61 - 90</AGING_LABEL> <AGING_AMOUNT>$0.00</AGING_AMOUNT> </AGING> <AGING> <AGING_LABEL>91 - 120</AGING_LABEL> <AGING_AMOUNT>$0.00</AGING_AMOUNT> </AGING> <AGING> <AGING_LABEL>Over 120</AGING_LABEL> <AGING_AMOUNT>$0.00</AGING_AMOUNT> </AGING> </STATEMENT_AGING> <GROUP ID='1' label=''> <GROUP_LABEL /> <GROUP_NAME>SoleraStatementSection</GROUP_NAME> <GROUP_TYPE>LABEL</GROUP_TYPE> <GROUP_HIDE_FLAG>0</GROUP_HIDE_FLAG> <GROUP_HEADER_ROW> <COL headerAlign='start' headerFormat='text' width='2cm'>Doc. #</COL> <COL headerAlign='start' headerFormat='text' width='1cm'>Doc. Type</COL> <COL headerAlign='start' headerFormat='text' width='1cm'>Doc. Date</COL> <COL headerAlign='start' headerFormat='text' width='1cm'>Due Date</COL> <COL headerAlign='center' headerFormat='text' width='1cm'>Currency</COL> <COL headerAlign='end' headerFormat='currency' width='1cm'>Original Amount</COL> <COL headerAlign='end' headerFormat='currency' width='1cm'>Balance</COL> <COL headerAlign='start' headerFormat='number' width='1cm'>ChildAccount</COL> </GROUP_HEADER_ROW> <GROUP_DATA_ROW> <COL>2120-000023134</COL> <COL>Invoice</COL> <COL>2024-05-13T00:23:14.205918-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$13,247.46</COL> <COL>$13,247.46</COL> <COL>Price Acura</COL> </GROUP_DATA_ROW> <GROUP_DATA_ROW> <COL>2120-000023135</COL> <COL>Invoice</COL> <COL>2024-05-13T00:36:37.008144-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$13,247.37</COL> <COL>$13,247.37</COL> <COL>Price Acura</COL> </GROUP_DATA_ROW> <GROUP_DATA_ROW> <COL>2120-000023136</COL> <COL>Invoice</COL> <COL>2024-05-13T00:51:32.838201-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$2,432.60</COL> <COL>$2,432.60</COL> <COL>Price Honda</COL> </GROUP_DATA_ROW> </GROUP> <GROUP ID='2' label=''> <GROUP_LABEL /> <GROUP_NAME>CreditData</GROUP_NAME> <GROUP_TYPE>LABEL</GROUP_TYPE> <GROUP_HIDE_FLAG>0</GROUP_HIDE_FLAG> <GROUP_HEADER_ROW> <COL headerAlign='start' headerFormat='text' width='0cm'>Credit #</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>Credit Type</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>Credit Date</COL> <COL headerAlign='center' headerFormat='text' width='0cm'>Currency</COL> <COL headerAlign='end' headerFormat='currency' width='0cm'>Credit Amount</COL> <COL headerAlign='end' headerFormat='currency' width='0cm'>Unallocated Amnt</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>ChildAccount</COL> </GROUP_HEADER_ROW> </GROUP> <GROUP ID='3' label=''> <GROUP_LABEL /> <GROUP_NAME>PaymentSection</GROUP_NAME> <GROUP_TYPE>LABEL</GROUP_TYPE> <GROUP_HIDE_FLAG>0</GROUP_HIDE_FLAG> <GROUP_HEADER_ROW> <COL headerAlign='start' headerFormat='text' width='0cm'>Payment #</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>Payment Type</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>Payment Date</COL> <COL headerAlign='center' headerFormat='text' width='0cm'>Currency</COL> <COL headerAlign='end' headerFormat='currency' width='0cm'>Payment Amount</COL> <COL headerAlign='end' headerFormat='currency' width='0cm'>Unallocated Amnt</COL> <COL headerAlign='start' headerFormat='text' width='0cm'>ChildAccount</COL> </GROUP_HEADER_ROW> </GROUP> </STATEMENT> 问题是,在 GROUPID 1 中,我尝试按 custName 或 col[8] 对数据进行分组,但我想在每次 custName 更改时打印标题。我尝试将值存储到变量中,但一旦循环再次出现,该值就会丢失,因此我尝试使用 IF 进行评估时不起作用。 有谁知道我该如何管理这个问题,因为我看到了很多使用 diff 方法的答案,但我不太理解所有这些答案,因为我的源 xml 有“通用”标签而不是真实的标签名称。 PS。我尝试了选择和 if 条件,但它们都不起作用。 <xsl:template name= "DetailSectionG1"> <fo:block space-before="8mm" font-weight="bold" color="{$blue-color}" keep-together.within-page="always" keep-with-next="always"> <fo:table width="100%" border-collapse="collapse" table-layout="fixed"> <fo:table-column column-width="{$left-margin}"/> <fo:table-column column-width="proportional-column-width(1)"/> <fo:table-column column-width="{$right-margin}"/> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block/> </fo:table-cell> <fo:table-cell> <fo:block font-size="{$large-font-size}" padding-top="3 * {$padding}" padding-bottom="{$padding}"> Statement Details </fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </fo:block> <xsl:choose> <xsl:when test="GROUP[@ID=1]/GROUP_DATA_ROW/COL[1] !=''"> <fo:block> <fo:table width="100%" border-collapse="collapse" table-layout="fixed"> <fo:table-column column-width="{$data-left-margin}"/> <fo:table-column column-width="30mm"/> <fo:table-column column-width="25mm"/> <fo:table-column column-width="25mm"/> <fo:table-column column-width="25mm"/> <fo:table-column column-width="22mm"/> <fo:table-column column-width="27mm"/> <fo:table-column column-width="25mm"/> <fo:table-column column-width="{$data-right-margin}"/> <xsl:for-each select="/STATEMENT/GROUP[@ID=1]/GROUP_DATA_ROW[generate-id(.)=generate-id(key('groupDataRow', concat(COL[position() = 8],'+',COL[position() = 1])))]"> <xsl:variable name="vGroup" select="key('kAllFields', concat(COL[position() = 8],'+',COL[position() = 1]))"/> <fo:table-body> <xsl:choose> <xsl:when test="custName != COL[position() = 8] or position() = 1"> <fo:table-row> <fo:table-cell> <fo:block/> </fo:table-cell> <fo:table-cell> <fo:block font-weight="bold" color="{$blue-color}" keep-together.within-page="always" keep-with-next="always" padding-bottom="{$padding}"/> <!--xsl:value-of select="COL[position() = 8]"/--> <!--/fo:block--> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block/> </fo:table-cell> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">1</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">2</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">3</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">4</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">5</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">6</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> <xsl:call-template name="HeaderCell"> <xsl:with-param name="position">7</xsl:with-param> <xsl:with-param name="color" select="$blue-color"/> </xsl:call-template> </fo:table-row> </xsl:when> <xsl:otherwise/> </xsl:choose> <!--xsl:if test="{$acctName} != COL[position() = 8] or position() = 1"> </xsl:if--> <fo:table-row> <fo:table-cell> <fo:block/> </fo:table-cell> <xsl:call-template name="Cell"> <xsl:with-param name="position">1</xsl:with-param> </xsl:call-template> <xsl:call-template name="Cell"> <xsl:with-param name="position">2</xsl:with-param> </xsl:call-template> <fo:table-cell> <fo:block color="{$base-color}" padding-bottom="{$small-padding}"> <xsl:attribute name="end-indent">1mm</xsl:attribute> <xsl:attribute name="text-align"> <xsl:value-of select="../GROUP_HEADER_ROW[1]/COL[position() = 3]/@headerAlign"/> </xsl:attribute> <xsl:call-template name="getDate"> <xsl:with-param name="value" select="COL[position()=3]"/> </xsl:call-template> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block color="{$base-color}" padding-bottom="{$small-padding}"> <xsl:attribute name="end-indent">1mm</xsl:attribute> <xsl:attribute name="text-align"> <xsl:value-of select="../GROUP_HEADER_ROW[1]/COL[position() = 4]/@headerAlign"/> </xsl:attribute> <xsl:call-template name="getDate"> <xsl:with-param name="value" select="COL[position()=4]"/> </xsl:call-template> </fo:block> </fo:table-cell> <xsl:call-template name="Cell"> <xsl:with-param name="position">5</xsl:with-param> </xsl:call-template> <xsl:call-template name="Cell"> <xsl:with-param name="position">6</xsl:with-param> </xsl:call-template> <xsl:call-template name="Cell"> <xsl:with-param name="position">7</xsl:with-param> </xsl:call-template> </fo:table-row> <xsl:variable name="custName"> <xsl:value-of select="COL[position() = 8]"/> </xsl:variable> </fo:table-body> </xsl:for-each> </fo:table> </fo:block> </xsl:when> <xsl:otherwise> <fo:block keep-together.within-page="always" keep-with-next="always"> <fo:table width="100%" border-collapse="collapse" table-layout="fixed"> <fo:table-column column-width="{$left-margin}"/> <fo:table-column column-width="proportional-column-width(1)"/> <fo:table-column column-width="{$right-margin}"/> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block/> </fo:table-cell> <fo:table-cell> <fo:block color="{$base-color}" padding-bottom="{$small-padding}" end-indent="1mm"> No invoices to display </fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </fo:block> </xsl:otherwise> </xsl:choose> </xsl:template> 我期望的 PDF 就是这个。 [期望输出] (https://i.sstatic.net/GPAPwxHQ.png) 目前我尝试的验证不起作用,并且 acctName 和标头表仅显示一次。 电流输出 您似乎在分组方面遇到了困难,请注意,在 XSLT 2 或更高版本中,分组要容易得多,您可以这样做 <xsl:template match="GROUP[@ID = 1]"> <xsl:for-each-group select="GROUP_DATA_ROW" group-by="COL[8]"> <group key="{current-grouping-key()}"> <xsl:sequence select="current-group()"/> </group> </xsl:for-each-group> </xsl:template> 并获得例如 <group key="Price Acura"> <GROUP_DATA_ROW> <COL>2120-000023134</COL> <COL>Invoice</COL> <COL>2024-05-13T00:23:14.205918-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$13,247.46</COL> <COL>$13,247.46</COL> <COL>Price Acura</COL> </GROUP_DATA_ROW> <GROUP_DATA_ROW> <COL>2120-000023135</COL> <COL>Invoice</COL> <COL>2024-05-13T00:36:37.008144-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$13,247.37</COL> <COL>$13,247.37</COL> <COL>Price Acura</COL> </GROUP_DATA_ROW> </group> <group key="Price Honda"> <GROUP_DATA_ROW> <COL>2120-000023136</COL> <COL>Invoice</COL> <COL>2024-05-13T00:51:32.838201-05:00</COL> <COL>2024-06-12T05:00:00.000000+00:00</COL> <COL>USD</COL> <COL>$2,432.60</COL> <COL>$2,432.60</COL> <COL>Price Honda</COL> </GROUP_DATA_ROW> </group> 因此您可以轻松地拥有两个组和每个组的正确数据,我没有尝试输出想要的 XSL-FO,但是一旦您解决了分组问题就应该很容易。 请注意,XSLT 3(XSLT 的当前版本)在多种平台、浏览器 (https://www.saxonica.com/download/javascript.xml) 和 Node.js () 中受支持https://www.npmjs.com/package/saxon-js)您可以使用 SaxonJS 2(2.6 是当前版本),对于 Java,您可以在 Maven 上找到 Saxon HE https://mvnrepository.com/artifact/ net.sf.saxon/Saxon-HE 或在 Github 上,对于 Python,有 SaxonCHE (https://pypi.org/project/saxonche/),对于 .NET,有 Saxon .NET HE (https:/ /www.nuget.org/packages/Saxon-HE,https://www.nuget.org/packages/SaxonHE12s9apiExtensions)。 尽管如此,如果您在受限环境中工作而被迫使用 XSLT 1,请使用密钥 <xsl:key name="group" match="GROUP[@ID = 1]/GROUP_DATA_ROW" use="COL[8]"/> <xsl:template match="GROUP[@ID = 1]"> <xsl:for-each select="GROUP_DATA_ROW[generate-id() = generate-id(key('group', COL[8])[1])]"> <group key="{COL[8]}"> <xsl:copy-of select="key('group', COL[8])"/> </group> </xsl:for-each> </xsl:template>

回答 1 投票 0

如何防止功能 FEATURE_SECURE_PROCESSING 设置为 true 的 TransformerFactory 剥离属性?

我有 2 个 TransformerFactory 实例:一个是默认实例,一个安全处理功能设置为 true。每个都为同一个 XSL 文件生成一个模板。当我对 XML 数据应用转换时...

回答 1 投票 0

XSLT 删除重复的子节点

我有一个源输入 XML,我需要根据两个子节点删除重复记录。我是 XSLT 的新手,一直在 XSLT 1.0 和 2.0 中尝试各种方法来完成此任务,但没有...

回答 1 投票 0

Xpath 测试是否是数字或有效的数字表达式

找出节点值是否为数字非常简单,例如: 但是,如果我...

回答 1 投票 0

XSLT3 将 json 输出设置为不带键的字符串数组

我是xslt新手, 我有一个包含这些元素的 xml 第 1 行 &...

回答 1 投票 0

有没有办法在 XSLT 脚本文件中本地运行 XQuery 函数/或 XQuery 脚本?

我在堆栈溢出中提出的上一个问题是关于如何成功使用 load-query-module 在 XSLT 中运行 XQuery,但是运行时必须能够知道如何使用位置提示来查找

回答 1 投票 0

XSLT 映射根据字段值对段进行排序

我正在尝试编写XSLT1.0来根据字段对子段进行排序,我无法对其进行排序,有人可以帮忙吗? 附加输入和输出文件请建议 会有多个段...

回答 1 投票 0

如何对具有两个不同字典 xml 路径的 XML 文件进行 XSL 转换并将其转换为一个词典

XML 文件,其中包含两个字典的路径和徽标的路径。该词典包含 10 个单词,每个单词一个英语单词,一个瑞典语单词。 XML 文件,其中包含两个词典的路径和徽标的路径。这些词典包含 10 个单词,每个英语单词和一个瑞典语单词。 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="lexikon.xsl"?> <lexicon> <wordlist1>xml_english.xml</wordlist1> <wordlist2>xml_swedish.xml</wordlist2> <editor>Simon Bale</editor> <logo>logo.svg</logo> </lexicon> XML 英语词典 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="xml_style.css"?> <wordlist> <language>English</language> <author>David Cahill</author> <words> <word>night</word> <word>day</word> <word>water</word> <word>car</word> <word>sun</word> <word>sky</word> <word>city</word> <word>house</word> <word>cat</word> <word>rain</word> </words> </wordlist> XSL 文件 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict" > <xsl:output method="xml" indent="yes" encoding="iso-8859-1" /> <xsl:template match="/"> <html> <head> <title>Lexicon</title> <link rel="stylesheet" type="text/css" href="xhtml_style.css"/> </head> <body> <image src="logo.svg" alt="Logo"/> <h1><xsl:value-of select="/wordlist/language"/> </h1> <h2><xsl:value-of select="/wordlist/author"/> </h2> <ul> <!--en lista--> <xsl:for-each select="/wordlist/words/word"> <li> <xsl:value-of select="."/> </li> </xsl:for-each> </ul> </body> </html> </xsl:template> </xsl:stylesheet> 我想将包含徽标和字典路径的 XML 文件转换为词典。但我陷入困境,不确定我做得是否正确。 <!-- transform.xsl --> <!-- Define variables to hold the documents --> <xsl:variable name="doc1" select="document('xml_english.xml')"/> <xsl:variable name="doc2" select="document('xml_swedish.xml')"/> <!-- Main template to process the merged documents --> <xsl:template match="/"> <output> <!-- Process content from doc1 --> <xsl:apply-templates select="$doc1/root/*"/> <!-- Process content from doc2 --> <xsl:apply-templates select="$doc2/root/*"/> </output> </xsl:template> <!-- Template to process nodes from doc1 --> <xsl:template match="data"> <!-- Customize processing as needed --> <processedData1> <xsl:value-of select="."/> </processedData1> </xsl:template> <!-- Template to process nodes from doc2 --> <xsl:template match="data"> <!-- Customize processing as needed --> <processedData2> <xsl:value-of select="."/> </processedData2> </xsl:template>

回答 1 投票 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

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