2017-09-26 18 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

我稍微改变了我的输出结果...你能帮我改变一下吗?被要求得到输出.. –