I am trying to output this entire XML but with the EVENT elements sorted by ID. Being new to XSLT I thought I would still give it a try but after many attempts and reading other examples and how to guides I still can't get what I thought was a simple thing to work.
<?xml version="1.0" encoding="UTF-8"?>
<PublishWCWORKORDEROUT xmlns="http://www.xcessteel.com/maxo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2021-05-10T08:23:18+00:00" transLanguage="EN" baseLanguage="EN" messageID="3116171.1620634998889850919" maxoVersion="7 6 20190514-1348 V7611-365" event="1">
<WCWORKORDEROUTSet>
<WORKORDER action="Replace">
<ACTCATEGORY />
<X_3857>1.1838832494481975E7</X_3857>
<Y_3857>-2766476.1752903816</Y_3857>
<SPEC>
<ALNVALUE />
<REFID xsi:nil="true" />
<ASSETATTRID>ACCOUNT_NO</ASSETATTRID>
<CHANGEBY>ADMIN</CHANGEBY>
</SPEC>
<SPEC>
<ALNVALUE />
<REFID xsi:nil="true" />
</SPEC>
<SPEC>
<ALNVALUE />
<REFID xsi:nil="true" />
<ASSETATTRID>METER_LOCATION</ASSETATTRID>
</SPEC>
<EVENT>
<ID>CCC333</ID>
<WORKTYPE>UNPLANNED</WORKTYPE>
</EVENT>
<EVENT>
<ID>AAA111</ID>
<WORKTYPE>PLANNED</WORKTYPE>
</EVENT>
<EVENT>
<ID>BBB222</ID>
<WORKTYPE>SCHEDULED</WORKTYPE>
</EVENT>
<ASSIGNMENT>
<AMCREW />
<WPLABORID>209336</WPLABORID>
</ASSIGNMENT>
<WCWODETAILS>
<REFID xsi:nil="true" />
<CUSTOMERNAME />
<WCREGION>SWR</WCREGION>
<ID>96057400</ID>
</WCWODETAILS>
</WORKORDER>
</WCWORKORDEROUTSet>
</PublishWCWORKORDEROUT>
I have tried with this XSLT but clearly it is not correct.
<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 -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/PublishWCWORKORDEROUT">
<xsl:copy>
<xsl:apply-templates select="EVENT">
<xsl:sort select="ID"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You have to declare the namespace xmlns="http://www.xcessteel.com/maxo" and then use that prefix in your match and the sorting could be done like this:
EDIT on 2021-05-24 on 10:19: attribute action="Replace" was missing
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:maxo="http://www.xcessteel.com/maxo"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="maxo:WORKORDER">
<xsl:copy>
<!-- Following line was missing -->
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="maxo:EVENT[1]/preceding-sibling::*"/>
<xsl:apply-templates select="maxo:EVENT">
<xsl:sort select="maxo:ID"/>
</xsl:apply-templates>
<xsl:apply-templates select="maxo:EVENT[ position()=last()]/following-sibling::*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And as an alternative, the following would work as well:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:maxo="http://www.xcessteel.com/maxo"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="maxo:EVENT[not(preceding-sibling:: maxo:EVENT)]">
<xsl:for-each select=".|following-sibling:: maxo:EVENT">
<xsl:sort select="maxo:ID"/>
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
<xsl:template match="maxo:EVENT[ preceding-sibling:: maxo:EVENT]"/>
</xsl:stylesheet>
Related
I need to sort some content but only when an attribute is equal to CAT. I think I should be able to pass a property from my ant build file to the use-when attribute but it is not working. Any help would be appreciated
Here is the xslt that I have:
<xsl:for-each select="document(#fileRef)/foo/bar">
<xsl:sort select="translate(child::Title/text(), '>', '')" order="ascending" use-when="system-property('customerCode')='CAT'"
collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"/>
<!-- do some stuff here -->
</xsl:for-each>
Using oXygen I got the following to work in an Ant file:
<xslt in="sample1.xml" out="sample1-transformed.xml" force="true" style="sheet1.xsl">
<factory name="net.sf.saxon.TransformerFactoryImpl"/>
<sysproperty key="cat" value="bar"/>
</xslt>
XML sample1.xml is e.g.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>c</item>
<item>a</item>
</root>
XSLT is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:comment select="system-property('cat')"/>
<xsl:next-match/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="item">
<xsl:sort select="." use-when="system-property('cat') = 'foo'"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
and then the output items are sorted only if the Ant sets e.g. <sysproperty key="cat" value="foo"/>.
I am trying to write an XSLT to modify a config file. I have tried to use the replace function but that is only supported in 2.0 and i have tried to use translate but "true" trnaslated to "false" gets truncated to 'fals'. I cant just replace the whole modules section since our customers are in a distributed environment and I don't know if they have added any thing else to the section.
What I am starting with:
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
</modules>
Desired output:
<modules runAllManagedModulesForAllRequests="false">
<remove name="FormsAuthentication" />
</modules>
This is what I thought would do the trick.
<xsl:template match="/configuration/system.webServer/modules">
<xsl:choose>
<xsl:when test="#name=runAllManagedModulesForAllRequests">
<xsl:copy>
<xsl:copy-of select="<modules runAllManagedModulesForAllRequests="false">"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="#*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Would this work for you?
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>
<xsl:template match="#runAllManagedModulesForAllRequests[.='true']">
<xsl:attribute name="runAllManagedModulesForAllRequests">false</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
I have the below input XML,
for some reason my xslt is not able to sort it.
please advice.
appreciate your insights.
this is the full XML
......................................................
I want to sort the G_SHL segment with its content based on D_628 field.
`<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<S_ISA>
<D_I01>00</D_I01>
<D_I02/>
<D_I03>00</D_I03>
<D_I04/>
<D_I05>01</D_I05>
<D_I06></D_I06>
<D_I05_2>ZZ</D_I05_2>
<D_I07></D_I07>
<D_I08>160427</D_I08>
<D_I09>1106</D_I09>
<D_I10>U</D_I10>
<D_I11>00401</D_I11>
<D_I12>000000001</D_I12>
<D_I13>0</D_I13>
<D_I14/>
<D_I15>></D_I15>
<S_GS>
<D_479>SH</D_479>
<D_142></D_142>
<D_124></D_124>
<D_373>20160427</D_373>
<D_337>1106</D_337>
<D_28>1</D_28>
<D_455>X</D_455>
<D_480>004010</D_480>
<S_ST>
<D_143>856</D_143>
<D_329>0001</D_329>
<S_BSN>
<D_353>00</D_353>
<D_396>0081664420</D_396>
<D_373>20160426</D_373>
<D_337>1347</D_337>
<D_1005>0001</D_1005>
</S_BSN>
<G_SHL>
<S_HL>
<D_628>0000001</D_628>
<D_734>0000000</D_734>
<D_735>S</D_735>
<S_PRF>
<D_324>SITT10-1447195769627</D_324>
<D_328>SITT10-1447195769627</D_328>
</S_PRF>
<S_MEA>
<D_737>PD</D_737>
<D_738>G</D_738>
<D_739>0.081</D_739>
<C_C001>
<D_355>KG</D_355>
</C_C001>
</S_MEA>
<S_TD1>
<D_103>PKG</D_103>
<D_80>00002</D_80>
<D_187>G</D_187>
<D_81>00000.18</D_81>
<D_355>LB</D_355>
</S_TD1>
<S_TD5>
<D_133>O</D_133>
<D_66>2</D_66>
<D_368>CC</D_368>
</S_TD5>
<S_REF>
<D_128>WH</D_128>
<D_127>WH:</D_127>
</S_REF>
<S_REF>
<D_128>RN</D_128>
<D_127>RN:</D_127>
</S_REF>
<S_DTM>
<D_374>011</D_374>
<D_373>20160414</D_373>
</S_DTM>
<G_SN1>
<S_N1>
<D_98>ST</D_98>
<D_93>C3333</D_93>
<S_N3>
<D_166>TEST</D_166>
</S_N3>
<S_N4>
<D_19>TEST</D_19>
<D_116>12345</D_116>
<D_26>US</D_26>
</S_N4>
</S_N1>
</G_SN1>
</S_HL>
</G_SHL>
<G_SHL>
<S_HL>
<D_628>0000002</D_628>
<D_734>0000001</D_734>
<D_735>O</D_735>
<S_MAN>
<D_88>CP</D_88>
<D_87></D_87>
</S_MAN>
</S_HL>
</G_SHL>
<G_SHL>
<S_HL>
<D_628>0000003</D_628>
<D_734>0000002</D_734>
<D_735>P</D_735>
</S_HL>
</G_SHL>
<G_SHL>
<S_HL>
<D_628>0000005</D_628>
<D_734>0000002</D_734>
<D_735>P</D_735>
</S_HL>
</G_SHL>
<G_SHL>
<S_HL>
<D_628>0000004</D_628>
<D_734>0000003</D_734>
<D_735>I</D_735>
<S_LIN>
<D_350>000010</D_350>
<D_235>SK</D_235>
<D_234></D_234>
<D_235_2>BP</D_235_2>
<D_234_2></D_234_2>
</S_LIN>
<S_SN1>
<D_350></D_350>
<D_382>2.000</D_382>
<D_355>EA</D_355>
<D_668>AC</D_668>
</S_SN1>
</S_HL>
</G_SHL>
<G_SHL>
<S_HL>
<D_628>0000006</D_628>
<D_734>0000005</D_734>
<D_735>I</D_735>
<S_LIN>
<D_350>000020</D_350>
<D_235></D_235>
<D_234></D_234>
<D_235_2>BP</D_235_2>
<D_234_2></D_234_2>
</S_LIN>
<S_SN1>
<D_382>1.000</D_382>
<D_355>EA</D_355>
<D_668>AC</D_668>
</S_SN1>
</S_HL>
</G_SHL>
<S_CTT>
<D_354>1</D_354>
</S_CTT>
<S_SE>
<D_96>[]</D_96>
<D_329>0001</D_329>
</S_SE>
</S_ST>
<S_GE>
<D_97>0</D_97>
<D_28>1</D_28>
</S_GE>
</S_GS>
<S_IEA>
<D_I16>01</D_I16>
<D_I12>000000001</D_I12>
</S_IEA>
</S_ISA>
</LIST>`
Try
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/LIST/S_ISA/S_GS/S_ST">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="G_SHL">
<xsl:sort select="S_HL/D_628" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
I can't verify the path /LIST/S_ISA/S_GS/S_ST but assume you have those ancestor elements in your real XML.
If there can be sibling elements you want to keep in their original input position then one way in XSLT 2.0 is to first identify the adjacent elements you want to sort with for-each-group group-adjacent:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/LIST/S_ISA/S_GS/S_ST">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="*" group-adjacent="boolean(self::G_SHL)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()">
<xsl:sort select="S_HL/D_628" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"></xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In Xpath how would I get my results to select all documents and display them like
<Document>
<Size>100</Size>
<Offset>200</Offset>
<FileName>x.doc</FileName>
</Document>
from
<Documents>
<Document>
<Size>100</Size>
<Offset>200</Offset>
<FileName>x.doc</FileName>
<Details>
<Header>asdfasdf</Header>
<Footer>adfasdfas</Footer>
<Author>asdfasdfs</Author>
<Date>20140101</Date>
</Details>
<Document>
</Documents>
Thanks.
With XSLT you get the result with
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="//Documents//*">
<xsl:if test="local-name(parent::*)!='Details' and local-name(.) != 'Details'">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
So you would get a copy of all Documents excluding the Details.
I have an xml structure like the following :
<doc>
<line void="false">
<lineNumber>1</lineNumber>
<info1>ddddd</info1>
<info2>aaaaa</info2>
</line>
<line void="true">
<lineNumber>2</lineNumber>
<voidLineNumber>1</voidLineNumber>
<voidValue>2.00</voidValue>
</line>
</doc>
I need one single set of data. I would like to select all the lines where void = false as well as the voidLineNumber and voidValue data from the line where void = true and the voidLineNumber = lineNumber from the original line.
Is this possible? Any help would be appreciated. Thanks
As Michael Kay noted, XPath itself can only be used to select nodes, not to transform them. You can do what you want with XSLT:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="doc">
<xsl:apply-templates select="line" />
</xsl:template>
<xsl:template match="line">
<xsl:variable name="voidvalue"><xsl:value-of select="#void" /></xsl:variable>
<xsl:if test="$voidvalue='false'">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:if test="name(.)='lineNumber'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>