简体   繁体   中英

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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