嵌套记录上的XSL转换未按预期进行转换

时间:2018-07-03 18:06:31

标签: xml xslt

我正在使用xslt使用for-each转换嵌套记录,并将结果写入2条记录并将其输出为文本文件。

XML:

<ad:Sale_Data xmlns:ad="urn:com.ad.report/saleslead">
<ad:Sale_Tx_Entry>
    <ad:name>prominent sale</ad:name>
    <ad:businessID>209320484</ad:businessID>
    <ad:Lead_Organization ad:Descriptor="Chief Executive Officer">
        <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
        <ad:ID ad:type="Business_Model_Sale_Ref_ID">475846759</ad:ID>
    </ad:Lead_Organization>
    <ad:Management_Hierarchy>
        <ad:Lead_Organization_ID>475847049</ad:Lead_Organization_ID>
    </ad:Management_Hierarchy>
    <ad:storemanager ad:Descriptor="J Loews">
        <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
        <ad:ID ad:type="S_ID">754954</ad:ID>
    </ad:storemanager>
    <ad:SalePerson>
        <ad:Sale_Ref_ID>3457578</ad:Sale_Ref_ID>
        <ad:Sale_Profile ad:Descriptor="Sale Person1">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="Sale_Tx_ID">475847</ad:ID>
        </ad:Sale_Profile>
        <ad:Business_Model ad:Descriptor="3457578 Sale Person1 - Business Development Leader1">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
        </ad:Business_Model>
        <ad:SalePerson ad:Descriptor="Business Development Leader1">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="S_ID">4782</ad:ID>
        </ad:SalePerson>
        <ad:StoreManager ad:Descriptor="J Loews">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="S_ID">754954</ad:ID>
        </ad:StoreManager>
    </ad:SalePerson>
    <ad:SalePerson>
        <ad:Sale_Ref_ID>3457579</ad:Sale_Ref_ID>
        <ad:Sale_Profile ad:Descriptor="Sale Person2">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="Sale_Tx_ID">918328</ad:ID>
        </ad:Sale_Profile>
        <ad:Business_Model ad:Descriptor="3457579 Sale Person2 - Business Development Leader2">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
        </ad:Business_Model>
        <ad:SalePerson ad:Descriptor="Business Development Leader2">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="S_ID">487548</ad:ID>
        </ad:SalePerson>
        <ad:StoreManager ad:Descriptor="J Loews">
            <ad:ID ad:type="AID">sadlksalfkdki23i204urfdfmd9mdscdkhytt</ad:ID>
            <ad:ID ad:type="S_ID">754954</ad:ID>
        </ad:StoreManager>
    </ad:SalePerson>
</ad:Sale_Tx_Entry>

XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ad="urn:com.ad.report/saleslead">
    <xsl:output method="text"/>
    <xsl:variable name="linefeed" select="'&#xA;'"></xsl:variable>
    <xsl:template match="/">
        <xsl:text>"BusinessName"|"BusinessCode"|"BusinessStructure"|"BusinessStructureID"|"BusinessStructureManager"|"StoreID"|"SaleProfile"|"BusinessModel"|"SalePerson"|"StoreManager"</xsl:text>
        <xsl:value-of select="$linefeed"/>
        <xsl:for-each select="/ad:Sale_Data/ad:Sale_Tx_Entry">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="ad:name"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:businessID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:Lead_Organization/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>"
            <xsl:value-of select="ad:Management_Hierarchy/ad:Lead_Organization_ID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:storemanager/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/ad:Sale_Ref_ID"/>  
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/ad:Sale_Profile/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/ad:Business_Model/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:StoreManager/@ad:Descriptor"/> 
            <xsl:text>"</xsl:text>
            <xsl:value-of select="$linefeed"/>
        </xsl:for-each> 
    </xsl:template>    
</xsl:stylesheet>

结果:

"BusinessName"|"BusinessCode"|"BusinessStructure"|"BusinessStructureID"|"BusinessStructureManager"|"StoreID"|"SaleProfile"|"BusinessModel"|"SalePerson"|"StoreManager"
"prominent sale"|"209320484"|"Chief Executive Officer"|""475847049"|"J Loews"|"3457578 3457579"|"Sale Person1 Sale Person2"|"3457578 Sale Person1 - Business Development Leader1 3457579 Sale Person2 - Business Development Leader2"|""|""

