1

I am new to all this and have tried various route but I am not able to extract the data I need. The XML file that I am working with is automatically generated by a piece of hardware and its size generally about 35Kb. In the XML file there is a reference to a XSD file, which I do not have.

The information that I need to extract are as follows:

   -n:D 
   -n:S
   -filter :
      -node CUS with n:did='1', 
      -child node CU with n:u='1'
      -grandchild node D with n:c='c' 
      -sort the grandchild by n:f 
      -extract the grand-grandchild n:Q
      -add all n:Q value for similar n:f from different nodes

As you can see below this is a very complex structure. Each time that I test for a condition on a node, I loose the entire XML structure in my output.

Here is part of the XML file:

<?xml version="1.0" encoding="UTF-8"?>
   <soapenv:E xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:B>
         <n:IR xmlns:n="http://abc.xxx/bb.xsd" n:result="0">
            <n:D>2016/02/11 09:11:15</n:D>
            <n:S>1</n:S>
            <C n:t="3">
                 <D n:c="C" n:f="1" n:r="0" n:did="1">
                    <n:Q>10</n:Q>
                    <n:St>2</n:St>
                </D>
            ...
                <D n:c="C" n:f="5" n:r="0" n:did="1">
                    <n:Q>12</n:Q>
                    <n:St>2</n:St>
                </D>
                <D n:c="U" n:f="20" n:r="0" n:did="1">
                    <n:Q>1</n:Q>
                    <n:St>2</n:St>
                </D>
            ...
                <D n:c="U" n:f="5" n:r="0" n:did="1">
                    <n:Q>5</n:Q>
                    <n:St>2</n:St>
                </D>
                <D n:c="C" n:f="1" n:r="0" n:did="2">
                     <n:Q>5</n:Q>
                    <n:St>2</n:St>
                </D>
             ...
                <D n:c="C" n:f="100" n:r="0" n:did="2">
                    <n:Q>5</n:Q>
                    <n:St>2</n:St>
                </D>
            </C>
            <C n:t="4">
                <D n:c="C" n:f="1" n:r="0" n:did="1">
                    <n:Q>1</n:Q>
                    <n:St>2</n:St>
                </D>
            ...
                <D n:c="C" n:f="5" n:r="0" n:did="1">
                    <n:Q>2</n:Q>
                    <n:St>2</n:St>
                </D>
                <D n:c="U" n:f="20" n:r="0" n:did="1">
                    <n:Q>1</n:Q>
                    <n:St>2</n:St>
                </D>
            ...
                <D n:c="U" n:f="5" n:r="0" n:did="1">
                    <n:Q>3</n:Q>
                    <n:St>2</n:St>
                </D>
                <D n:c="C" n:f="1" n:r="0" n:did="2">
                    <n:Q>2</n:Q>
                    <n:St>2</n:St>
                </D>
            ...
                <D n:c="C" n:f="100" n:r="0" n:did="2">
                    <n:Q>3</n:Q>
                    <n:St>2</n:St>
                </D>
            </C>
            <CUS n:did="1">
                <CU n:u="1a" n:st="22" n:nf="90" n:ne="10" n:max="110"/>
                <CU n:u="1" n:st="0" n:nf="100" n:ne="9" n:mx="105">
                    <D n:c="C" n:f="1" n:r="0" n:did="1">
                        <n:Q>3</n:Q>
                        <n:St>2</n:St>
                     </D>
                ...
                    <D n:c="U" n:f="5" n:r="0" n:did="1">
                        <n:Q>12</n:Q>
                        <n:St>2</n:St>
                    </D>
                </CU>
                <CU n:u="2" n:st="0" n:nf="100" n:ne="9" n:mx="105"/>
            ...
                <CU n:u="10" n:st="0" n:nf="100" n:ne="9" n:mx="105"/>
            </CUS>
            <CUS n:did="1">
                <CU n:u="1" n:st="0" n:nf="100" n:ne="9" n:mx="105">
                    <D n:c="C" n:f="1" n:r="0" n:did="2">
                        <n:Q>3</n:Q>
                        <n:St>2</n:St>
                    </D>
                ...
                    <D n:c="C" n:f="5" n:r="0" n:did="2">
                        <n:Q>12</n:Q>
                        <n:St>2</n:St>
                    </D>
                </CU>
                <CU n:u="2" n:st="0" n:nf="100" n:ne="9" n:mx="105"/>
            ...
                <CU n:u="10" n:st="0" n:nf="100" n:ne="9" n:mx="105">
                    <D n:c="C" n:f="1" n:r="0" n:did="2">
                        <n:Q>4</n:Q>
                        <n:St>2</n:St>
                    </D>
                ...
                    <D n:c="C" n:f="5" n:r="0" n:did="2">
                        <n:Q>10</n:Q>
                        <n:St>2</n:St>
                    </D>
                </CU>
            </CUS>
        </n:IR>
    </soapenv:B>
