繁体   English   中英

如何使用 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>

目标是我应该能够创建从“Booth”到“Explanation”的链接。 所有 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>

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM