2010-08-10 112 views
2

我有以下xml被转换。但我不知道如何获得转换后的xml中的主驱动标签值。主驱动程序应该基于驱动程序节点的位置。有人能帮助我吗?xslt父元素位置

<drivers> 
    <driver> 
    <first_name>Doug</first_name> 
    <last_name>Harry</last_name> 
    <vehicles> 
    <vehicle> 
    <vin>4T1BB46K08</vin> 
    <year>2008</year> 
    </vehicle> 
     </vehicles> 
     <records/> 
    </driver> 
    <driver> 
     <first_name>Sherry</first_name> 
     <last_name>Bloom</last_name> 
     <vehicles> 
      <vehicle> 
       <vin>5TDZA23C06</vin> 
       <year>2006</year> 
      </vehicle> 
     </vehicles> 
     <records/> 
    </driver> 
</drivers> 

结果应该是

<Vehicles> 
    <vehicle> 
    <vin>4T1BB46K08</vin> 
    <year>2008</year> 
    <primarydriver>1</primarydriver> 
    </vehicle> 
    <vehicle> 
    <vin>5TDZA23C06</vin> 
    <year>2006</year> 
    <primarydriver>2</primarydriver> 
    </vehicle> 
</Vehicles> 
+0

您可以重新格式化XML以提高可读性吗? – developer 2010-08-10 19:54:12

+0

我不怎么格式化这个。任何帮助,将不胜感激。 – Amzath 2010-08-10 20:43:50

+0

你还没有定义什么是“主要驱动程序”?这是文档中具有VIN值的第一个驱动程序吗?如果是这样,你的XML文档不具有代表性:只有两个驱动程序,每个都有不同的VIN,所以它们都是主要的驱动程序。您需要发布一个XML文档,其中一些驱动程序不应该在结果中标记为主驱动程序。 – 2010-08-10 22:03:43

回答

1

这种转变:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="/*"> 
    <Vehicles> 
    <xsl:apply-templates/> 
    </Vehicles> 
</xsl:template> 

<xsl:template match="driver"> 
    <xsl:apply-templates> 
    <xsl:with-param name="pDriverPos" select="position()"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="vehicles"> 
    <xsl:param name="pDriverPos"/> 

    <xsl:apply-templates> 
    <xsl:with-param name="pDriverPos" select="$pDriverPos"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="vehicle"> 
    <xsl:param name="pDriverPos"/> 

    <vehicle> 
    <xsl:apply-templates/> 
    <primarydriver><xsl:value-of select="$pDriverPos"/></primarydriver> 
    </vehicle> 
</xsl:template> 

<xsl:template match="first_name|last_name|records"/> 
</xsl:stylesheet> 

时所提供的XML文档应用:

<drivers> 
    <driver> 
    <first_name>Doug</first_name> 
    <last_name>Harry</last_name> 
    <vehicles> 
    <vehicle> 
    <vin>4T1BB46K08</vin> 
    <year>2008</year> 
    </vehicle> 
     </vehicles> 
     <records/> 
    </driver> 
    <driver> 
     <first_name>Sherry</first_name> 
     <last_name>Bloom</last_name> 
     <vehicles> 
      <vehicle> 
       <vin>5TDZA23C06</vin> 
       <year>2006</year> 
      </vehicle> 
     </vehicles> 
     <records/> 
    </driver> 
</drivers> 

产生想要的,正确的结果

<Vehicles> 
    <vehicle> 
     <vin>4T1BB46K08</vin> 
     <year>2008</year> 
     <primarydriver>1</primarydriver> 
    </vehicle> 
    <vehicle> 
     <vin>5TDZA23C06</vin> 
     <year>2006</year> 
     <primarydriver>2</primarydriver> 
    </vehicle> 
</Vehicles> 

待办事项:使用修改后的身份规则和传递当前驾驶员位置的参数。这比计算前面的兄弟姐妹效率更高。

0

该样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="drivers"> 
     <Vehicles> 
      <xsl:apply-templates/> 
     </Vehicles> 
    </xsl:template> 
    <xsl:template match="driver|driver/*|driver/*/text()"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="vehicle/*[last()]"> 
     <xsl:call-template name="identity"/> 
     <primarydriver> 
      <xsl:value-of select="count(preceding::driver)+1"/> 
     </primarydriver> 
    </xsl:template> 
</xsl:stylesheet> 

输出:

<Vehicles> 
    <vehicle> 
     <vin>4T1BB46K08</vin> 
     <year>2008</year> 
     <primarydriver>1</primarydriver> 
    </vehicle> 
    <vehicle> 
     <vin>5TDZA23C06</vin> 
     <year>2006</year> 
     <primarydriver>2</primarydriver> 
    </vehicle> 
</Vehicles> 

:计数之前。