简体   繁体   中英

XSLT document() not calling specific nodes

I am trying to call a 'firstName' node in my xsl code, however when I try to call the 'value-of select="firstName' it doesn't seem to work and once transformed, it does not display anything for it.

My xsl code is:

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

    <xsl:variable name="customers" select="document('customer.xml')/customers"/>

    <xsl:template match="/">
        <xsl:copy>
            <xsl:for-each select="transactions/transaction">
                <xsl:sort select="giftShop"/>
                <xsl:sort select="transaction_date"/>
                Shop: <xsl:value-of select="giftShop"/>
                Date: <xsl:value-of select="transaction_date"/>
                <xsl:for-each select="$customers">
                    <xsl:value-of select="."/>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

transactions.xml (first xml file):

<?xml version="1.0"?>
<transactions>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <customerID>8BLOJZWL</customerID>
        <giftShop>1</giftShop>
        <transactionID>BjYAtd7lmEOlQUVy</transactionID>
        <value currency="gbp">149.99</value>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <customerID>DR18OCFI</customerID>
        <giftShop>2</giftShop>
        <transactionID>aQ27lMvfnlzY4mkx</transactionID>
        <value currency="gbp">100.11</value>
    </transaction>
<transaction>

customer.xml (second xml file):

<?xml version="1.0"?>
<customers>
    <customer>
        <prefix>Mrs</prefix>
        <lastName>Samantha</lastName>
        <givenName>Smith</givenName>
        <addressID>213456</addressID>
        <customerID>ASJ4OTLG</customerID>
    </customer>
    <customer>
        <prefix>Mr </prefix>
        <lastName>Cameron</lastName>
        <givenName>Wills</givenName>
        <addressID>125907</addressID>
        <customerID>SID8RY23</customerID>
    </customer>
</customers>

Expected output: Also sorry if my expected output is not very accurate, I am very new to xml and xsl

<giftShop>
    <transaction_date>
        <transacation>
            <customer>

I am trying to call specific 'customer data', but at the moment it displays all the data and keeps crashing my Notepad++ when I try to close the file.

Any help is GREATLY appreciated!!

Since your document variable already references "customers"

<xsl:variable name="customers" select="document('customer.xml')/customers"/>

When you do your for loop

<xsl:for-each select="$customers">
  <xsl:value-of select="."/>
</xsl:for-each>

You should be doing something like this

<xsl:for-each select="$customers/customer">
  <xsl:value-of select="customerID"/> <!-- Or whichever node you want -->
</xsl:for-each>

I am guessing (:) you want to do something like this:

XSLT 1.0

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

<xsl:variable name="customers" select="document('retail_customer.xml')/customers"/>

<xsl:template match="/transactions">
    <xsl:for-each select="transaction">
        <xsl:sort select="giftShop"/>
        <xsl:sort select="transaction_date"/>
        <xsl:text>Shop: </xsl:text>
        <xsl:value-of select="giftShop"/>
        <xsl:text>&#10;Date: </xsl:text>
        <xsl:value-of select="transaction_date"/>
        <xsl:variable name="customer" select="$customers/customer[customerID=current()/customerID]"/>
        <xsl:text>&#10;Customer: </xsl:text>
        <xsl:value-of select="$customer/lastName"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$customer/givenName"/>
        <xsl:text>&#10;&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Of course, this can work only if a transaction has a matching customer in the other document. In your example, there are none.


PS Note that <xsl:sort select="transaction_date"/> will not work correctly in your case because your dates are in DD/MM/YYYY ( or MM/DD/YYYY ?) format, and sorting them alphabetically will not produce a chronological order. But that's a separate question (and you can find the answer here: https://stackoverflow.com/a/58251767/3016153 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM