Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by zx485 for How to modify a file if file contain numbers that begin with '+1'

Be very careful when using sed to edit XML files. (It's risky).

But you can easily use an XSLT-1.0 processor like xsltproc or Saxon to remove the leading +1 string from the <address> element. So use the following XSLT file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" /> 
  <xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="sms/address[starts-with(.,'+1')]">
        <xsl:copy>
            <xsl:value-of select="substring(.,3)"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

with your XML and the result (with the XML from your question) is:

<root>
    <sms>
        <address>5629876543</address>
        <date>1554966601000</date>
        <type>1</type>
        <body> Yea, should be true. </body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
    <sms>
        <isMms>1</isMms>
        <date>1554968044000</date>
        <type>2</type>
        <mmsMsgBox>2</mmsMsgBox>
        <mmsReaded>1</mmsReaded>
        <attachments>
            <attachment>
                <type>image/jpeg</type>
                <body/>
                <name>Screenshot_20190411-002704_Flud.jpg</name>
            </attachment>
        </attachments>
    </sms>
    <sms>
        <isMms>0</isMms>
        <address>5621234567</address>
        <date>1554968778000</date>
        <type>1</type>
        <isMms>0</isMms>
        <address>7141234534</address>
        <date>1558919932000</date>
        <type>1</type>
        <body>:)</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
    <sms>
        <isMms>0</isMms>
        <address>7141234567</address>
        <date>1558927846000</date>
        <type>1</type>
        <body>It's so</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
        <isMms>0</isMms>
        <address>7145757575</address>
        <date>1543704644000</date>
        <type>1</type>
        <body>Hey</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
    <sms>
        <isMms>0</isMms>
        <date>1543704676000</date>
        <type>2</type>
        <body>More text</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
    <sms>
        <isMms>0</isMms>
        <address>7142323232</address>
        <date>1543704736000</date>
        <type>1</type>
        <body>Lol not even</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
    <sms>
        <isMms>0</isMms>
        <address>7141010101</address>
        <date>1543704748000</date>
        <type>1</type>
        <body>You do</body>
        <mmsReaded>1</mmsReaded>
        <attachments/>
    </sms>
</root>

This should be as desired.


Viewing all articles
Browse latest Browse all 3

Trending Articles