I have an XML looking like this:
<pack>
<titlesPacks>
<StoryPack id="1111111">
<value>A</value>
</StoryPack>
<StoryPack id="2222222">
<value>F</value>
</StoryPack>
</titlesPacks>
<referenceTable>
<TitleReference id="1111111" />
<TitleReference id="2222222" />
</referenceTable>
</pack>
I need to copy the xml file, but:
delete StoryPack nodes where value node has specific value (A, B, C for example). This part is already OK
delete 'TitleReferencenodes whereidattribute value is equal toid attribute value of' StoryPack nodes deleted above
I don't know how to do the second one: I tried with Key, but doesn't work:
My current XSL:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:data="http://example.com/data" exclude-result-prefixes="data">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="titleId" match="StoryPack" use="#id"/>
<!-- Values for which nodes must be deleted -->
<data:data xmlns="">
<value>A</value>
<value>B</value>
<value>C</value>
</data:data>
<xsl:variable name="values" select="document('')/xsl:stylesheet/data:data/value"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Delete nodes with specific values -->
<xsl:template match="StoryPack[value = $values]"/>
<!-- Dlete nodes with id from specific values -->
<xsl:template match="TitleReference[StoryPack[key('titleId', #id)/value = $values]]" />
</xsl:stylesheet>
Thank you for your help!
=============
Following #michael.hor257k answers : my source XML is a little more complex, that's probably why it doesn't work in my case:
The Xpath of value is deeper:
/Pack/titlesPacks/StoryPack/assets/TitleAssets/assets/StringAssetInfo/value
So I've done:
<xsl:template match="TitleReference[key('titleId', #id)/assets/TitleAssets/assets/StringAssetInfo[#attrId = '127']/value = $values]" />
but it doesn't work.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Pack>
<titlesPacks>
<StoryPack id="1111111">
<assets>
<TitleAssets>
<assets>
<StringAssetInfo attrId="127">
<value>A</value>
</StringAssetInfo>
</assets>
</TitleAssets>
</assets>
</StoryPack>
</titlesPacks>
<referenceTable>
<ReferenceTable>
<titlesReferences>
<TitleReference id = "1111111"/>
</titlesReferences>
</ReferenceTable>
</referenceTable>
</pack>
my need is to
delete StoryPack where attribute attrId of StringAssetInfo = 127 and value = A, or B... => Already OK
delete TitleReference where id attribute value = id attribute value of deleted nodes above
Try it this way?
XSLT 2.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="*"/>
<xsl:key name="titleId" match="StoryPack" use="#id"/>
<!-- Values for which nodes must be deleted -->
<xsl:variable name="values">
<value>A</value>
<value>B</value>
<value>C</value>
</xsl:variable>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Delete nodes with specific values -->
<xsl:template match="StoryPack[value = $values/value]"/>
<!-- Delete nodes with id from specific values -->
<xsl:template match="TitleReference[key('titleId', #id)/value = $values/value]" />
</xsl:stylesheet>
I have simplified the variable definition, so it can work in this online demo:
https://xsltfiddle.liberty-development.net/jz1PuP9
- and also because it's simpler.
Added:
In view of your updated XML structure, change:
<xsl:template match="StoryPack[value = $values/value]"/>
to:
<xsl:template match="StoryPack[.//value = $values/value]"/>
or (preferably) to:
<xsl:template match="StoryPack[assets/TitleAssets/assets/StringAssetInfo/value = $values/value]"/>
and change:
<xsl:template match="TitleReference[key('titleId', #id)/value = $values/value]" />
to:
<xsl:template match="TitleReference[key('titleId', #id)//value = $values/value]" />
or (preferably) to:
<xsl:template match="TitleReference[key('titleId', #id)/assets/TitleAssets/assets/StringAssetInfo/value = $values/value]" />
Related
I am trying to do something that is probably very simple, but my very rudimentary xslt is not up to it.
Given the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<MyLists>
<List1>
<Place01 ctr="PTG">Lisbon</Place01>
<Place02 ctr="SPA">Madrid</Place02>
<Place03 ctr="FRA">Paris</Place03>
<Place04 ctr="ENG">York</Place04>
</List1>
<List2>
<Item01 type="country">Italy</Item01>
<Item02 type="person">John</Item02>
<Item03 type="city">York</Item03>
<Item04 type="city" subtype="capital">Madrid</Item04>
</List2>
</MyLists>
I would like to compare the text nodes from <List1> and <List2>, and, whenever their values are the same, pass, for each element, the attributes from <List2> to the corresponding items in <List1>, in order to get:
<?xml version="1.0" encoding="UTF-8"?>
<MyLists>
<List1>
<Place01 ctr="PTG">Lisbon</Place01>
<Place02 ctr="SPA" type="city" subtype="capital">Madrid</Place02>
<Place03 ctr="FRA">Paris</Place03>
<Place04 ctr="ENG" type="city">York</Place04>
</List1>
<List2>
<Item01 type="country">Italy</Item01>
<Item02 type="person">John</Item02>
<Item03 type="city">York</Item03>
<Item04 type="city" subtype="capital">Madrid</Item04>
</List2>
</MyLists>
Ideally, I'd like to be able to copy whichever attributes these element possess, without having to specify them.
Many thanks in advance!
If I understand this correctly, you could do something like:
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="*"/>
<xsl:key name="match" match="Item" use="." />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Place">
<xsl:copy>
<xsl:copy-of select="key('match', .)/#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have an incoming XML like below: I need to remove the <shoeboxImage> tag from the incoming below XML.
Incoming XML Input:
<attachReceipt>
<baseMessage>
<returnCode>200</returnCode>
</baseMessage>
<payload>
<returnCode>0</returnCode>
<shoeboxItem>
<shoeboxImageCount>2</shoeboxImageCount>
<shoeboxImages>
<shoeboxImage>
<name>receiptImage.jpg</name>
</shoeboxImage>
<shoeboxImage>
<name>receiptImage.jpg</name>
</shoeboxImage>
</shoeboxImages>
</shoeboxItem>
</payload>
</attachReceipt>
Expected Output:
<attachReceipt>
<baseMessage>
<returnCode>200</returnCode>
</baseMessage>
<payload>
<returnCode>0</returnCode>
<shoeboxItem>
<shoeboxImageCount>2</shoeboxImageCount>
<shoeboxImages>
<name>receiptImage.jpg</name>
<name>receiptImage.jpg</name>
</shoeboxImages>
</shoeboxItem>
</payload>
</attachReceipt>
Need some xslt code snippet to do this.
I don't have the necessary software installed to actually test this, but this should work:
<xsl:template match="shoeboxImage">
<xsl:apply-templates select="*|text()"/>
</xsl:template>
The idea is that when a shoeboxImage element is encountered, it generates nothing for the element itself, and just continues with its children.
You need to have an identity template and a template that will remove the element shoeboxImage but will retain its descendants.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<!-- identity template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- template override for the element shoeboxImage -->
<xsl:template match="shoeboxImage">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
I need your assistance with the logic to add the end tags. The structure i am looking at is . I tried with for-each or xsl:if or xsl:choose. The input XML is as below
<SuperShipNotice>
<Package packageType="P" packageLevel="1">
<PackageNumber>PWN34332</PackageNumber>
<ShipmentNumber>105909390</ShipmentNumber>
<ShipmentLineNumber>1</ShipmentLineNumber>
<PartNumber>1CH162-510</PartNumber>
<Quantity>1000</Quantity>
<SSCCNumber>00176364909402100165</SSCCNumber>
</Package>
<Package packageType="C" packageLevel="2">
<PackageNumber>CWX612432660</PackageNumber>
<ParentPackageNumber>PWN34332</ParentPackageNumber>
<ShipmentNumber>105909390</ShipmentNumber>
<ShipmentLineNumber>1</ShipmentLineNumber>
<PartNumber>1CH162-510</PartNumber>
<Quantity>25</Quantity>
<SSCCNumber>00176364909402100165</SSCCNumber>
</Package>
<Package packageType="S" packageLevel="3">
<PackageNumber>W1D2WNGL</PackageNumber>
<ParentPackageNumber>CWX612432660</ParentPackageNumber>
<ShipmentNumber>105909390</ShipmentNumber>
<ShipmentLineNumber>1</ShipmentLineNumber>
<PartNumber>1CH162-510</PartNumber>
<Quantity>1</Quantity>
<DateOfMfg>20131209</DateOfMfg>
<COO>CN</COO>
<SSCCNumber>00176364909402100165</SSCCNumber>
</Package>
<Package packageType="S" packageLevel="3">
<PackageNumber>W1D2WNGL</PackageNumber>
<ParentPackageNumber>CWX612432660</ParentPackageNumber>
<ShipmentNumber>105909390</ShipmentNumber>
<ShipmentLineNumber>1</ShipmentLineNumber>
<PartNumber>1CH162-510</PartNumber>
<Quantity>1</Quantity>
<DateOfMfg>20131209</DateOfMfg>
<COO>CN</COO>
<SSCCNumber>00176364909402100165</SSCCNumber>
</Package>
</SuperShipNotice>
Not sure if this can be of any use for you - following XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="SuperShipNotice">
<xsl:apply-templates select="//PackageNumber[parent::Package[#packageLevel='1']]" />
</xsl:template>
<xsl:template match="PackageNumber[parent::Package[#packageLevel='1']]">
<xsl:variable name="packageNumber" select="." />
<PkgLevel1>
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
<xsl:apply-templates select="//PackageNumber[parent::Package[#packageLevel='2'] and parent::Package/ParentPackageNumber = $packageNumber]" />
</PkgLevel1>
</xsl:template>
<xsl:template match="PackageNumber[parent::Package[#packageLevel='2']]">
<xsl:variable name="packageNumber" select="." />
<PkgLevel2>
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
<xsl:apply-templates select="//PackageNumber[parent::Package[#packageLevel='3'] and parent::Package/ParentPackageNumber = $packageNumber
and not(parent::Package/ParentPackageNumber = preceding::Package[#packageLevel='3']/ParentPackageNumber)]" />
</PkgLevel2>
</xsl:template>
<xsl:template match="PackageNumber[parent::Package[#packageLevel='3']]">
<PkgLevel3>
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</PkgLevel3>
</xsl:template>
</xsl:transform>
when applied to your input XML produces the output
<PkgLevel1>
<PackageNumber>PWN34332</PackageNumber>
<PkgLevel2>
<PackageNumber>CWX612432660</PackageNumber>
<PkgLevel3>
<PackageNumber>W1D2WNGL</PackageNumber>
</PkgLevel3>
</PkgLevel2>
</PkgLevel1>
The first template matching SuperShipNotice applies templates to PackageNumbers of Packages with the packageLevel value 1.
In the template matching those Packagenumbers templates are applied to all PackageNumbers with the packageLevel value 2 and the ParentPackageNumber of the current PackageNumber.
As there is a double entry for Packages with the packageLevel value 3, the second Package with the same ParentPackageNumber is omitted:
select="//PackageNumber[parent::Package[#packageLevel='3']
and parent::Package/ParentPackageNumber = $packageNumber
and not(parent::Package/ParentPackageNumber = preceding::Package[#packageLevel='3']/ParentPackageNumber)]"
In case you can adjust this to fit further requirements, you can use the saved Demo
I need to make certain modifications to my XML input, depending on certain conditions. I am using XSLT 1.0.
the value of the message_type element (child element of m_cotrol) should be changed
A new element message_status should be added (as a child of the m_control element).
These changes are reflected in the expected output XML. With my current XSLT code, I am only able to achieve the second requirement.
Input XML:
<?xml version="1.0"?>
<message xmlns="http://www.origoservices.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<m_control>
<control_timestamp>2013-04-12T09:24:38.902</control_timestamp>
<message_id>a50ec030-72ab</message_id>
<retry_number>0</retry_number>
<message_type>Request</message_type>
<message_version>test.XSD</message_version>
<expected_response_type>synchronous</expected_response_type>
<initiator_id>FST</initiator_id>
<initiator_orchestration_id>1637280</initiator_orchestration_id>
<responder_id>mycomp</responder_id>
</m_control>
<m_content>
<b_control>
<service_provider_reference_number>650971</service_provider_reference_number>
<intermediary_case_reference_number>Sample1</intermediary_case_reference_number>
<quote_type>Comparison</quote_type>
<quote_or_print>Print</quote_or_print>
<message_version_number>3.7</message_version_number>
<submission_date>0001-04-12</submission_date>
</b_control>
</m_content>
</message>
Expected Output:
<message xmlns="http://www.origoservices.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<m_control>
<control_timestamp>2013-04-12T09:24:38.902</control_timestamp>
<message_id>a50ec030-72ab</message_id>
<retry_number>0</retry_number>
<message_type>Response</message_type>
<message_version>test.XSD</message_version>
<expected_response_type>synchronous</expected_response_type>
<initiator_id>FST</initiator_id>
<initiator_orchestration_id>1637280</initiator_orchestration_id>
<responder_id>mycomp</responder_id>
<message_status>User not allowed access</message_status>
</m_control>
<m_content>
<b_control>
<service_provider_reference_number>650971</service_provider_reference_number>
<intermediary_case_reference_number>Sample1</intermediary_case_reference_number>
<quote_type>Comparison</quote_type>
<quote_or_print>Print</quote_or_print>
<message_version_number>3.7</message_version_number>
<submission_date>0001-04-12</submission_date>
<quote_response_status>Error</quote_response_status>
<quote_error_note>
<reason>[Error] Check if the User has access to the requested service</reason>
</quote_error_note>
</b_control>
</m_content>
</message>
XSLT code: Based on the value of DataPower variable (var://service/error-message), I need the expected output.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" version="1.0" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[contains(name(),'m_control')]">
<xsl:choose>
<xsl:when test="dp:variable('var://service/error-message') = 'not present'">
<m_control xmlns="http://www.origoservices.com">
<xsl:apply-templates select="#* | *"/>
<message_status>User not recognized</message_status>
</m_control>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="//*[contains(name(),'b_control')]">
<xsl:choose>
<xsl:when test="dp:variable('var://service/error-subcode')='0x01d30002'">
<b_control xmlns="http://www.origoservices.com">
<xsl:apply-templates select="#* | *"/>
<quote_response_status>Error</quote_response_status>
<quote_error_note>
<reason>[Error] Check if the User has access to the requested service</reason>
</quote_error_note>
</b_control>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The following stylesheet meets both of your requirements. It does a common identity transform (which your XSLT does, too) with exceptions.
Note that I have not taken into consideration any changes that are performed by your stylesheet but not listed as a requirement (i.e. changing quote_error_note and quote_response_status).
This line:
<xsl:template match="text()[parent::ori:message_type]">
meets your first requirement, the one you were unable to code. It matches the text content of message_type and outputs "Response" instead.
But this solution differs from yours in another way: it does not match elements along the lines of:
<xsl:template match="//*[contains(name(),'m_control')]">
Rather, their correct namespace is identified:
<xsl:template match="ori:m_control">
Now, what's the difference? Your way of describing the template match allows elements of any namespace to be matched. This might not be a problem in your case (no conflicting namespaces) but it could be one in general.
Full stylesheet
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ori="http://www.origoservices.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="ori xsi">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ori:m_control">
<xsl:copy>
<xsl:apply-templates/>
<message_status>
<xsl:text>User not allowed access</xsl:text>
</message_status>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[parent::ori:message_type]">
<xsl:text>Response</xsl:text>
</xsl:template>
</xsl:stylesheet>
I've searched through similar questions, but couldn't make any of the suggestions to work. I have the following xml I need to modify it
<XDB>
<ROOT>
<KEY><ID>12345</ID><DATE>5/10/2011</DATE></KEY>
<PERSONAL><ID>1</ID><INFO><LASTNAME>Smith</LASTNAME>...</INFO></PERSONAL>
<CONTACT><ID>1</ID><EMAIL>asmith#yahoo.com</EMAIL>...</CONTACT>
</ROOT>
<ROOT>
<KEY><ID>98765</ID><DATE>5/10/2013</DATE></KEY>
<CONTACT><ID>2</ID><EMAIL>psmithton#yahoo.com</EMAIL>...</CONTACT>
</ROOT>
...
</XDB>
And it needs to look like this:
<XDB>
<ROOT>
<KEY><ID>12345</ID><DATE>5/10/2011</DATE>
<PERSONAL><ID>1</ID><INFO><LASTNAME>Smith</LASTNAME>...</INFO></PERSONAL>
<CONTACT><ID>1</ID><EMAIL>asmith#yahoo.com</EMAIL>...</CONTACT>
</KEY>
</ROOT>
<ROOT>
<KEY><ID>98765</ID><DATE>5/10/2013</DATE>
<CONTACT><ID>2</ID><EMAIL>psmithton#yahoo.com</EMAIL>...</CONTACT>
</KEY>
</ROOT>
...
</XDB>
I need to make 2...n siblings as children of the first 'key' sibling. Essentially, i need to remove the closing < /KEY> and put it before the closing < /ROOT>. I would appreciate your help.
Thanks.
Following xslt based on Identity transform could make this job
<?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"/>
<!-- Copy everything you find... -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<!-- ... but if you find first element inside ROOT ... -->
<xsl:template match="ROOT/node()[1]">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
<!-- ... copy its sibling into it ... -->
<xsl:copy-of select="following-sibling::*" />
</xsl:copy>
</xsl:template>
<!-- ignore other elements inside ROOT element since they are copied in template matching first element -->
<xsl:template match="ROOT/node()[position() > 1]" />
</xsl:stylesheet>