Setinger
Setinger

Reputation: 169

Get the url of the hyperlink

I have to write xslt to wordml (2007) documents. There are hyperlinks like below.

< w:p w:rsidR="00FD086A" w:rsidRDefault="00425A76" w:rsidP="00FD086A">
< w: hyperlink r:id="rId4" w:history="1">
< w:r w:rsidR="00FD086A" w:rsidRPr="00425A76">
< w:rPr>
< w:rStyle w:val="Hyperlink"/>
< /w:rPr>
< w:t>google</w:t>
< /w:r>
< /w:hyperlink>
< /w:p>

I want to get the url for the link name. In here I want to get the url for the "google" link. I know its there in Relationships, but I can't access that with xslt. Does anybody know? (Probably writing a template?) please help me!

Upvotes: 1

Views: 354

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66781

Assuming that the following namespace prefixes are declared:

xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"

the following XPath can be used to select the value of the URL using the value of the w:hyperlink/@r:id (hard-coded value of "rId5" in this example):

/pkg:package
  /pkg:part
     /pkg:xmlData
       /rel:Relationships
         /rel:Relationship[@Id='rId5']/@Target

You could use it in the context of a template matching on the w:hyperlink to produce an HTML anchor element, like this:

<xsl:template match="w:hyperlink">
    <a href="{/pkg:package
                /pkg:part
                  /pkg:xmlData
                    /rel:Relationships
                      /rel:Relationship[@Id=current()/@r:id]/@Target}">
        <xsl:apply-templates/>
    </a>
</xsl:template>

Upvotes: 2

Related Questions