预期结果:

"BusinessName"|"BusinessCode"|"BusinessStructure"|"BusinessStructureID"|"BusinessStructureManager"|"StoreID"|"SaleProfile"|"BusinessModel"|"SalePerson"|"StoreManager"

"prominent sale"|"209320484"|"Chief Executive Officer"|""475847049"|"J Loews"|"3457578"|"Sale Person1"|"3457578 Sale Person1 - Business Development Leader1"|""|""

"prominent sale"|"209320484"|"Chief Executive Officer"|""475847049"|"J Loews"|"3457579"|"Sale Person2"|"3457579 Sale Person2 - Business Development Leader2"|""|""

2 个答案:

答案 0 :(得分:2)

只需沿着树走到 SalePerson 级别,需要两个模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ad="urn:com.ad.report/saleslead">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="linefeed" select="'&#xA;'"></xsl:variable>
    <xsl:template match="/ad:Sale_Data">
        <xsl:text>"BusinessName"|"BusinessCode"|"BusinessStructure"|"BusinessStructureID"|"BusinessStructureManager"|"StoreID"|"SaleProfile"|"BusinessModel"|"SalePerson"|"StoreManager"</xsl:text>
        <xsl:apply-templates select="ad:Sale_Tx_Entry/ad:SalePerson"/>
    </xsl:template>

    <xsl:template match="ad:SalePerson">
        <xsl:value-of select="$linefeed"/>
            <xsl:text>"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Tx_Entry/ad:name"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Tx_Entry/ad:businessID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Tx_Entry/ad:Lead_Organization/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Tx_Entry/ad:Management_Hierarchy/ad:Lead_Organization_ID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Tx_Entry/ad:storemanager/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:Sale_Ref_ID"/>  
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:Sale_Profile/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:Business_Model/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:StoreManager/@ad:Descriptor"/> 
            <xsl:text>"</xsl:text>
     </xsl:template>    
</xsl:stylesheet>

XSLT Fiddle Demo (输出在左下方)

答案 1 :(得分:1)

<xsl:output method="text"/>
    <xsl:variable name="linefeed" select="'&#xA;'"></xsl:variable>
    <xsl:template match="/">
        <xsl:text>"BusinessName"|"BusinessCode"|"BusinessStructure"|"BusinessStructureID"|"BusinessStructureManager"|"StoreID"|"SaleProfile"|"BusinessModel"|"SalePerson"|"StoreManager"</xsl:text>
        <xsl:value-of select="$linefeed"/>
        <xsl:for-each select="/ad:Sale_Data/ad:Sale_Tx_Entry/ad:SalePerson/ad:Sale_Profile[@ad:Descriptor='Sale Person1']">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:name"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:businessID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:Lead_Organization/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>"
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:Management_Hierarchy/ad:Lead_Organization_ID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:storemanager/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:if test="@ad:Descriptor='Sale Person1'">
            <xsl:value-of select="@ad:Descriptor"/>  
            </xsl:if>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:SalePerson/ad:Business_Model[ preceding-sibling::ad:Sale_Profile[@ad:Descriptor='Sale Person1']]/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:StoreManager/@ad:Descriptor"/> 
            <xsl:text>"</xsl:text>
            <xsl:value-of select="$linefeed"/>
        </xsl:for-each>
        <xsl:for-each select="/ad:Sale_Data/ad:Sale_Tx_Entry/ad:SalePerson/ad:Sale_Profile[@ad:Descriptor='Sale Person2']">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:name"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:businessID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:Lead_Organization/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>"
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:Management_Hierarchy/ad:Lead_Organization_ID"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:storemanager/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:if test="@ad:Descriptor='Sale Person2'">
                <xsl:value-of select="@ad:Descriptor"/>  
            </xsl:if>

            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ancestor::ad:Sale_Data/ad:Sale_Tx_Entry/ad:SalePerson/ad:Business_Model[ preceding-sibling::ad:Sale_Profile[@ad:Descriptor='Sale Person2']]/@ad:Descriptor"/>
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:SalePerson/@ad:Descriptor"/> 
            <xsl:text>"|"</xsl:text>
            <xsl:value-of select="ad:StoreManager/@ad:Descriptor"/> 
            <xsl:text>"</xsl:text>
            <xsl:value-of select="$linefeed"/>
        </xsl:for-each>
    </xsl:template>

尝试