2017-09-26 19 views
1

輸入XML分組XML屬性:用XSLT

<?xml version="1.0" encoding="UTF-8"?><svg height="3553.21" width="9358.09" viewBox="0 0 9358.09 3553.21" xmlns="http://www.w3.org/2000/svg"> 
 
<rect fill="#16202c" x="0" y="0" width="9358.09" height="3553.21"/> 
 
\t <g transform="scale(0.7)> 
 
\t \t <g id="0-0-0" class="classPolyline" type="twycenterline" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="218.36444444444453,127.61111111111109 "/> 
 
\t \t </g> 
 
\t \t <g id="1-0-0" class="classPolyline" type="twycenterline" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="462.22222222222223,127.14222222222219 "/> 
 
\t \t </g> 
 
\t \t <g id="2-0-0" class="classPolyline" type="twy" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="462.22222222222223,127.14222222222219 "/> 
 
\t \t </g> 
 
\t \t <g id="3-0-0" class="classPolyline" type="circle" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <text class="classText" id="12200-0-0" type="lamp_label_only" fill="#ffffff">20L-AP2-065A</text> 
 
\t \t </g> 
 
\t \t <g id="4-0-0" class="classPolyline" type="circle" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <text class="classText" id="12201-0-0" type="lamp_label_only" fill="#ffffff">WW8c-008B</text> 
 
\t \t </g> 
 
\t </g> 
 
</svg>

預計產量:我曾經參與

<?xml version="1.0" encoding="UTF-8"?><svg height="3553.21" width="9358.09" viewBox="0 0 9358.09 3553.21" xmlns="http://www.w3.org/2000/svg"> 
 
<rect fill="#16202c" x="0" y="0" width="9358.09" height="3553.21"/> 
 
<g transform="scale(0.7)> 
 
\t <g type="twycenterline"> 
 
\t \t <g id="0-0-0" class="classPolyline" type="twycenterline" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="218.36444444444453,127.61111111111109 "/> 
 
\t \t </g> 
 
\t \t <g id="1-0-0" class="classPolyline" type="twycenterline" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="462.22222222222223,127.14222222222219 "/> 
 
\t \t </g> 
 
\t </g> 
 
\t <g type="twy"> 
 
\t \t <g id="2-0-0" class="classPolyline" type="twy" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <polyline fill="none" stroke="#292929" stroke-width="0.2" fill-opacity="1" points="462.22222222222223,127.14222222222219 "/> 
 
\t \t </g> 
 
\t </g> 
 
\t <g type="circle"> 
 
\t \t <g id="3-0-0" class="classPolyline" type="circle" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <text class="classText" id="12200-0-0" type="lamp_label_only" fill="#ffffff">20L-AP2-065A</text> 
 
\t \t </g> 
 
\t \t <g id="4-0-0" class="classPolyline" type="circle" nameObject="WW12" sizeMin="" sizeMax=""> 
 
\t \t \t <text class="classText" id="12201-0-0" type="lamp_label_only" fill="#ffffff">WW8c-008B</text> 
 
\t \t </g> 
 
\t </g> 
 
</g> 
 
</svg>

我的XSLT文件。不知道什麼是對這個

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

    <xsl:key name="type1" match="g" use="@type" /> 

    <xsl:template match="/"> 
     <svg xmlns="http://www.w3.org/2000/svg" > 
      <xsl:variable name="viewWidth"><xsl:value-of select=".//@width"/></xsl:variable> 
      <xsl:variable name="viewHeight"><xsl:value-of select=".//@height"/></xsl:variable> 

      <xsl:attribute name="height"><xsl:value-of select="$viewHeight" /></xsl:attribute> 
      <xsl:attribute name="width"><xsl:value-of select="$viewWidth" /></xsl:attribute> 

      <g> 
       <xsl:variable name="trans"><xsl:value-of select=".//@transform"/></xsl:variable> 
       <xsl:attribute name="transform"><xsl:value-of select="$trans" /></xsl:attribute> 

       <xsl:variable name="type"><xsl:value-of select=".//@type"/></xsl:variable> 

       <xsl:comment>trans <xsl:copy-of select="$trans" /></xsl:comment> 
       <xsl:comment>type <xsl:copy-of select="$type" /></xsl:comment> 


       <xsl:apply-templates select="g[generate-id(.)=generate-id(key('type1',@type)[1])]"/> 
      </g> 
     </svg> 
    </xsl:template> 

    <xsl:template match="g"> 
     <xsl:comment>template <xsl:copy-of select="template" /></xsl:comment> 
     <g value="{@type}"> 
      <xsl:comment>g1111 <xsl:copy-of select="g11111" /></xsl:comment> 
      <xsl:for-each select="key('type1', @type)"> 
       <xsl:comment><xsl:copy-of select="@type" /></xsl:comment> 

      </xsl:for-each> 
     </g> 

    </xsl:template> 


</xsl:stylesheet> 

去錯我已經閱讀這許多博客但不是我不能夠創建組。以下是詳細信息。 輸入需要轉換爲預期輸出的XML。下面我共享了輸入和輸出XMl和我的XSLT文件。 任何幫助,請....

回答

1

你有兩個問題,你的XSLT

  1. 您最初的模板匹配「/」,這是文檔節點。但是,當您做xsl:apply-templates時,選擇不是文檔節點的子節點的「g」(「svg」是文檔節點的子節點)。爲了解決這個問題,你可以改變模板匹配爲「/ *」。

  2. 您尚未在您的XSLT中計入名稱空間。 XML中的所有元素都在「http://www.w3.org/2000/svg」命名空間中,但在XSLT中,您正在尋找沒有命名空間的元素。爲了解決這個問題,你需要在XSLT中聲明名稱空間(帶有前綴),然後在匹配任何元素時使用該前綴。

它也可以幫助,如果你使用的身份模板在你的XSLT,以幫助複製現有元素,因爲這將有助於簡化您的XSLT了很多。

試試這個XSLT

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

    <xsl:key name="type1" match="svg:g[@type]" use="@type" /> 

    <!-- Matches the first distinct 'type' attribute --> 
    <xsl:template match="svg:g[@transform]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*" /> 
      <xsl:for-each select="svg:g[generate-id()=generate-id(key('type1',@type)[1])]"> 
       <xsl:copy> 
        <xsl:attribute name="type"> 
         <xsl:value-of select="@type" /> 
        </xsl:attribute> 
        <xsl:apply-templates select="key('type1', @type)" /> 
       </xsl:copy> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 

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

感謝...它的工作。你可以建議任何鏈接或書籍,以提高我的XSLT技能。我只是一個begginer。 –

+0

https://stackoverflow.com/questions/3511759/where-can-i-find-a-good-tutorial-on-xslt-files –

+0

我稍微改變了我的輸出結果...你能幫我改變一下嗎?被要求得到輸出.. –