2016-03-04 61 views
1

我對這一切都很陌生,並嘗試過各種途徑,但我無法提取我需要的數據。我正在使用的XML文件是由一塊硬件自動生成的,其大小通常約爲35Kb。在XML文件中有一個對我沒有的XSD文件的引用。是XSL後丟失XML結構

,我需要提取的信息如下:

 
    -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 

正如你可以看到下面這是一個非常複雜的結構。每次我測試節點上的條件時,我都會丟失輸出中的整個XML結構。

下面是XML文件的一部分:

<?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> 

下面是XSL文件中,我試圖孤立的XML文件,我需要的部分開始:

<?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> 

結果我得到的是:

<?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> 

有了這個結果,我再也不用過濾器來爲Extrac節點的能力t我需要添加數據的節點。
有什麼建議嗎?


對不起,錯字n:日期應該是n:D以上。 預期的結果應該與此類似:

<?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> 

但最終的結果是這樣的

2016年2月11日09文本格式:11:15,1,4,2,6 ,3:

其中4是n:c ='C',n:f ='5'和n:1的n:u ='1' did ='1'等其他值。

+0

你的問題不清楚。請向我們展示預期輸出,並通過「爲來自不同節點的類似n:f添加所有n:Q值」來澄清您的意思。對於一個問題,這裏也有太多問題,恕我直言。在你的輸入中沒有'n:Date',所以這是一個不可行的。 –

+0

我有正確的n:日期n:D在所需的信息。我還添加了預期結果和最終遊戲文本輸出格式。 – YPC

+0

恐怕這對我來說仍然是神祕的。 –

回答

0

恐怕我只能給你一個出發點。下面的樣式表:

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> 

應用到你的榜樣輸入,將返回:

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

我建議你用這個和後附加的具體問題上運行,你遇到的特定問題。

P.S.如果您的最終結果需要爲文本,我不明白爲什麼您需要臨時XMl結果。

+0

感謝您的啓動。你是對的,臨時XML結果只是他們構建XSL的目的。 – YPC