2016-04-25 118 views
1

所有內每個幫助包裝,XSLT新元素

我知道,這是很簡單的,但你對我作爲一個初學者,我不能得到它的權利。 :(請參見下面的預期產出應該是什麼正確XLST在此先感謝

輸入XML:?

<map> 
 
    <title>Lang's Commercial Leasing in Australia</title> 
 
    <topic id="io1529956sl235024462" /> 
 
    <topichead navtitle="PRECEDENT FINDING LISTS" id="io2559290sl622242477"> 
 
\t \t <topic id="io2558936sl197225260" /> 
 
\t \t <topic id="io2558936sl197225261" /> 
 
\t \t <topic id="io2558936sl197225262" /> 
 
    </topichead> 
 
</map>

XLST

<?xml version="1.0" encoding="UTF-8"?> 
 
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 
    \t <xsl:output method="xml" indent="yes" /> 
 
    \t <xsl:strip-space elements="*"/> 
 
    \t <xsl:template match="/"> 
 
    \t \t <xsl:apply-templates select="@*|node()" /> 
 
    \t </xsl:template> 
 
    \t <xsl:template match="@*|node()"> 
 
    \t \t <xsl:copy> 
 
    \t \t \t <xsl:apply-templates select="@*|node()" /> 
 
    \t \t </xsl:copy> 
 
    \t </xsl:template> 
 
    \t <xsl:template match="map/topichead/topic"> 
 
      <comm.intro> 
 
    \t \t \t <group> 
 
    \t \t \t <xsl:attribute name="link"><xsl:value-of select="@id" /></xsl:attribute> 
 
    \t \t \t <xsl:apply-templates select="node()" /> 
 
    \t \t \t </group> 
 
      </comm.intro> 
 
    \t </xsl:template> 
 
    </xsl:stylesheet>

輸出:

<map> 
 
    <title>Lang's Commercial Leasing in Australia</title> 
 
    <topic id="io1529956sl235024462" /> 
 
    <topichead navtitle="PRECEDENT FINDING LISTS" id="io2559290sl622242477"> 
 
    <comm.intro> 
 
\t \t <group link="io2558936sl197225260"> 
 
    </comm.intro> 
 
    <comm.intro> 
 
\t \t <group link="io2558936sl197225261"> 
 
    </comm.intro> 
 
    <comm.intro> 
 
\t \t <group link="io2558936sl197225262"> 
 
    </comm.intro> 
 
    </topichead> 
 
</map>

預期輸出:

<map> 
 
    <title>Lang's Commercial Leasing in Australia</title> 
 
    <topic id="io1529956sl235024462" /> 
 
    <topichead navtitle="PRECEDENT FINDING LISTS" id="io2559290sl622242477"> 
 
    <comm.intro> 
 
\t \t <group link="io2558936sl197225260"> 
 
\t \t <group link="io2558936sl197225261"> 
 
\t \t <group link="io2558936sl197225262"> 
 
    </comm.intro> 
 
    </topichead> 
 
</map>

回答

0

而不是

<xsl:template match="map/topichead/topic"> 
     <comm.intro> 
      <group> 
      <xsl:attribute name="link"><xsl:value-of select="@id" /></xsl:attribute> 
      <xsl:apply-templates select="node()" /> 
      </group> 
     </comm.intro> 
    </xsl:template> 

使用兩個模板

<xsl:template match="map/topichead"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <comm.intro> 
      <xsl:apply-templates/> 
     </comm.intro> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="map/topichead/topic"> 
     <group link="{@id}"> 
      <xsl:apply-templates/> 
     </group> 
    </xsl:template> 

而且<xsl:template match="/">你有你不需要的第一個模板,文檔節點的孩子是由內置的模板處理。

+0

非常感謝你。我非常感謝你的幫助。 –