3

I have following XML.

<sec xmlns:xlink="http://www.w3.org/1999/xlink" id="fm2" sec-type="other">
  <title />
  <?SAE page="ii"?>
  <p><bold>Other SAE books of interest:</bold></p>
  <p><bold>Electric and Hybrid-Electric Vehicles</bold></p>
  <p>Edited by Ronald K. Jurgen</p>
  <p>(Product Code: PT-143.SET)</p>
  <p><bold>Diesel Emissions and Their Control</bold></p>
  <p>By Magdi K. Khair and W. Addy Majewski</p>
  <p>(Product Code: R-303)</p>
  <p><bold>Hybrid Powered Vehicles, Second Edition</bold></p>
  <p>By John M. German</p>
  <p>(Product Code: T-125)</p>
  <p>For more information or to order a book, contact SAE International at</p>
  <p>400 Commonwealth Drive, Warrendale, PA 15096-0001, USA;</p>
  <p>phone 877-606-7323 (U.S. and Canada only) or 724-776-4970 (outside U.S. and Canada);</p>
  <p>fax 724-776-0790;</p>
  <p>email <email>CustomerService@sae.org</email>;</p>
  <p>website <uri xlink:href="http://books.sae.org">http://books.sae.org</uri>.</p>
</sec>

I have to create XSLT for it. I am doing it in this way:

<xsl:for-each select="book-front/sec">
  <xsl:value-of select="title"/>
  <xsl:value-of select="p"/>
</xsl:for-each>    

But it is not parsing anything.

The main problem is parsing <?SAE page="ii"?> It is not allowing its further attributes to parse. I don't know how to parse it and then what will be its corresponding XSLT.

My xml is

<sec id="ch1.4">
<label><bold>1.4</bold></label>
<title><bold>Energy Consumption of Commercial Vehicles</bold></title>
<p>Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B;<xref ref-type="bibr" rid="R3">1.3</xref>&#x005D;. According to the U.S. Department of Energy, 72&#x0025; <?SAE page="8"?>of the U.S. petroleum consumption is for transportation. Commercial vehicles consumed up to 18.7&#x0025; of the total energy consumption in transportation in the United States. In other words, commercial vehicles in the United States alone consumed over 3&#x0025; of the global petroleum production in 2009 &#x005B;<xref ref-type="bibr" rid="R3">1.3</xref>, <xref ref-type="bibr" rid="R34">1.34</xref>&#x005D;.</p></sec>

after applying ur code, i got

Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B; 

But i want

**Energy Consumption of Commercial Vehicles**
Commercial vehicle manufacturing and operation is a major source of energy consumption globally. In 2009, the United States consumed 23&#x0025; of the global petroleum production &#x005B;1.3 &#x005D;. According to the U.S. Department of Energy, 72&#x0025;Page 8 of the U.S. petroleum consumption is for transportation. Commercial vehicles consumed up to 18.7&#x0025; of the total energy consumption in transportation in the United States. In other words, commercial vehicles in the United States alone consumed over 3&#x0025; of the global petroleum production in 2009 &#x005B;

Now explain how to get this output ?? I hope my problem is more understandable to you now.

user
  • 128
  • 2
  • 12

2 Answers2

3

You can use

<xsl:template match="processing-instruction('SAE')">
  <xsl:value-of select="." />
</xsl:template>

to match processing instructions named SAE. (And <xsl:apply-templates select="processing-instruction('SAE')" />, naturally)

The value of the instruction is not really XML, even if it looks like regular attributes in this case.

Processing instructions really only contain plain text, because they might be anything - from simple bits of information to complete programs in languages other than XSLT.

You can only get the content of the instruction, in your case the string 'page="ii"', which you then must parse manually.

You could do it in XPath like this:

<xsl:template match="processing-instruction('SAE')">
  <xsl:variable name="start-token">page="</xsl:variable>
  <xsl:variable name="end-token">"</xsl:variable>
  <xsl:variable name="temp" select="substring-after(., $start-token)" />

  <xsl:value-of select="substring-before($temp, $end-token)" />
  <!-- output: "ii" -->
</xsl:template>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Thanks. It was a great help. Will u please also tell me how to parse xref tags also ? Because in my case wherever xref tags are found, it doesn't write the contents written after xref tags.I hope my ques is understandable to you or should i explain in more detail ? – user Feb 13 '14 at 08:54
  • You might want to try and search for that first. Others will have had the same problem before you, I'm sure you will find an example that explains it. – Tomalak Feb 13 '14 at 08:56
0

If you want to simple copy the whole content, just do the following:

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

this is called an identity template. Afterwards, create template matches to override the copied nodes. In case of xref, you can try.

<xsl:template match="xref">
    <a href="{@rid}">
        <xsl:apply-templates/>
    </a>
</xsl:template>
Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14
  • M. Lamsen I already have added this namespace in my root tag. But still it is not working.. My root tag is – user Feb 13 '14 at 06:06
  • I tried running your code here and it seems okay. Can you edit your post to add some more information about your stylesheet? – Joel M. Lamsen Feb 13 '14 at 06:38
  • when i tried to run this code, in my case p tags having bold values are not parsing now.. Only simple p tags are parsed. But i want to parse this p tag and want to bold the contents too in output. How to do that ? and i want output in sequence as it is written. – user Feb 13 '14 at 07:15
  • in my case, xref tags also are also not parsing. Whenever, xref tags come, it stops there only and find another p tags. How to parse xref tags also ? – user Feb 13 '14 at 07:22
  • do you want to copy the whole XML? or just a part of it? – Joel M. Lamsen Feb 13 '14 at 07:28
  • i just dont want simply to copy the contents, basically i am converting xml to html using xslt. so i have to define certain styles as well as add some p tags. so my ques is just how to parse xref tags in xslt ? – user Feb 13 '14 at 08:38
  • modified my answer again. – Joel M. Lamsen Feb 13 '14 at 09:15
  • it gives only the value of xref tag but still it is not showing further contents that are written in p tag after the closing tag of xref tag. – user Feb 13 '14 at 09:56
  • see the question, i have updated the output that i got and the output i want along with XML. – user Feb 13 '14 at 10:00