gpt4 book ai didi

xml - XSLT - 重命名节点和删除空格等等

转载 作者:行者123 更新时间:2023-12-04 05:19:37 24 4
gpt4 key购买 nike

这是一个非常复杂的问题,超出了我对 XSLT 的了解 - 我仍在学习,无论我阅读了多少 O'Reilly 的 XSLT 书籍,我都处于头脑中。

我有一个多方面的问题,我已经为它生成了一个输入 XML 文件,之后我也将尝试解释需求。

输入

<roottag>
<body>
<header>
<r>
<c>
<d>Header Tag</d><!-- This can include spaces-->
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Header Tag A</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Header Value A</d>
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Header Tag B</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Header Value B</d>
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Header Tag C</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Header Value C</d>
<e>System generated trash</e>
</c>
</r>
</header>
<information>
<r>Body of document</r>
<r>Appears here but have an XSLT that deals with this</r>
</informtaion>
<footer>
<r>
<c>
<d>Footer Tag</d><!-- This can include spaces-->
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Footer Tag A</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Footer Value A</d>
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Footer Tag B</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Footer Value B</d>
<e>System generated trash</e>
</c>
</r>
<r>
<c>
<d>Sub Footer Tag C</d>
<e>System generated trash</e>
</c>
<c>
<d>Sub Footer Value C</d>
<e>System generated trash</e>
</c>
</r>
</footer>
</body>
</roottag>

输出
<?xml version="1.0" encoding="utf-8"?>
<roottag>
<body>
<header>
<HeaderTag>
<!-- without spaces -->
<HeaderName>Header Tag</HeaderName>
<!-- This needs to preserve spaces-->
</HeaderTag>
<SubHeaderTagA>
<!-- without spaces -->
<HeaderName>Sub Header Tag A</HeaderName>
<!-- This needs to preserve spaces-->
<HeaderValue>Sub Header Value A</HeaderValue>
</SubHeaderTagA>
<SubHeaderTagB>
<HeaderName>Sub Header Tag B</HeaderName>
<HeaderValue>Sub Header Value B</HeaderValue>
</SubHeaderTagB>
<SubHeaderTagC>
<HeaderName>Sub Header Tag C</HeaderName>
<HeaderValue>Sub Header Value C</HeaderValue>
</SubHeaderTagC>
</header>
<information>
<r>Body of document</r>
<r>Appears here but have an XSLT that deals with this</r>
</information>
<footer>
<FooterTag>
<FooterName>Footer Tag</FooterName>
</FooterTag>
<SubFooterTagA>
<FooterName>Sub Footer Tag A</FooterName>
<FooterValue>Sub Footer Value A</FooterValue>
</SubFooterTagA>
<SubFooterTagB>
<FooterName>Sub Footer Tag B</FooterName>
<FooterValue>Sub Footer Value B</FooterValue>
</SubFooterTagB>
<SubFooterTagC>
<FooterName>Sub Footer Tag C</FooterName>
<FooterValue>Sub Footer Value C</FooterValue>
</SubFooterTagC>
</footer>
</body>
</roottag>

所以要解释我所看到的问题,以及我所面临的问题。
  • 删除空格:
    roottag/body/header/r/c/d 中保存的值可以并且通常包含空格,因此我需要一种方法来删除它,这是我从网站 [addLink] 上的问题中找到的,但是这也会替换该值,因此当我在稍后的过程中使用数据,它没有输出所需的空格。
  • 仅用第一个值替换 R:
    我不知道如何做到这一点,我尝试和研究的一切似乎都使用第二个值作为值。我在我的 wiks 结束了。
  • 页眉或页脚名称/值:
    同样,我的知识是有限的,如果这甚至是可能的,或者每个标签都需要单独匹配?
  • 页眉和页脚标签的移动:
    我没有将它包含在我需要的输出中,但我想我可能需要这样做 - 是否可以将页眉标签和页脚标签移到正文标签之外?所以 XML 将是:roottag-header-body-information-/body-footer/-/rt

  • 如果您需要更多说明,请告诉我。

    最佳答案

    从源中的元素内容派生元素名称通常是一个坏主意 - 尽管您可以删除空格,但始终存在其他特殊字符的可能性,即使您将它们全部删除,最终也可能会出现意外的重复项。包含 1 Tag 的两个元素和 2 Tag例如,两者都需要精简为 Tag .

    但是,这样的事情应该可以完成这项工作:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*" />

    <xsl:variable name="allowed">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz_</xsl:variable>

    <xsl:template match="r[c/d]">
    <xsl:variable name="elemName" select="translate(c/d,translate(c/d,$allowed,''),'')" />
    <xsl:element name="{$elemName}">
    <xsl:apply-templates />
    </xsl:element>
    </xsl:template>

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

    <xsl:template match="c[1]/d">
    <HeaderName>
    <xsl:apply-templates />
    </HeaderName>
    </xsl:template>

    <xsl:template match="c[2]/d">
    <HeaderValue>
    <xsl:apply-templates />
    </HeaderValue>
    </xsl:template>

    <xsl:template match="e" />

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

    这使用“双重翻译”方法从字符串中去除所有不需要的字符。 translate函数可用于从列表中删除所有字符,方法是指定一个空字符串将这些字符转换为。您可以使用它从字符串中删除所有 VALID 字符,留下仅包含 INVALID 字符的字符串。然后您再次使用 translate 从原始字符串中删除所有这些 INVALID 字符。

    如果您确实需要正文之外的页眉/页脚,请添加以下模板:
    <xsl:template match="roottag">
    <xsl:copy>
    <xsl:apply-templates select="body/header" />
    <xsl:apply-templates select="body" />
    <xsl:apply-templates select="body/footer" />
    </xsl:copy>
    </xsl:template>

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

    忽略 r节点所在 c\d包含一个等号,在匹配“ r[c/d]”的下面添加这个模板:
    <xsl:template match="r[contains(c/d,'=')]" />

    关于xml - XSLT - 重命名节点和删除空格等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13799148/

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