gpt4 book ai didi

xml - 用 XSLT 包裹相邻的

标记

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

我正在尝试使用 XSLT 将 HTML 转换为 XML。

HTML:

<html>
<body>
<p class="one">Some paragraph 1.</p>
<p class="one">Some paragraph 2.</p>
<p class="one">Some paragraph 3.</p>
<p class="one">Some paragraph 4.</p>
<p class="one">Some paragraph 5.</p>
<h3>Some heading</h3>
<p class="ipsum">Wrapped paragraph 1.</p>
<p class="ipsum">Wrapped paragraph 2.</p>
<p class="ipsum">Wrapped paragraph 3.</p>
<p class="ipsum">Wrapped paragraph 4.</p>
<p class="ipsum">Wrapped paragraph 5.</p>
<h3>Some heading</h3>
<p class="two">Some paragraph 1.</p>
<p class="two">Some paragraph 2.</p>
<p class="two">Some paragraph 3.</p>
<p class="two">Some paragraph 4.</p>
<p class="two">Some paragraph 5.</p>
</body>
</html>

期望的输出:
<DocumentWrapper>
<paragraph>Some paragraph 1.</paragraph>
<paragraph>Some paragraph 2.</paragraph>
<paragraph>Some paragraph 3.</paragraph>
<paragraph>Some paragraph 4.</paragraph>
<paragraph>Some paragraph 5.</paragraph>
<Heading>Some heading</Heading>
<WrapperSpecial>
<SpecialParagraph>Wrapped paragraph 1.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 2.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 3.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 4.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 5.</SpecialParagraph>
</WrapperSpecial>
<Heading>Some heading</Heading>
<paragraph>Some paragraph 1.</paragraph>
<paragraph>Some paragraph 2.</paragraph>
<paragraph>Some paragraph 3.</paragraph>
<paragraph>Some paragraph 4.</paragraph>
<paragraph>Some paragraph 5.</paragraph>
</DocumentWrapper>

我写了以下 XSLT:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<DocumentWrapper>
<xsl:apply-templates select="html/body/*" />
<xsl:text>&#xa;</xsl:text></DocumentWrapper>
</xsl:template>

<xsl:template match="p[@class='one']|p[@class='two']">
<xsl:text>&#xa;</xsl:text><paragraph>
<xsl:value-of select="."/>
</paragraph>
</xsl:template>

<xsl:template match="h3">
<xsl:text>&#xa;</xsl:text>
<Heading>
<xsl:value-of select="."/>
</Heading>
</xsl:template>

</xsl:stylesheet>

除了包装 p 之外,它什么都做带有 ipsum 的标签分类为:
<WrapperSpecial>
<SpecialParagraph>Wrapped paragraph 1.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 2.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 3.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 4.</SpecialParagraph>
<SpecialParagraph>Wrapped paragraph 5.</SpecialParagraph>
</WrapperSpecial>

我不知道该怎么做。

最佳答案

我会处理这类问题的通常方法是,如果你想对一组相邻的 sibling 做一些特殊的事情,那就是编写代码,以便我们最初只将模板应用于每次运行的第一个元素,然后让模板用于那个元素处理组的其余部分。

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes" />

<xsl:template match="/">
<xsl:apply-templates select="html/body" />
</xsl:template>

<xsl:template match="body">
<DocumentWrapper>
<!-- apply templates to (a) everything that is _not_ an ipsum para
and (b) the first ipsum para in each run -->
<xsl:apply-templates select="*[not(self::p[@class='ipsum'])]
| p[@class='ipsum']
[not(preceding-sibling::*[1][self::p[@class='ipsum']])]" />
</DocumentWrapper>
</xsl:template>

<xsl:template match="p[@class='ipsum']">
<WrapperSpecial>
<xsl:apply-templates select="." mode="group" />
</WrapperSpecial>
</xsl:template>

<xsl:template match="p" mode="group">
<SpecialParagraph>
<xsl:value-of select="." />
</SpecialParagraph>
<!-- keep processing siblings until we hit something that _isn't_ an
ipsum para -->
<xsl:apply-templates mode="group"
select="following-sibling::*[1][self::p[@class='ipsum']]" />
</xsl:template>

<!-- this template will match any p elements except the ipsum ones -->
<xsl:template match="p">
<paragraph>
<xsl:value-of select="."/>
</paragraph>
</xsl:template>

<xsl:template match="h3">
<Heading>
<xsl:value-of select="."/>
</Heading>
</xsl:template>
</xsl:stylesheet>

关于xml - 用 XSLT 包裹相邻的 <p> 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23342310/

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