</soapenv:E>

Here is the start of the XSL file in which I am trying to isolate the section of the XML file that I need:

<?xml version = "1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version = "1.0"
                         xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope"
                         xmlns:n="http://abc.xxx/bb.xsd">

<xsl:output method="xml" indent="yes"/>
    <xsl:template match="*">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:for-each select="//CU">
                <xsl:copy/>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="C"/>
</xsl:stylesheet>

The result I get is:

<?xml version="1.0"?>
<soapenv:E xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:B>
<n:IR xmlns:n="http://abc.xxx/bb.xsd">
<n:D>2016/02/11 09:11:15</n:D>
<n:S>1</n:S>
<CUS>
<CU>
<D>
<n:Q>0</n:Q>
<n:St>0</n:St>
</D>
</CU>
<CU>
<D>
<n:Q>0</n:Q>
<n:St>0</n:St>
</D>
</CU>
....
</CUS>
</n:IR>
</soapenv:B>
</soapenv:E>

With this results, I no longer have the ability to filter nodes to extract the nodes I need to add data from.
Any suggestions?


Sorry for the typo n:Date should have been n:D above. The expected result should be something similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:E xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:B>
  <n:IR xmlns:n="http://abc.xxx/bb.xsd" n:result="0">
   <n:Date>2016/02/11 09:11:15</n:Date>
   <n:S>1</n:S>
   <CUS n:did="1">
    <CU n:u="1" n:st="0" n:nf="100" n:ne="9" n:mx="105">
     <D n:c="C" n:f="5" n:r="0" n:did="1">
      <n:Q>3</n:Q>
      <n:St>2</n:St>
     </D>
     <D n:c="C" n:f="10" n:r="0" n:did="1">
      <n:Q>2</n:Q>
      <n:St>2</n:St>
     </D>
     <D n:c="C" n:f="20" n:r="0" n:did="1">
      <n:Q>5</n:Q>
      <n:St>2</n:St>
     </D>
    </CU>
    <CU n:u="2" n:st="0" n:nf="100" n:ne="9" n:mx="105">
     <D n:c="C" n:f="5" n:r="0" n:did="1">
      <n:Q>1</n:Q>
      <n:St>2</n:St>
     </D>
     <D n:c="C" n:f="20" n:r="0" n:did="1">
      <n:Q>1</n:Q>
      <n:St>2</n:St>
    </D>
    <D n:c="C" n:f="50" n:r="0" n:did="1">
     <n:Q>3</n:Q>
     <n:St>2</n:St>
    </D>
   </CU>
  </CUS>
 </n:IR>
</soapenv:B>
</soapenv:E>

But the end result is a text format like this

2016/02/11 09:11:15,1,4,2,6,3

Where 4 is the sum n:Q from CU n:u='1' and n:U='2' for n:c='C', n:f='5' and n:did='1' and so on for the other values.

YPC
  • 11
  • 3
  • Your question is not clear. Please show us the expected output, and clarify what you mean by "add all n:Q value for similar n:f from different nodes". There are also too many issues here for one question, IMHO. And there is no `n:Date` in your input, so that's a no-go right away. – michael.hor257k Mar 04 '16 at 16:01
  • I have correct the n:Date to n:D in the required out info. I also added the expected result and the end game a text output format. – YPC Mar 04 '16 at 17:56
  • I am afraid it's still to cryptic for me. – michael.hor257k Mar 04 '16 at 18:22

1 Answers1

1

I am afraid I can only give you a starting point. The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n="http://abc.xxx/bb.xsd">
<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>

<xsl:template match="n:IR">
    <xsl:value-of select="n:D" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="n:S" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="sum(CUS[@n:did='1']/CU[@n:u='1']/D[@n:c='C']/n:Q)" />
    <!-- ... -->
</xsl:template>

</xsl:stylesheet>

applied to your example input, will return:

2016/02/11 09:11:15,1,18

I suggest you run with this and post additional specific question as you encounter specific problems.

P.S. I don't see why you would need an interim XMl result, if your final result needs to be text.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks for the kick start. And you are right, the interim XML result was only them for building the XSL purposes. – YPC Mar 04 '16 at 19:10