XSL中的动态父头?

时间:2015-05-15 04:52:04

标签: xml xslt

我有一组使用XSLTproc处理成.CSV文件的几个.XML文件。

我已经设法找到了如何构建动态子标题,但我可以为父标题做同样的事情吗?

这是我到目前为止所得到的:

XML:

<ArrayOfVW_Retailer_Consumer_Meter_Details_Addresses xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <VW_Retailer_Consumer_Meter_Details_Addresses>
    <AddressLine2>SECTOR F-2, MIRPUR</AddressLine2>
    <BillingDate>1</BillingDate>
    <BlockOrPostalStopNumber />
    <City>MIRPUR</City>
    <ConsumerContactID>b09a8b6e-84f9-4d15-a62d-4074a7224690</ConsumerContactID>
    <ConsumerID>173410</ConsumerID>
    <ConsumptionAboveAverageLimit>1.30</ConsumptionAboveAverageLimit>
    <ConsumptionBelowAverageLimit>0.700</ConsumptionBelowAverageLimit>
    <CountryRegion>PAKISTAN</CountryRegion>
    <ExcessLoadLimit>1.300</ExcessLoadLimit>
    <Exteral_Reading_Multiplier>1.000</Exteral_Reading_Multiplier>
    <FirstName>ABC</FirstName>
    <HasBreakerInstalled>false</HasBreakerInstalled>
    <Interal_Reading_Multiplier>1.000</Interal_Reading_Multiplier>
    <IsPrePaid>false</IsPrePaid>
    <LastName i:nil="true" />
    <MasterMeterID>T10_SHA_HAM</MasterMeterID>
    <MeterID>BHR141400258</MeterID>
    <Meter_In_Service_End_Date i:nil="true" />
    <Meter_In_Service_End_Reading i:nil="true" />
    <Meter_In_Service_Start_Date>2014-08-23T00:00:00</Meter_In_Service_Start_Date>
    <Meter_In_Service_Start_Reading>0.000</Meter_In_Service_Start_Reading>
    <MiddleName i:nil="true" />
    <PostalCode />
    <RecID>758b4798-f939-4d91-89a9-a341b9b94b2d</RecID>
    <Remarks i:nil="true" />
    <Salution>Mr</Salution>
    <ServiceLocation>ffda46df-6e49-4e64-a341-002cfed87fa0</ServiceLocation>
    <StateProvince>KASHMIR</StateProvince>
    <Suffix i:nil="true" />
    <UnitOrHouseNumber />
  </VW_Retailer_Consumer_Meter_Details_Addresses>
</ArrayOfVW_Retailer_Consumer_Meter_Details_Addresses>

XSL:

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

  <xsl:template match="/*">
    <xsl:for-each select="*[1]/*">
      <xsl:text>"</xsl:text>
      <xsl:value-of select="name()" />
      <xsl:text>"</xsl:text>
      <xsl:if test="position() != last()">,</xsl:if>
      <xsl:if test="position() = last()">
        <xsl:text> </xsl:text>
      </xsl:if>
    </xsl:for-each>
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="/impex">
    <xsl:apply-templates select="VW_Retailer_Consumer_Meter_Details_Addresses[1]" mode="header" />
    <xsl:apply-templates select="VW_Retailer_Consumer_Meter_Details_Addresses" />
  </xsl:template>

  <xsl:template match="*" mode="header">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="$headerVal" />
    <xsl:text>"</xsl:text>
  </xsl:template>

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

  <xsl:template match="*">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>"</xsl:text>
    <xsl:choose>
      <xsl:when test="position()=last()">
        <xsl:text> </xsl:text>
      </xsl:when>
      <xsl:otherwise>,</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

如果父标题ArrayOfVW_Retailer_Consumer_Meter_Details_Addresses更改为其他内容,我需要在XSL中进行哪些更改才能使其正常工作? 谢谢!

1 个答案:

答案 0 :(得分:0)

这个怎么样:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" />

  <xsl:variable name="q" select="'&quot;'" />
  <xsl:variable name="d" select="','" />

  <xsl:template match="/*">
    <xsl:for-each select="*[1]">
      <xsl:apply-templates select="*" mode="header" />
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
    <xsl:for-each select="*">
      <xsl:apply-templates select="*" mode="data" />
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*" mode="header">
    <xsl:if test="position() &gt; 1"><xsl:value-of select="$d" /></xsl:if>
    <xsl:value-of select="concat($q, name(), $q)" />
  </xsl:template>

  <xsl:template match="*" mode="data">
    <xsl:if test="position() &gt; 1"><xsl:value-of select="$d" /></xsl:if>
    <xsl:value-of select="concat($q, ., $q)" />
  </xsl:template>
</xsl:stylesheet>