Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As stated in title, is it possible using xsl or should I look at VB. I'm not a coder but I am good at reading. This is a snippet of my XML and I have worked on the first child, if I can crack that then the rest should fall into place.
MY XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="205.xsl"?>
<data>
<satellite name="Telstar" longitude="205" lof_hi="10600000" lof_lo="9750000" lof_threshold="11700000" lnb_power="13/18v" signal_22khz="auto" toneburst="none" diseqc1_0="none" diseqc1_1="none" motor="none">
  <transponder original_network_id="1" ts_id="65535" frequency="11714000" symbol_rate="25000000" polarisation="H" />
  <transponder original_network_id="1" ts_id="65535" frequency="11729000" symbol_rate="25000000" polarisation="V">
    <program name="Channel_1 HD" service_id="40805" channel_number="48" type="1" scrambled="false" parental_lock="false" skip="false" id="1500" plp_id="255" sdt_version="28" default_channnel_num="17" lcn="2697" fav="0">
      <video pid="165" format="h264" />
      <pcr pid="165" />
      <audio pid="110" format="ac3" language="spa" />
      <audio pid="111" format="ac3" language="dos" />
    </program>
    <program name="Channel_2 HD" service_id="40806" channel_number="14" type="1" scrambled="false" parental_lock="false" skip="false" id="1501" plp_id="255" sdt_version="28" default_channnel_num="18" lcn="2698" fav="0">
      <video pid="160" format="h264" />
      <pcr pid="160" />
      <audio pid="80" format="ac3" language="spa" />
      <audio pid="81" format="ac3" language="qaa" />
    </program>
    <program name="Channel_3 HD" service_id="40807" channel_number="34" type="1" scrambled="false" parental_lock="false" skip="false" id="1502" plp_id="255" sdt_version="28" default_channnel_num="19" lcn="2699" fav="0">
      <video pid="161" format="h264" />
      <pcr pid="161" />
      <audio pid="86" format="ac3" language="spa" />
      <audio pid="87" format="ac3" language="qaa" />
    </program>
    </transponder>
    </satellite>
   </data>


MY XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="XML" version="1.0" encoding="UTF-8" indent="yes"/>
     <xsl:template match="/">
       <xsl:element name="Servicelist">
       <xsl:element name="satellite">
       <xsl:text></xsl:text>
       <xsl:for-each select = "data"> 
        <xsl:text>SatId 1 /SatId </xsl:text>
            <xsl:text></xsl:text>
            <xsl:for-each select="satellite">
            <xsl:value-of select="@name" />
            <xsl:text></xsl:text>
            <xsl:value-of select="@lof_lo" />
            <xsl:text></xsl:text>
            <xsl:value-of select="@lof_hi" />
            <xsl:text></xsl:text>
            <xsl:value-of select="@lof_threshold" />
            <xsl:text></xsl:text>
            <xsl:value-of select="@longitude" />
            <xsl:text></xsl:text>
           <xsl:text>0 </xsl:text>
            <xsl:text></xsl:text>                
            </xsl:for-each>
        </xsl:for-each>       
       </xsl:element>
       </xsl:element>
     </xsl:template>
</xsl:stylesheet>


MY OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<Servicelist>
<satellite>
SatId 1 /SatId 
Telstar
9750000
10600000
11700000
205
0 
</satellite>
</Servicelist>


CODE NEEDED
<?xml version="1.0" encoding="UTF-8"?>
<Servicelist>
  <Satellite>
    <SatId>1</SatId>
    <SatName>Telstar</SatName>
    <LO1Frequency>9750</LO1Frequency>
    <LO2Frequency>10600</LO2Frequency>
    <BandSwitchFreq>11700</BandSwitchFreq>
    <Longitude>205</Longitude>
    <SkewOffset>0</SkewOffset>
  </Satellite>
<Servicelist>


What I have tried:

I need to figure out how to get the elements displayed (eg <satellite>) and also how to rename the elements... Also the element <satid> is not in the original xml, so I added it in the xsl as text, but the xsl gives me an error if I add the code tags (<>). Am I in the right direction with the xsl or should I take a different approach?? Any advice welcome and thank you.
Posted
Updated 11-Mar-20 1:04am

1 solution

A slight change to your XSL will produce the desired output:
XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <Servicelist>
            <xsl:for-each select="data/satellite">
                <xsl:variable name="i" select="position()" />
                
                <Satellite>
                    <SatId><xsl:value-of select="$i" /></SatId>
                    <SatName><xsl:value-of select="@name" /></SatName>
                    <LO1Frequency><xsl:value-of select="@lof_lo" /></LO1Frequency>
                    <LO2Frequency><xsl:value-of select="@lof_hi" /></LO2Frequency>
                    <BandSwitchFreq><xsl:value-of select="@lof_threshold" /></BandSwitchFreq>
                    <Longitude><xsl:value-of select="@longitude" /></Longitude>
                    <SkewOffset>0</SkewOffset>
                </Satellite>
            </xsl:for-each>
        </Servicelist>
    </xsl:template>
</xsl:stylesheet>
The SatId will be a sequential number starting at 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900