2

I would like to extract everything between the <head> tag elements in an html page. Including link tags and script tags.

Assume the source code below is a snippet which would be part of a full html document.

The source:

...
<head>
 <link rel="stylesheet" href="style.css"
    type="text/css" media="handheld" />

 <link rel="stylesheet" href="style.css"
    type="text/css" media="handheld" />

 <script type="text/javascript" src="main.js"></script>
 <script type="text/javascript" src="second.js"></script>
</head>
...

XSLT:

<xsl:output method="xml" encoding="utf-8" indent="no"/>

<xsl:template match="/">
    <xsl:copy-of select="link"/>
</xsl:template>

This works fine if there is only one tag I'm trying to get. Is there a way I can process everything and only everything between the "head" tags.

The output I expect would be:

 <link rel="stylesheet" href="style.css"
    type="text/css" media="handheld" />

 <link rel="stylesheet" href="style.css"
    type="text/css" media="handheld" />

 <script type="text/javascript" src="main.js"></script>
 <script type="text/javascript" src="second.js"></script>
2
  • Can you provide a fuller example of your source document, and an example of your expected output? Commented May 7, 2013 at 17:38
  • In general, you can use XSL only on XML-compliant data, such as XHTML. If your input is not XHTML you will probably get parse exceptions at some point. Commented May 7, 2013 at 22:53

3 Answers 3

2

You need to use an XSL identity transform

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

along with a template that prevents output of everything you don't want.

<xsl:template match="/">
  <xsl:apply-templates select="html/head"/>
</xsl:template>

The second template, being more specific, will match the root and then apply the stylesheet to the contents of the <head> tag. The identity transform will output the desired tags.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for a much more elegant answer than relying on <xsl:for-each>.
0

You need to use the 'xsl:for-each' statement

<xsl:template match="/">
      <xsl:for-each select="head/*">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:template>

Comments

0

I guess you can use <xsl:for-each> element to select every XML element of a specified node-set.

Just loop through all the elements inside head tag and then use xsl-current() function to get the value of each element in a way like this; <xsl:value-of select="current()"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.