如何使用 XSLT 从 XML 文件中的特定标签创建 linkTag

发布于 2025-01-11 11:24:06 字数 1381 浏览 0 评论 0原文

我想更改以下 XML 中的具体标签。标签 应转换为 和标签 应转换为

<root>
<directCompNotes>
  <paragraph>Go to:</paragraph>
  <bullet-list>
    <item>
      <paragraph>
        <linkSource>
          Booth
        </linkSource>
      </paragraph>
    </item>
    <item>
      <paragraph>
        WithoutLinkSource
      </paragraph>
    </item>
  </bullet-list>
</directCompNotes>
<directComp>
  <paragraph>
    <linkDestination>
      Explainition
    </linkDestination>
  </paragraph>
</directComp>
</root>

这就是我试图做的:

<xsl:template match="root">
  
      <xsl:for-each select="directCompNotes/bullet-list/item/paragraph/linkSource">
        <fo:basic-link internal-destination="boothId">
          <xsl:value-of select="."/>
        </fo:basic-link>
      </xsl:for-each>
    
  </xsl:template>

目标是我应该能够创建从“Booth”到“Explanition”的链接。所有 linkSource 和 linkDestination 都在已定义的 XML 文件中。

有人可以帮我吗?

(抱歉,我的英语不是很好,但我希望我能够很好地解释这个问题)。

I want to change the specifics Tags in following XML. The tags <linkSource> should be convert to <fo:basic-link internal-destination="boothId"> and the tag <linkDestination> should be convertet to <fo:block id="boothId">.

<root>
<directCompNotes>
  <paragraph>Go to:</paragraph>
  <bullet-list>
    <item>
      <paragraph>
        <linkSource>
          Booth
        </linkSource>
      </paragraph>
    </item>
    <item>
      <paragraph>
        WithoutLinkSource
      </paragraph>
    </item>
  </bullet-list>
</directCompNotes>
<directComp>
  <paragraph>
    <linkDestination>
      Explainition
    </linkDestination>
  </paragraph>
</directComp>
</root>

That is what I tried to do:

<xsl:template match="root">
  
      <xsl:for-each select="directCompNotes/bullet-list/item/paragraph/linkSource">
        <fo:basic-link internal-destination="boothId">
          <xsl:value-of select="."/>
        </fo:basic-link>
      </xsl:for-each>
    
  </xsl:template>

The goal is that I should be able to create a link from "Booth" to "Explanition". All of the linkSource and linkDestinition are in the XML file already defined.

Can anyone help me please?

(Sorry, My English is not very well, but I hope I was able to explain the question well).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-01-18 11:24:06

请尝试以下 XSLT。

输入 XML

<?xml version="1.0"?>
<root>
    <directCompNotes>
        <paragraph>Go to:</paragraph>
        <bullet-list>
            <item>
                <paragraph>
                    <linkSource>Booth</linkSource>
                </paragraph>
            </item>
            <item>
                <paragraph>WithoutLinkSource</paragraph>
            </item>
        </bullet-list>
    </directCompNotes>
    <directComp>
        <paragraph>
            <linkDestination>Explainition</linkDestination>
        </paragraph>
    </directComp>
</root>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="root">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <xsl:apply-templates/>
        </fo:root>
    </xsl:template>

    <xsl:template match="linkSource">
        <fo:basic-link internal-destination="boothId">
            <xsl:value-of select="."/>
        </fo:basic-link>
    </xsl:template>

    <xsl:template match="linkDestination">
        <fo:block id="boothId">
            <xsl:value-of select="."/>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>

输出 XML

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <directCompNotes>
    <paragraph>Go to:</paragraph>
    <bullet-list>
      <item>
        <paragraph>
          <fo:basic-link internal-destination="boothId">Booth</fo:basic-link>
        </paragraph>
      </item>
      <item>
        <paragraph>WithoutLinkSource</paragraph>
      </item>
    </bullet-list>
  </directCompNotes>
  <directComp>
    <paragraph>
      <fo:block id="boothId">Explainition</fo:block>
    </paragraph>
  </directComp>
</fo:root>

Please try the following XSLT.

Input XML

<?xml version="1.0"?>
<root>
    <directCompNotes>
        <paragraph>Go to:</paragraph>
        <bullet-list>
            <item>
                <paragraph>
                    <linkSource>Booth</linkSource>
                </paragraph>
            </item>
            <item>
                <paragraph>WithoutLinkSource</paragraph>
            </item>
        </bullet-list>
    </directCompNotes>
    <directComp>
        <paragraph>
            <linkDestination>Explainition</linkDestination>
        </paragraph>
    </directComp>
</root>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="root">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <xsl:apply-templates/>
        </fo:root>
    </xsl:template>

    <xsl:template match="linkSource">
        <fo:basic-link internal-destination="boothId">
            <xsl:value-of select="."/>
        </fo:basic-link>
    </xsl:template>

    <xsl:template match="linkDestination">
        <fo:block id="boothId">
            <xsl:value-of select="."/>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>

Output XML

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <directCompNotes>
    <paragraph>Go to:</paragraph>
    <bullet-list>
      <item>
        <paragraph>
          <fo:basic-link internal-destination="boothId">Booth</fo:basic-link>
        </paragraph>
      </item>
      <item>
        <paragraph>WithoutLinkSource</paragraph>
      </item>
    </bullet-list>
  </directCompNotes>
  <directComp>
    <paragraph>
      <fo:block id="boothId">Explainition</fo:block>
    </paragraph>
  </directComp>
</fo:root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文