0

Hello i have some xml output

<?xml version="1.0" encoding="utf-8"?>
<Active-plan currentid="1276215628">
    <Planers>
        <Plan1>
            <package>Intro_Europe_1GB_7d</package>
            <current-package>
                <package currentid="63753939" addid="13000">
                    <group-by-pack>data</group-by-pack>
                    <quantity>1071741824.00</quantity>
                    <remaining>993553024.00</remaining>
                    <timestamp-start>2022-03-01 10:35:05Z</timestamp-start>
                    <timestamp-end>2022-03-08 10:35:05Z</timestamp-end>
                </package>
            </current-package>
        </Plan1>
    </Planers>
</Active-plan>

What i want to have is to have output maybe with SED or AWK

Active plan currentid:1276215628
Package: Intro_Europe_1GB_7d
Package current id:63753939 addit:13000
Group pack : data
Quantity : 1071741824.00
Remaining data : 993553024.00
Start Time : 2022-03-01 10:35:05Z
End time : 022-03-08 10:35:05Z

I think this i need to export with sed and call by echo so i can display in that order that i post (every tag, new line)

Vancho
  • 25
  • 7
  • This will be a lot easier with a scripting language - Perl, PHP, Python, Ruby, node.js, whatever you're most comfortable with - where you can parse the XML properly and output the fields you need. It would be a few lines of PHP with [SimpleXML](https://php.net/simplexml) for instance. – IMSoP Mar 08 '22 at 12:23
  • Thanks @IMSoP i have managed this little bit. currentid="63753939" addid="13000" But how to export this using simplexml?, cause they are generated with every request and it's random. – Vancho Mar 08 '22 at 13:00
  • I'm not sure what you mean. From the sample given, the `currentid` would be something like `echo $xml->Planers->Plan1->{"current-package"}->package['currentid'];` – IMSoP Mar 08 '22 at 13:16
  • @IMSoP thanks a lot! and what about if i have Active-plan currentid ? Because i tried : `$xml>Active-plan['currentid'] ` and doesn't return anything Regards – Vancho Mar 08 '22 at 15:14
  • It's important to note that this isn't a forum, so if you have some code that almost works and need help with it, you should post that as a question. First, though, you can search for existing questions; in this case: [SimpleXML Reading node with a hyphenated name](https://stackoverflow.com/q/3626901/157957) – IMSoP Mar 08 '22 at 15:20

1 Answers1

2

XSLT 3.0 solution:

<xsl:transform version="3.0"
   xmlns:xsl="http://www.w3.org/2001/XSL/Transform" 
   expand-text="yes">    
  <xsl:output method="text"/>
  <xsl:template match="/">
    Active plan currentid:{/Active-plan/@currentid}
    Package: {//Plan1/Package}
    Package current id:{//current-package/@currentid} addit:{//current-package/@addit}
    Group pack : {//current-package/group-by-data}
    Quantity : {//current-package/quantity}
    Remaining data : {//current-package/remaining}
    Start Time : {//current-package/timestamp-start}
    End time : {//current-package/timestamp-end}
  </xsl:template>
</xsl:transform>

Solutions using XSLT 1.0 or 2.0 will be a little more verbose but no more difficult.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164