2010-08-13 60 views
1

我想這個文件轉換:XSL轉換新手需要幫助扁平化的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<x12errors> 
    <header> 
    <errors> 
     <issue> 
     <message>Wrong value</message> 
     <path>path to bad value</path> 
     </issue> 
     <issue> 
     <message>Missing value</message> 
     <path>path where missing value should be</path> 
     </issue> 
    </errors> 
    <warnings> 
     <issue> 
     <message>Value too small</message> 
     <path>path to value</path> 
     </issue> 
    </warnings> 
    </header> 
    <boxes> 
    <box> 
     <boxName>cardboard</boxName> 
     <boxStyleNum>12345</boxStyleNum> 
     <errors> 
     <issue> 
      <message>Box too small</message> 
      <path>path to box size</path> 
     </issue> 
     </errors> 
     <warnings> 
     <issue> 
      <message>Box flaps off center</message> 
      <path>path to box measurements</path> 
     </issue> 
     </warnings> 
     <wrappings> 
     <wrapping> 
      <material>bubble wrap</material> 
      <dimensions>9x12</dimensions> 
      <errors> 
      <issue> 
       <message>Wrong material</message> 
       <path>path</path> 
      </issue> 
      </errors> 
      <warnings> 
      <issue> 
       <message>Prefer different color</message> 
       <path>path to value</path> 
      </issue> 
      </warnings> 
     </wrapping> 
     </wrappings> 
    </box> 
    </boxes> 
</x12errors> 

此文件:

<?xml version="1.0" encoding="utf-8"?> 
<x12errors> 
    <header> 
    <headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg> 
    <headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg> 
    <headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg> 
    </header> 
    <boxes> 
    <box> 
     <boxName>cardboard</boxName> 
     <boxStyleNum>12345</boxStyleNum> 
     <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg> 
     <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg> 
     <wrappings> 
     <wrapping> 
      <material>bubble wrap</material> 
      <dimensions>9x12</dimensions> 
      <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg> 
      <wrappingMsg><type>E</type><msgText>Prefer different color</msgText></wrappingMsg> 
     </wrapping> 
     </wrappings> 
    </box> 
    </boxes> 
</x12errors> 

我有以下的XSL文件,該文件關閉,但它留下在那裏的「錯誤」和「警告」標籤,我不明白爲什麼!誰能幫我?

這裏是我當前的xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="xml" indent="no"/> 

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

    <xsl:template match="header//errors/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="header//errors/issue/message"> 
    <headerMsg> 
     <type>E</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </headerMsg> 
    </xsl:template> 

    <xsl:template match="header/warnings/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="header//warnings/issue/message"> 
    <headerMsg> 
     <type>W</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </headerMsg> 
    </xsl:template> 

    <xsl:template match="boxs"> 
    <xsl:apply-templates select="box"/> 
    </xsl:template> 

    <xsl:template match="box/errors/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="box//errors/issue/message"> 
    <boxMsg> 
     <xsl:apply-templates select="../../boxName"/> 
     <xsl:apply-templates select="../../boxStyle"/> 
     <type>E</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </boxMsg> 
    </xsl:template> 

    <xsl:template match="box/warnings/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="box//warnings/issue/message"> 
    <boxMsg> 
     <xsl:apply-templates select="../../boxName"/> 
     <xsl:apply-templates select="../../boxStyle"/> 
     <type>W</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </boxMsg> 
    </xsl:template> 

    <xsl:template match="wrappings"> 
    <xsl:apply-templates select="wrapping"/> 
    </xsl:template> 

    <xsl:template match="wrapping/errors/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="wrapping//errors/issue/message"> 
    <wrappingMsg> 
     <xsl:apply-templates select="../../material"/> 
     <xsl:apply-templates select="../../dimensions"/> 
     <type>E</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </wrappingMsg> 
    </xsl:template> 

    <xsl:template match="wrapping/warnings/issue"> 
    <xsl:apply-templates select="message"/> 
    </xsl:template> 

    <xsl:template match="wrapping//warnings/issue/message"> 
    <wrappingMsg> 
     <xsl:apply-templates select="../../material"/> 
     <xsl:apply-templates select="../../dimensions"/> 
     <type>W</type> 
     <xsl:element name="msgText"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </wrappingMsg> 
    </xsl:template> 


