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

How to make a linkTag from the specific Tag in a XML file using XSLT

我想更改以下 XML 中的具体标签。标签 <linkSource> 应转换为 <fo:basic-link internal-destination="boothId">,标签 <linkDestination> 应转换为 <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>

这就是我尝试做的事情:

<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>

目标是我应该能够创建一个从“展位”到“解释”的 link。所有 linkSource 和 linkDestinition 都在已经定义的 XML 文件中。

谁能帮帮我?

(对不起,我的英文不是很好,但我希望我能够很好地解释问题)。

请尝试以下 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>