Преобразование XSL для вложенных записей не преобразуется должным образом - PullRequest
0 голосов
/ 03 июля 2018

Я использую 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 голосов
/ 04 июля 2018
<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>

Попробуйте

0 голосов
/ 03 июля 2018

Просто спуститесь по дереву до уровня 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 (вывод в левом нижнем углу)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...