</xsl:stylesheet> 

謝謝! Laurie

+0

哪個解析器是你在用嗎? – 2010-08-13 18:05:48

+0

Visual Studio 2008 – Laurie 2010-08-13 18:44:41

回答

0

我想你是在問'開始時所有的節點。你會得到很多原創的東西。只需在您的XSLT刪除此:

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

我能得到一個輸出:

<?xml version="1.0" encoding="utf-8"?> 
<headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg> 
<headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg> 
<headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg> 
     cardboard 
     12345 
     <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg> 
     <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg> 
     bubble wrap 
     9x12 
     <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg> 
     <wrappingMsg><type>W</type><msgText>Prefer different color</msgText></wrappingMsg> 
+0

我也嘗試過 - 但我不知道如何只需要我想要的節點。我對xsl知之甚少,並且幾乎完全通過反覆試驗發現了我到目前爲止所擁有的東西。 – Laurie 2010-08-13 18:45:29

2

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:m="message" exclude-result-prefixes="m"> 
    <m:header> 
     <m:message type="E">Wrong value</m:message> 
     <m:message type="E">Missing value</m:message> 
     <m:message type="W">Value too small</m:message> 
    </m:header> 
    <m:box> 
     <m:message type="E">Box too small</m:message> 
     <m:message type="W">Box flaps off center</m:message> 
    </m:box> 
    <m:wrapping> 
     <m:message type="E">Wrong material</m:message> 
     <m:message type="E">Prefer different color</m:message> 
    </m:wrapping> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="path"/> 
    <xsl:template match="errors|warnings|issue"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="message"> 
     <xsl:element name="{local-name(../../..)}Msg"> 
      <type> 
       <xsl:value-of select="document('')/*/m:*[local-name()=local-name(current()/../../..)]/*[.=current()]/@type"/> 
      </type> 
      <msgText> 
       <xsl:value-of select="."/> 
      </msgText> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<x12errors> 
    <header> 
     <headerMsg> 
      <type>E</type> 
      <msgText>Wrong value</msgText> 
     </headerMsg> 
     <headerMsg> 
      <type>E</type> 
      <msgText>Missing value</msgText> 
     </headerMsg> 
     <headerMsg> 
      <type>W</type> 
      <msgText>Value too small</msgText> 
     </headerMsg> 
    </header> 
    <boxes> 
     <box> 
      <boxName>cardboard</boxName> 
      <boxStyleNum>12345</boxStyleNum> 
      <boxMsg> 
       <type>E</type> 
       <msgText>Box too small</msgText> 
      </boxMsg> 
      <boxMsg> 
       <type>W</type> 
       <msgText>Box flaps off center</msgText> 
      </boxMsg> 
      <wrappings> 
       <wrapping> 
        <material>bubble wrap</material> 
        <dimensions>9x12</dimensions> 
        <wrappingMsg> 
         <type>E</type> 
         <msgText>Wrong material</msgText> 
        </wrappingMsg> 
        <wrappingMsg> 
         <type>E</type> 
         <msgText>Prefer different color</msgText> 
        </wrappingMsg> 
       </wrapping> 
      </wrappings> 
     </box> 
    </boxes> 
</x12errors> 

編輯1:更好的解釋。 注意:「身份轉換」(模板[@ name ='idenity'])只是照原樣複製輸入源。某些元素被繞過(不是複製,而是將模板應用於孩子):errors,warningsissuepath元素帶有空模板條紋。內聯映射:document('')評估爲樣式表文檔根;除XSLT命名空間之外的其他名稱空間中的頂級元素被處理器忽略,但我們可以選擇它們(在本例中,本地名稱等於message grand grand father的本地名稱,然後是那些具有字符串值的子元素等於message字符串值,最後他們的type屬性)

+0

謝謝!!!!!!!!! – Laurie 2010-08-13 19:29:09

+0

哇!這是一個流暢的解決方案。你可以通過走過它來啓發它嗎?我會很高興。你已經有了我的+1。 – 2010-08-13 19:34:52

+0

@Alejandro:非常感謝 – 2010-08-16 13:33:22