I have a source xml, that looks like below:
<root>
<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
<False />
</CompoundPredicate>
<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
<SimplePredicate field="AGE" operator="lessOrEqual" value="40" />
<False />
</CompoundPredicate>
</root>
I want to perform a transformation in such way, that
1). if there is only 'False' element after the inner 'CompoundPredicate' element, then delete the outer 'CompoundPredicate' element and the element which appears after the inner 'CompoundPredicate' element. For example,
<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
<False />
</CompoundPredicate>
becomes
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
2) if there are other elements after the inner 'CompoundPredicate' element other than 'False', then only delete 'False' element which appears after the inner 'CompoundPredicate' element. For example,
<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
<SimplePredicate field="AGE" operator="lessOrEqual" value="40" />
<False />
</CompoundPredicate>
becomes
<CompoundPredicate booleanOperator="surrogate">
<CompoundPredicate booleanOperator="and">
<True />
<SimplePredicate field="MODELYEAR" operator="lessOrEqual" value="1999" />
</CompoundPredicate>
<SimplePredicate field="AGE" operator="lessOrEqual" value="40" />
</CompoundPredicate>
For this problem, I don`t even know how to start. I would really appreciate your help. Thanks a lot.
See if this points you in the right direction. It's based on a restatement of your rules, which may or may not be correct:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- for outer 'CompoundPredicate' that contains only inner 'CompoundPredicate' and/or 'False' -->
<xsl:template match="root/CompoundPredicate[not(*[not(self::CompoundPredicate or self::False)])]">
<xsl:apply-templates select="CompoundPredicate"/>
</xsl:template>
<!-- remove 'False' elements, children of outer 'CompoundPredicate' -->
<xsl:template match="root/CompoundPredicate/False"/>
</xsl:stylesheet>
Related
I want to display only those orders which dont have OrderLineSource = YTR. All other should be displayed....
My Sample XML :
<Orders>
<Order>
<OrderID>34209649</OrderID>
<OrderStatus>checkout_complete</OrderStatus>
<Amount>32.93</Amount>
<OrderCreation>2014-02-08T00:00:03.00</OrderCreation>
<OrderCompletion>2014-02-08T00:00:03.00</OrderCompletion>
<CustomerGUID>303965683</CustomerGUID>
<CSMPurchaserGUID>0</CSMPurchaserGUID>
<Brand>TRFE</Brand>
<SourceECommerceSystem>Framework</SourceECommerceSystem>
<Currency>GBP</Currency>
<OrderChannel>Online</OrderChannel>
<TransactionSummary>
<TransactionID>2407065</TransactionID>
<MerchantReference>TEdV-5648-34209649</MerchantReference>
<CardCategory>Personal</CardCategory>
<CardScheme>VISA Debit</CardScheme>
<CardCountry>gbr</CardCountry>
<CardIssuer>sdfsdf sdf Bank asdf</CardIssuer>
<CardStartDate>0/0</CardStartDate>
<CardExpiryDate>2016/08</CardExpiryDate>
<Amount>32.93</Amount>
<Currency>GBP</Currency>
<CardPAN>************4585</CardPAN>
<Created>2014-02-07T23:56:48</Created>
<Updated>2014-02-08T00:00:03</Updated>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
<HostedPageIdentifier>dsfasdf-ee85-4afa-bb6a-0afc6dc99896</HostedPageIdentifier>
<HostedPageURL>https://hps.datacash.com/hps/</HostedPageURL>
<PaymentStatus>Paid</PaymentStatus>
<PaymentType>Debit Card</PaymentType>
<NameOnCard>Miss L J adsf</NameOnCard>
<DataCashRef>56456456454</DataCashRef>
<MerchantID>545646</MerchantID>
<ThreeDCard>1</ThreeDCard>
<ThreeDRequested>1</ThreeDRequested>
<IPAddress>127.89.560.1</IPAddress>
</TransactionSummary>
<OrderLine>
<OrderLineID>84598837</OrderLineID>
<OrderID>34209649</OrderID>
<OrderLineLabel>GAREGSBV</OrderLineLabel>
<OrderLineSource>GHR</OrderLineSource>
<Quantity>1</Quantity>
<UnitPrice>32.93</UnitPrice>
<Total>32.93</Total>
<SKUCode>P0032</SKUCode>
<Title>Miss.</Title>
<FirstName>ertwer</FirstName>
<FamilyName>sdaf</FamilyName>
<DateOfBirth>1984-05-30</DateOfBirth>
<Email>sdfasdfa#hotmail.com</Email>
<Mobile>645646454</Mobile>
<PostChannel>0</PostChannel>
<TelephoneChannel>0</TelephoneChannel>
<EmailChannel>0</EmailChannel>
<TextAndOtherChannel>0</TextAndOtherChannel>
<BuildingNumber>27</BuildingNumber>
<AddressLine1>27</AddressLine1>
<AddressLine2>dsfasdf Road</AddressLine2>
<Town>London</Town>
<Country>sdfasdf er</Country>
<Postcode>KL7 2NS</Postcode>
<AddressValidated>1</AddressValidated>
<HKPolicy>
<PolicyNum>PP01754397</PolicyNum>
<ProductDescription>sadfsadfasdfgasdg</ProductDescription>
<CoverTypeDesc>Individual</CoverTypeDesc>
<SingleParentFamilyFlag>0</SingleParentFamilyFlag>
<PolicyTypeRefID>S</PolicyTypeRefID>
<PolicyTypeDesc>Sinasdfnce</PolicyTypeDesc>
<TierDesc>Classic</TierDesc>
<DestinationDesc>Worldwide including USA, Canada, Caribbean</DestinationDesc>
<TotalTravellers>1</TotalTravellers>
<NumOfAdults>1</NumOfAdults>
<NumOfUnder18>0</NumOfUnder18>
<PolicyStartDate>2014-02-08</PolicyStartDate>
<PolicyEndDate>2014-02-12</PolicyEndDate>
<BaseCost>32.93</BaseCost>
<Commission>11.18</Commission>
<UpsoldInd>0</UpsoldInd>
<TierRefID>C</TierRefID>
<DestinationRefID>W2</DestinationRefID>
<CoverTypeRefID>I</CoverTypeRefID>
<AONToPostPolicy>yes</AONToPostPolicy>
<SalesChannel>0011002</SalesChannel>
<WhereYouHeardOfUs>Press advertising</WhereYouHeardOfUs>
<TIPOLTraveller>
<TravellerUUID>1864-1</TravellerUUID>
<PolicyNum>PI0e31754397</PolicyNum>
<Title>Miss</Title>
<FirstName>sdfsf</FirstName>
<FamilyName>sdfsdf</FamilyName>
<DateOfBirth>1984-05-30</DateOfBirth>
<AgeBand>1864</AgeBand>
<DependentFlag>0</DependentFlag>
</TIPOLTraveller>
</TIPOLPolicy>
</OrderLine>
<OrderCustomerDetails>
<Title nil="true" />
<FirstName nil="true" />
<SecondName nil="true" />
<FamilyName nil="true" />
<DateOfBirth nil="true" />
<Email nil="true" />
<Telephone nil="true" />
<Mobile nil="true" />
<Gender nil="true" />
<PostChannel nil="true" />
<TelephoneChannel nil="true" />
<EmailChannel nil="true" />
<TextAndOtherChannel nil="true" />
<BuildingNumber>27</BuildingNumber>
<AddressLine1>27</AddressLine1>
<AddressLine2>asdfa Road</AddressLine2>
<Town>asdfasdf</Town>
<Country>United dsf</Country>
<Postcode>KH9 2NS</Postcode>
<AddressValidated>1</AddressValidated>
</OrderCustomerDetails>
</Order>
<Order>
<OrderID>34209674</OrderID>
<OrderStatus>checkout_complete</OrderStatus>
<Amount>11.13</Amount>
<OrderCreation>2014-02-08T00:08:40.00</OrderCreation>
<OrderCompletion>2014-02-08T00:08:40.00</OrderCompletion>
<CustomerGUID>303965688</CustomerGUID>
<CSMPurchaserGUID>0</CSMPurchaserGUID>
<Brand>TRFDS</Brand>
<SourceECommerceSystem>Framework</SourceECommerceSystem>
<Currency>GBP</Currency>
<OrderChannel>Online</OrderChannel>
<TransactionSummary>
<TransactionID>8115032</TransactionID>
<MerchantReference>JHF-0800-34209674</MerchantReference>
<CardCategory>Personal</CardCategory>
<CardScheme>VISA Debit</CardScheme>
<CardCountry>gbr</CardCountry>
<CardIssuer>Unknown</CardIssuer>
<CardStartDate>0/0</CardStartDate>
<CardExpiryDate>2016/09</CardExpiryDate>
<Amount>11.13</Amount>
<Currency>GBP</Currency>
<CardPAN>************4849</CardPAN>
<Created>2014-02-08T00:08:00</Created>
<Updated>2014-02-08T00:08:40</Updated>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
<HostedPageIdentifier>f3306487-d6ea-4200-9eea-99b1d6832a2e</HostedPageIdentifier>
<HostedPageURL>https://hps.dat.com/hps/</HostedPageURL>
<PaymentStatus>Paid</PaymentStatus>
<PaymentType>Debit Card</PaymentType>
<NameOnCard>Miss Jor </NameOnCard>
<DataCashRef>380010093738013</DataCashRef>
<MerchantID>21877049</MerchantID>
<ThreeDCard>1</ThreeDCard>
<ThreeDRequested>1</ThreeDRequested>
<IPAddress>86..25640.99</IPAddress>
</TransactionSummary>
<OrderLine>
<OrderLineID>84598874</OrderLineID>
<OrderID>34209674</OrderID>
<OrderLineLabel>3-1008617753325</OrderLineLabel>
<OrderLineSource>YTR</OrderLineSource>
<Quantity>1</Quantity>
<UnitPrice>11.13</UnitPrice>
<Total>11.13</Total>
<Title>Miss.</Title>
<FirstName>Jordan</FirstName>
<SecondName>oirut</SecondName>
<FamilyName>dfgsdfgs</FamilyName>
<Email>dfgsdfg#hotmail.com</Email>
<Mobile>654756464</Mobile>
<PostChannel>0</PostChannel>
<TelephoneChannel>0</TelephoneChannel>
<EmailChannel>0</EmailChannel>
<TextAndOtherChannel>0</TextAndOtherChannel>
<BuildingNumber>12</BuildingNumber>
<AddressLine1>12</AddressLine1>
<AddressLine2>sfgsdfg End Gardens</AddressLine2>
<Town>HEMEL sfgaefa</Town>
<Country>adf dgfsdfg</Country>
<Postcode>HP1 1SN</Postcode>
<OrderLineDetail>
<NameValuePair>
<Name>dfgsdfg</Name>
<Value>628</Value>
</NameValuePair>
<NameValuePair>
<Name>NameOnCard</Name>
<Value>adsfgasdgf Piper</Value>
</NameValuePair>
<NameValuePair>
<Name>DateOnCard</Name>
<Value>2014-02-05</Value>
</NameValuePair>
<NameValuePair>
<Name>CustomsOrSurcharge</Name>
<Value>CUSTOMS CHARGE TO PAY</Value>
</NameValuePair>
</OrderLineDetail>
</OrderLine>
<OrderCustomerDetails>
<Title>Miss.</Title>
<FirstName>Jordan</FirstName>
<SecondName>asdgfasdgf</SecondName>
<FamilyName nil="true" />
<DateOfBirth />
<Email>adfadf#hotmail.com</Email>
<Telephone />
<Mobile>adfasdf</Mobile>
<Gender nil="true" />
<PostChannel nil="true" />
<TelephoneChannel nil="true" />
<EmailChannel nil="true" />
<TextAndOtherChannel nil="true" />
<BuildingNumber>12</BuildingNumber>
<AddressLine1>12</AddressLine1>
<AddressLine2>adfasdf End Gardens</AddressLine2>
<Town>adsfasdf HEMPSTEAD</Town>
<Country>United asdfasdf</Country>
<Postcode>asd 1SN</Postcode>
</OrderCustomerDetails>
</Order>
</Orders>
I tried using XSLT :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Orders -->
<xsl:template match="/*">
<xsl:element name="Orders">
<xsl:apply-templates select="./Order" />
</xsl:element>
</xsl:template>
<!-- Orders > Order -->
<xsl:template match="/Order">
<xsl:variable name="IsValid">
<xsl:call-template name="HasOrIsValidPOLine" />
</xsl:variable>
<xsl:if test="$IsValid='VALID'"> <!-- only display the order if there's a valid line under it-->
<xsl:element name="Order">
<xsl:apply-templates select=".//VORNR" />
</xsl:element>
</xsl:if>
</xsl:template>
<!-- Part Order List > Part Order > Operational BO Number -->
<xsl:template match="//VORNR">
<xsl:element name="./Order">
<xsl:apply-templates select="node()|#*"/>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>
<xsl:template name="HasOrIsValidPOLine">
<xsl:choose>
<xsl:when test="./OrderLineSource/text() != 'YTR'">VALID</xsl:when>
<xsl:otherwise>INVALID</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can you provide me the solution or let me know where I am going wrong
First, your sample XML is not well-formed: It contains a closing </TIPOLPolicy> tag that doesn't match the starting <HKPolicy> tag. Change that to </HKPolicy> first.
After that, the following XSLT 1.0 does what you want:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity transform -->
<!-- Default priority 0 for root node and -0.5 for the rest -->
<xsl:template match="/ | node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<!-- Do nothing for Order elements whose OrderLine/OrderLineSource equals 'YTR' -->
<!-- Default priority 0.5 -->
<xsl:template match="Order[OrderLine/OrderLineSource = 'YTR']"/>
</xsl:stylesheet>
It makes use of the identity transform and different default priorities: The identity transform with a lower default priority copies the input to the output unless another template with a higher priority exists for a given input match. This is the case for Order elements whose OrderLine/OrderLineSource descendant contains the text value 'YTR'. Due to its higher default priority, the more specific template takes precedence over the identity transform. Since the template doesn't produce any output, any matching Order elements are removed from the output.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Order[OrderLine/OrderLineSource[string() = 'YTR']]"/>
</xsl:stylesheet>
I am trying to convert following xml into other xml but I am not getting the values for xCoordinate and yCoordinate. I would like to convert the structure from source - XML to Target-XML where the goocodes will match and the result would be assigned to x and y.
Source - XML
<?xml version="1.0"?>
<AddressResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" errorCode="0" errorDescription="">
<wrappedResultList xmlns="http://xlocate.xserver.ptvag.com">
<ResultAddress city="Amsterdam" city2="" country="NL" houseNumber="" postCode="1***" state="Noord-Holland" street="" adminRegion="Amsterdam" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="100">
<wrappedAdditionalFields />
<coordinates>
<kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
<point x="4.89327999999999" y="52.373090000000005" xmlns="http://common.xserver.ptvag.com" />
</coordinates>
</ResultAddress>
<ResultAddress city="Amsterdam-Zuidoost" city2="" country="NL" houseNumber="" postCode="110*" state="Noord-Holland" street="" adminRegion="Amsterdam" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="80">
<wrappedAdditionalFields />
<coordinates>
<kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
<point x="4.9513699999999838" y="52.316199999999988" xmlns="http://common.xserver.ptvag.com" />
</coordinates>
</ResultAddress>
<ResultAddress city="Nieuw-Amsterdam" city2="" country="NL" houseNumber="" postCode="7833" state="Drenthe" street="" adminRegion="Emmen" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="80">
<wrappedAdditionalFields />
<coordinates>
<kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
<point x="6.8528699999999994" y="52.716139999999982" xmlns="http://common.xserver.ptvag.com" />
</coordinates>
</ResultAddress>
</wrappedResultList>
</AddressResponse>
Target - XML
<GeoCodeResponse>
<geocordinate>
<xCordinate>4.89327999999999</xCordinate>
<yCordinate>52.716139999999982</yCordinate>
</geocordinate>
</GeoCodeResponse>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xl="http://xlocate.xserver.ptvag.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://common.xserver.ptvag.com" exclude-result-prefixes="xl xsi xsd cm" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<GeoCodeResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<geocordinate xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<xsl:for-each select="AddressResponse/xl:wrappedResultList/xl:ResultAddress">
<xsl:sort select="#xl:totalScore" order="descending" data-type="number"/>
<xsl:if test="position()= 1">
<xCordinate> <xsl:value-of select="/xl:coordinates/cm:point/cm:x" /></xCordinate>
<yCordinate> <xsl:value-of select="/xl:coordinates/cm:point/cm:y" /></yCordinate>
</xsl:if>
</xsl:for-each>
</geocordinate>
</GeoCodeResponse>
</xsl:template>
</xsl:stylesheet>
Please help what could be done in above xslt.
You were almost there. The coordinates are attributes, not nodes.
change it into this:
<xsl:if test="position()= 1">
<xCordinate>
<xsl:value-of select="xl:coordinates/cm:point/#x" />
</xCordinate>
<yCordinate>
<xsl:value-of select="xl:coordinates/cm:point/#y" />
</yCordinate>
</xsl:if>
You are also referencing the coordinates node from the root, but it should be relative. I changed /xl:coordinaties into xl:coordinates
I'm stuck with what should be a simple problem with XSLT keys.
If it is relevant, I'm forced into an XSLT 1.0 parser.
Sample XML:
<updates>
<update>
<id>first</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.0'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.0'/>
</collection>
</pkglist>
</update>
<update>
<id>second</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.1'/>
<package name='conf' version='1.1'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.2'/>
<package name='conf' version='1.1'/>
</collection>
</pkglist>
</update>
<update>
<id>third</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.3'/>
<package name='conf' version='1.1'/>
<package name='src' version='1.3'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.3'/>
<package name='conf' version='1.2'/>
<package name='src' version='1.3'/>
</collection>
</pkglist>
</update>
</updates>
This XPATH selects what I'm looking for
/updates//update[pkglist/collection/package/#name = 'bin']/id/text()
I'm looking for all 'id' values in the whole document that have packages with an attribute of 'name'. But in the real world I have way more packages than would be sensible to list out by hand.
So I figured a key would be the way to go
<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/#name" use="../../../../id" />
But that doesn't give me back anything useful
<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/#name" use="../../../../id" />
<xsl:template match="/">
<xsl:apply-templates select="updates/update" />
</xsl:template>
<xsl:template match="update">
Updates:
<xsl:value-of select="id" />
Related updates
<xsl:apply-templates select="pkglist" />
</xsl:template>
<xsl:template match="pkglist">
<xsl:for-each select="key('idByPackage', collection/package/#name)">
<xsl:value-of select="." />
<xsl:value-of select="collection/package/#name" />
</xsl:for-each>
</xsl:template>
I know I'm in the right area as when I change the key to this:
<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/#name" use="../../../../pkglist/collection/package/#name" />
The same xsl template spits pack my package names.
When I run the template with the 'looks valid to me but does not work' key I get this:
Updates:
first
Related updates
Updates:
second
Related updates
Updates: third
Related updates
When I expect to get something like
Updates:
first
Related updates
second
third
Updates:
second
Related updates
first
third
Updates: third
Related updates
first
second
What am I doing wrong?
This is the key you need:
<xsl:key name="idByPackage"
match="update/id" use="../pkglist/collection/package/#name" />
When this XSLT is run on your sample input:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="idByPackage"
match="update/id" use="../pkglist/collection/package/#name" />
<xsl:variable name="nl" select="'
'" />
<xsl:template match="/">
<xsl:apply-templates select="updates/update" />
</xsl:template>
<xsl:template match="update">
<xsl:value-of select="concat('Updates:', $nl,
id, $nl,
'Related updates:', $nl)" />
<xsl:variable name="name" select="pkglist/collection/package/#name" />
<xsl:apply-templates select="key('idByPackage', $name)[. != current()/id]" />
</xsl:template>
<xsl:template match="id">
<xsl:value-of select="concat(., $nl)" />
</xsl:template>
</xsl:stylesheet>
The result is:
Updates:
first
Related updates:
second
third
Updates:
second
Related updates:
first
third
Updates:
third
Related updates:
first
second
I'm trying to use the XSLT key() function to return all the <Code> elements in an XML file that match the following two criteria:
Code[code=$code] AND ancestor::CodeType[type=$codeType]`
Here is a simplified example of what the input XML looks like:
<Items>
<Item code-number="C1" category="ABC" />
<Item code-number="C3" category="ABC" />
<Item code-number="C1" category="XYZ" />
</Items>
<CodeTypes>
<CodeType type="ABC">
<SubType title="Category III Codes"> <!-- <SubType> elements are optional -->
<SubType title="Subcategory III-15 Codes">
<Code code="C1" description="Red" />
<Code code="C2" description="Green" />
<Code code="C3" description="Blue" />
<Code code="C3" description="Purple" /> <!-- Same code can appear more than once -->
</SubType>
</SubType>
<CodeType>
<CodeType type="XYZ">
<Code code="C1" description="Black" /> <!-- Same code can be used for multiple CodeTypes -->
<Code code="C2" description="Orange" />
<Code code="C3" description="Yellow" />
<CodeType>
</CodeTypes>
Note that the comments aren't actually there in the actual XML, I'm just adding them here to clarify the XML structure.
Here is the XSLT transform I am trying to use, though it doesn't seem to be working:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="LookupMatchingCodeElements" match="Code" use="concat(../#code, '+', ancestor::CodeType/#type)" />
<xsl:template match="Item">
<xsl:call-template name="GetCodeElements">
<xsl:with-param name="code" select="#code-number" />
<xsl:with-param name="codeType" select="#category" />
</xsl:call-template>
</xsl:template>
<xsl:template name="GetCodeElements">
<xsl:param name="code" />
<xsl:param name="codeType" />
<xsl:for-each select="key('LookupMatchingCodeElements', concat($code, '+', $codeType))">
<!-- process each <Code> element -->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And this is what I want the key() function to return with different inputs:
<!-- For code="C1" AND codeType="ABC" -->
<Code code="C1" description="Red" />
<!-- For code="C3" AND codeType="ABC" -->
<Code code="C3" description="Blue" />
<Code code="C3" description="Purple" />
<!-- For code="C1" AND codeType="XYZ" -->
<Code code="C1" description="Black" />
Is this possible with the key() function? As there are hundreds of thousands of both <Item> and <Code> elements, being able to use <xsl:key> is very important.
Use:
<xsl:key name="kCode" match="Code"
use="concat(ancestor::CodeType[1]/#type, '+', #code)"/>
Here is a complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kCode" match="Code"
use="concat(ancestor::CodeType[1]/#type, '+', #code)"/>
<xsl:template match="/">
<xsl:copy-of select="key('kCode', 'ABC+C1')"/>
====================
<xsl:copy-of select="key('kCode', 'ABC+C3')"/>
====================
<xsl:copy-of select="key('kCode', 'XYZ+C1')"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following document (the provided XML text -- corrected malformedness):
<t>
<Items>
<Item code-number="C1" category="ABC" />
<Item code-number="C3" category="ABC" />
<Item code-number="C1" category="XYZ" />
</Items>
<CodeTypes>
<CodeType type="ABC">
<SubType title="Category III Codes">
<!-- <SubType> elements are optional -->
<SubType title="Subcategory III-15 Codes">
<Code code="C1" description="Red" />
<Code code="C2" description="Green" />
<Code code="C3" description="Blue" />
<Code code="C3" description="Purple" />
<!-- Same code can appear more than once -->
</SubType>
</SubType>
</CodeType>
<CodeType type="XYZ">
<Code code="C1" description="Black" />
<!-- Same code can be used for multiple CodeTypes -->
<Code code="C2" description="Orange" />
<Code code="C3" description="Yellow" />
</CodeType>
</CodeTypes>
</t>
the wanted, correct result is produced:
<Code code="C1" description="Red"/>
====================
<Code code="C3" description="Blue"/>
<Code code="C3" description="Purple"/>
====================
<Code code="C1" description="Black"/>
My xml looks like this:
<topics>
<topic name="topic1" />
<topic name="topic2" />
<topic name="topic3" />
<topic name="topic4" />
<topic name="topic5" />
<topic name="topic6" />
<topic name="topic7" />
</topics>
<supertopics>
<supertopic title="supertopic1" name="1;topic1;#2;#topic2;3;#topic3" />
<supertopic title="supertopic2" name="4;#topic4;#7;#topic7" />
<supertopic title="supertopic3" name="2;#topic2;#3;#topic3" />
<supertopic title="supertopic4" name="5;#topic5;#7;#topic7" />
<supertopic title="supertopic5" name="3;#topic3;#7;#topic7" />
<supertopic title="supertopic6" name="4;#topic4;#7;#topic7" />
<supertopic title="supertopic7" name="5;#topic5;#7;#topic7" />
<supertopic title="supertopic8" name="2;#topic2;#6;#topic6" />
<supertopic title="supertopic9" name="5;#topic6;#7;#topic7" />
<supertopic title="supertopic10" name="3;#topic3;#4;#topic4" />
</supertopics>
I basically want 1 supertopic per topic.So that means I have 7 topics and I want 7 most recent supertopics associated with it. I have date also with it through I m doing sorting but main thing is I want these 7 supertopics to be unique as there are multiple supertopics per topic.
So I want my output to be like this:
supertopic1 (topic1 is associated to supertopic1)
supertopic3 (topic2 is associated to supertopic1 but as its already there i want it to look for next supertopic its associated to)
supertopic5 (topic3)
supertopic2 (topic4)
supertopic4 (topic5)
supertopic8 (topic6)
supertopic6 (topic7)
I am using xsl 1.0
and I was trying to achieve it using but i couldn't find any way to do this:
<xsl:param name="FilteredAssets1">
<stopic></stopic>
<ttopic></ttopic>
</xsl:param>
<xsl:for-each select="topics/topic/#name">
<xsl:variable name="topicname">
<xsl:value-of select="."></xsl:value-of>
</xsl:variable>
<xsl:for-each select="/supertopics/supertopic[contains(#name,$topicname)]">
<xsl:if test="not(contains(msxsl:node-set($FilteredAssets1)/stopic,#title)) and not(contains(msxsl:node-set($FilteredAssets1)/ttopic,$programname))">
<stopic><xsl:value-of select="#title"></xsl:value-of></stopic>
<ttopic><xsl:value-of select="$programname"></xsl:value-of></ttopic>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vSupers"
select="/*/supertopics/supertopic"/>
<xsl:template match="/*">
<xsl:apply-templates select="topics/topic[1]"/>
</xsl:template>
<xsl:template match="topic">
<xsl:param name="pUsedSupers" select="/.."/>
<xsl:variable name="vNewSuper" select=
"$vSupers
[not(#title = $pUsedSupers/#title)
and
contains(concat(#name, ';'),
concat('#', current()/#name, ';')
)
]
[1]
"/>
<xsl:value-of select="concat('
', $vNewSuper/#title)"/>
<xsl:apply-templates select="following-sibling::topic[1]">
<xsl:with-param name="pUsedSupers"
select="$pUsedSupers | $vNewSuper"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML text (wrapped in with a single top element to be made a well-formed XML document and also with acorrection to what I think is a typo for supertopic1):
<t>
<topics>
<topic name="topic1" />
<topic name="topic2" />
<topic name="topic3" />
<topic name="topic4" />
<topic name="topic5" />
<topic name="topic6" />
<topic name="topic7" />
</topics>
<supertopics>
<supertopic title="supertopic1" name="1;#topic1;#2;#topic2;3;#topic3" />
<supertopic title="supertopic2" name="4;#topic4;#7;#topic7" />
<supertopic title="supertopic3" name="2;#topic2;#3;#topic3" />
<supertopic title="supertopic4" name="5;#topic5;#7;#topic7" />
<supertopic title="supertopic5" name="3;#topic3;#7;#topic7" />
<supertopic title="supertopic6" name="4;#topic4;#7;#topic7" />
<supertopic title="supertopic7" name="5;#topic5;#7;#topic7" />
<supertopic title="supertopic8" name="2;#topic2;#6;#topic6" />
<supertopic title="supertopic9" name="5;#topic6;#7;#topic7" />
<supertopic title="supertopic10" name="3;#topic3;#4;#topic4" />
</supertopics>
</t>
produces the wanted, correct result:
supertopic1
supertopic3
supertopic5
supertopic2
supertopic4
supertopic8
supertopic6