<LoanAddressDetails>
<LoanAddressDetail>
<LOAN_ADDRESS_TYPE>Mailing</LOAN_ADDRESS_TYPE>
<LOAN_ADDRESS_LINE_1>2197 RIDGEMONT RD</LOAN_ADDRESS_LINE_1>
<LOAN_ADDRESS_LINE_2/>
<LOAN_CITY>GROSSE POINTE WOO</LOAN_CITY>
<LOAN_STATE>MI</LOAN_STATE>
<LOAN_ZIP_CODE>48231337</LOAN_ZIP_CODE>
</LoanAddressDetail>
</LoanAddressDetails>
Asked
Active
Viewed 131 times
0

Undo
- 25,519
- 37
- 106
- 129

Zaheer Chughtai
- 1
- 1
-
through a batch script will be preferable. – Zaheer Chughtai Nov 12 '18 at 16:50
-
Possible duplicate of [how to edit XML using bash script?](https://stackoverflow.com/questions/6873070/how-to-edit-xml-using-bash-script) – mx0 Nov 12 '18 at 17:03
-
No it's different. I want to first get the length of the value of an XML Tag from a file. And then if its length is greater than 5, remove the characters after the fifth character. Please let me know how to do it. – Zaheer Chughtai Nov 12 '18 at 17:17
-
For XML processing it is always preferable to use an XML parser and not some kind of text processing, because otherwise it could lead to bugs that are hard to impossible to detect. – zx485 Nov 12 '18 at 19:06
-
Thank you so much sir. Can this code be used in a Visual Cron job through some task please? – Zaheer Chughtai Nov 12 '18 at 19:43
1 Answers
0
To limit the length of the LOAN_ZIP_CODE
element to 5 characters, you can use xsltproc
, a tiny Linux command line XSLT-1.0 processor, for this. So use the following stylesheet test.xslt
<?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" omit-xml-declaration="yes" indent="yes"/>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<!-- Shorten one specific text node to a maximum length of 5 -->
<xsl:template match="LOAN_ZIP_CODE/text()">
<xsl:value-of select="substring(.,1,5)" />
</xsl:template>
</xsl:stylesheet>
and pass the input XML file (here named file.xml
) to the output XML file (here named target.xml
) using xsltproc
with
xsltproc -o target.xml test.xslt file.xml
Output is:
<LoanAddressDetails>
<LoanAddressDetail>
<LOAN_ADDRESS_TYPE>Mailing</LOAN_ADDRESS_TYPE>
<LOAN_ADDRESS_LINE_1>2197 RIDGEMONT RD</LOAN_ADDRESS_LINE_1>
<LOAN_ADDRESS_LINE_2/>
<LOAN_CITY>GROSSE POINTE WOO</LOAN_CITY>
<LOAN_STATE>MI</LOAN_STATE>
<LOAN_ZIP_CODE>48231</LOAN_ZIP_CODE>
</LoanAddressDetail>
</LoanAddressDetails>
Of course you can use this command in a bash script.

zx485
- 28,498
- 28
- 50
- 59
-
Two more things please. I want to edit the length of a single node which is LOAN_ZIP_CODE, not all nodes. Secondly how to write the output of xsltproc into a file? – Zaheer Chughtai Nov 12 '18 at 21:23
-
-
That worked. Thank you so much. One more thing, is there a way we can call xslproc in a loop to read the xml files one by one in a folder and then fix them? And on fixing the last file, it exit automatically? – Zaheer Chughtai Nov 13 '18 at 15:57
-
You can do this with a bash `for` loop: `for f in *.xml; do xsltproc -o "$f"_target.xml test.xslt "$f"; done`. – zx485 Nov 13 '18 at 16:33
-
That's so kind of you but I need to run this xsltproc through a .bat file or through some windows environment, like Visual Cron. Is it possible to modify it to make it compatible with windows please? – Zaheer Chughtai Nov 13 '18 at 17:24
-
Also I need to set the directory where from it should pick xml files. – Zaheer Chughtai Nov 13 '18 at 17:27
-
You do not have to use `xsltproc` as an XSLT processor. Just use whatever XSLT-processor you have available and put it into a batch script. I can't help you with Windows programs. – zx485 Nov 13 '18 at 19:32