gpt4 book ai didi

xml - 按键:选择前一个同级节点的最后一个子节点

转载 作者:行者123 更新时间:2023-12-03 16:21:21 29 4
gpt4 key购买 nike

我有以下格式的(格式错误的DITA)XML:

<dl>
<dlentry><dt>BLARG</dt>
<dd>BLARG Definition</dd>
<dt>BLARG2</dt>
<dd>BLARG2 Definition</dd></dlentry>
</dl>
<dl>
<dlentry><dt>BLARG3</dt>
<dd>BLARG3 Definition</dd></dlentry>
</dl>
<p>Continuation of BLARG3 definition.</p>
<note>Note pertaining to BLARG3 definition</note>


等等。

我正在尝试将这一系列 <dl>元素合并为一个 <dl>,看起来像这样:

<dl>
<dlentry><dt>BLARG</dt>
<dd>BLARG Definition</dd></dlentry>
<dlentry><dt>BLARG2</dt>
<dd>BLARG2 Definition</dd></dlentry>
<dlentry><dt>BLARG3</dt>
<dd>BLARG3 definition. Continuation of BLARG3 definition.
<note>Note pertaining to BLARG3 definition</note></dd></dlentry>
<dl>


我试图根据先前的 <p><note>值使用键为任何 generate-id()<dd>节点建立索引,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="kFollowing" match="p|note|table" use="generate-id(preceding::dd[1])"/>
<!-- identity template -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dl">
<xsl:choose>
<xsl:when test="count(preceding::dl)=0">
<!--We only want to process the first dl. All others should be ignored, because their content will get included in this one.-->
<dl>
<!--Want to grab all following terms, except ones that contain phrase elements, which need to be turned into a table because they are field values-->
<xsl:for-each select="following::dt[not(child::ph)]|descendant::dt">
<dlentry>
<dt><xsl:value-of select="normalize-space(.)"/></dt>
<xsl:variable name="ddID"><xsl:value-of select="generate-id(following::dd[1])"/></xsl:variable>
<dd><xsl:for-each select="following::dd[1]">
<xsl:apply-templates/><xsl:text>. </xsl:text>
</xsl:for-each>
<xsl:variable name="ddKey" select="key('kFollowing',generate-id(following::dd[1]))"/>
<xsl:for-each select="following::*[$ddKey]">
<xsl:apply-templates/>
</xsl:for-each>
</dd>
</dlentry>
</xsl:for-each>
</dl>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>

最佳答案

此转换:

<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="kFollowing" match="p|note"
use="generate-id(preceding-sibling::dl[1]/dlentry)"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="dl[1]">
<dl>
<xsl:apply-templates select="../dl/dlentry"/>
</dl>
</xsl:template>

<xsl:template match="dd">
<dd>
<xsl:value-of select="."/>
<xsl:copy-of select="key('kFollowing', generate-id(..))/self::p/text()"/>
<xsl:copy-of select="key('kFollowing', generate-id(..))/self::note"/>
</dd>
</xsl:template>
<xsl:template match="dl|p|note"/>
</xsl:stylesheet>


当应用于以下XML文档(提供的片段包装在单个top元素中)时:

<t>
<dl>
<dlentry>
<dt>BLARG</dt>
<dd>BLARG Definition</dd>
<dt>BLARG2</dt>
<dd>BLARG2 Definition</dd>
</dlentry>
</dl>
<dl>
<dlentry>
<dt>BLARG3</dt>
<dd>BLARG3 Definition</dd>
</dlentry>
</dl>
<p>Continuation of BLARG3 definition.</p>
<note>Note pertaining to BLARG3 definition</note>
</t>


产生想要的正确结果:

<t>
<dl>
<dlentry>
<dt>BLARG</dt>
<dd>BLARG Definition</dd>
<dt>BLARG2</dt>
<dd>BLARG2 Definition</dd>
</dlentry>
<dlentry>
<dt>BLARG3</dt>
<dd>BLARG3 DefinitionContinuation of BLARG3 definition.<note>Note pertaining to BLARG3 definition</note>
</dd>
</dlentry>
</dl>
</t>


我故意没有在两个文本节点之间生成 ". "分隔符,因为尚未将其声明为必需条件,可能只是OP的一种修饰。这样做需要花费少量的额外精力,留给读者练习:)。

关于xml - 按键:选择前一个同级节点的最后一个子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023568/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com