I have a file say abc.txt which contains..
<mapping number=1 name=m1>
<transformation type=aggregator name=agg_m1> />
<transformation type=joiner name=j_m1 />
</mapping>
<mapping number=2 name=m2>
<transformation type=router name=rtr_m2> />
<transformation type=joiner name=j_m2 />
</mapping>
I neen an output of the form of the same order. i.e, i need to get name field from mapping and type and name fields from transformation "in the same order :
name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
name=m2
type=router name=rtr_m2
type=joiner name=j_m2
I have used the command 'awk' to get the output , but I am getting it of the following form which is incorrect :
name=m1
name=m2
type=aggregator name=agg_m1
type=joiner name=j_m1
type=router name=rtr_m2
type=joiner name=j_m2
I tried using 'grep' and 'sed' commands too but I am getting the desired output, because the order is not preserved. I need to the get the output where the order is preserved.
perl -lne '$_=~m/^[^\s]*\s([^\/\>]*)/g;print $1' your_file
tested below:
> cat temp
<mapping number=1 name=m1>
<transformation type=aggregator name=agg_m1> />
<transformation type=joiner name=j_m1 />
</mapping>
<mapping number=2 name=m2>
<transformation type=router name=rtr_m2> />
<transformation type=joiner name=j_m2 />
</mapping>
> perl -lne '$_=~m/^[^\s]*\s([^\/\>]*)/g;print $1' temp
number=1 name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
number=2 name=m2
type=router name=rtr_m2
type=joiner name=j_m2
>
If you do not need number,then use the below regex:
perl -lne '$_=~m/^[^\s]*\s[number=\d]*([^\/\>]*)/g;print $1' your_file
Here's one way using grep:
grep -oE "(name|type)=[^ >]* *[^ >]*" file
Results:
name=m1
type=aggregator name=agg_m1
type=joiner name=j_m1
name=m2
type=router name=rtr_m2
type=joiner name=j_m2
Related
I am working on writing rules using Schematron to validate data below. The requirement is to verify whether a patient has at least one encounter in the past 12 months. If there are multiple encounters per patient, use the last encounter.
<root>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/12345</id>
</subject>
<encounterDate>2018-04-10T10:00:00</encounterDate>
</resource>
</entry>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/abcde</id>
</subject>
<encounterDate>2020-04-10T10:00:00</encounterDate>
</resource>
</entry>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/abcde</id>
</subject>
<encounterDate>2019-05-10T10:00:00</encounterDate>
</resource>
</entry>
</root>
The above data should pass the validation because the latest encounter is less than a year ago.
What I want to know is, if I write a template that groups encounters together by patient id, is there a way to pass that template to the rule context? If not, is there any other way of doing it?
I am completely new to both xslt and Schematron and here is what I have so far:
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern>
<key name="patientId" match="entry" use="/resouce/subject/id/text()"/>
<template name="dateByPatient" match="entry">
<root>
<for-each select="resource/subject/id">
<patient >
<for-each select="key('patientId',text())">
<effectiveDateTime><value-of select="./resource/encounterDate"/></effectiveDateTime>
</for-each>
</patient>
</for-each>
</root>
</template>
<let name="template">
<dateByPatient/>
</let>
<let name="latest">
<root>
<for-each select="$template/root/patient">
<patient >
<sort select="effectiveDateTime" order="descending" />
<if test="position() = 1">
<effectiveDateTime><value-of select="effectiveDateTime" /></effectiveDateTime>
</if>
</patient>
</for-each>
</root>
</let>
<rule context="$latest/root/patient/effectiveDateTime">
<let name="days" value="days-from-duration(fn:current-dateTime() - xs:dateTime(text()))" />
<assert test="days-from-duration(fn:current-dateTime() - xs:dateTime(text())) < 365">
Encounter date more than a year : <value-of select="$days" /> days
</assert>
</rule>
</pattern>
</schema>
With XSLT 3 underlying you could use
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="groups"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
return map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)"/>
<sch:assert
test="every $patient in map:keys($groups)
satisfies
(current-dateTime() - max($groups($patient)))
lt xs:dayTimeDuration('P365D')">At least one patient with latest encounter more than a year ago.</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
Or to output more detailed information and to only process resources with type Encounter:
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="groups"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
return map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)"/>
<sch:let name="failing-patients"
value="map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
<sch:report
test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
I don't think you can mix Schematron and XSLT as freely as your code tries, you would need to set up an XProc pipeline to use p:xslt to group the original input and then a validation step to validate with Schematron.
As for your problems to run the second sample with node-schematron, it uses an XPath implementation that doesn't support the XPath 3.1 sort function it seems, node-schematron also fails to handle maps as intermediary results of a Schematron variable, so only stuffing all into one variable expression seems to do; two examples work:
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="failing-patients"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter'],
$groups := map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)
return map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
<sch:report
test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
or
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="failing-patients"
value="let
$encounter-resources := entry/resource[resourceType = 'Encounter'],
$groups := fold-left(
$encounter-resources,
map{},
function($m, $e) {
map:put(
$m,
data($e/subject/id),
max((xs:dateTime($e/encounterDate), map:get($m, data($e/subject/id))))
)
})
return map:keys($groups)[(current-dateTime() - $groups(.)) gt xs:dayTimeDuration('P365D')]"/>
<sch:report test="exists($failing-patients)">Patients <sch:value-of
select="$failing-patients"/> with latest encounter more than a year
ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
If you need an assertion that fails then replace the sch:report with
<sch:assert
test="empty($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:assert>
I'm trying to change an xml element value from "true" to "false" using ANT replaceregexp task but am having difficulties matching across a new line. The relevant part of the XML node in question:
<validationRules>
<fullName>CAReversaApprovallLockdown</fullName>
<active>true</active>
In my text editor (sublime), I'm able to use the following regex to find/replace but I can't figure out how to replicate this in ANT replaceregexp:
/fullname>\n <active>true
I can't figure out the correct syntax to match the combination of the newline and the spacing afterwards. The spacing after the newline is always the same, if that makes things easier.
Looking at https://ant.apache.org/manual/Tasks/replaceregexp.html I've tried various combinations of ^ and $ with m flag, \s+ for spaces etc but just can't hit the right combo....any ideas?
My current progress is below but no luck unfortunately...
<target name="deactivate_val_rules">
<echo message="deactivating validation rules..." />
<replaceregexp match="/fullname>\r\n\s+<active>true" flags="gim" byline="false">
<substitution expression="/fullname>\r\n <active>false"/>
<fileset dir="src\objects" includes="Claim_Approvals__c.object"/>
</replaceregexp>
</target>
Got it - the following gave the correct result:
<target name="deactivate_val_rules">
<echo message="deactivating workflows..." />
<replaceregexp match="/fullname>\r\n\s+<active>true" flags="gis" byline="false">
<substitution expression="/fullname>${line.separator} <active>false"/>
<fileset dir="src\objects" includes="Claim_Approvals__c.object"/>
</replaceregexp>
</target>
The output viewed via diff is:
- <fullName>the_name</fullName>
- <active>true</active>
+ <fullName>the_name</fullname>
+ <active>false</active>
To Use replaceregexp you need to define the value to be changed as reference.
For Example:
<validationRules>
<fullName>CAReversaApprovallLockdown</fullName>
<active>true</active>
Ant:
<target name = "deactivate_val_rules">
<echo message="deactivating validation rules..." />
<replaceregexp file="${FILE_LOACTION}/FILE_NAME.FILE_EXT" match="true" replace="false" />
</target>
i wanted to replace what ever lines come between
<? and <Arguments>
and also
</Arguments> and </Task>
via powershell and regex
here's the entire string
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>administrator</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Enabled>true</Enabled>
<StartBoundary>2013-03-13T00:34:00</StartBoundary>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Settings>
<Enabled>true</Enabled>
<ExecutionTimeLimit>PT259200S</ExecutionTimeLimit>
<Hidden>false</Hidden>
<WakeToRun>false</WakeToRun>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<Priority>5</Priority>
<IdleSettings>
<Duration>PT600S</Duration>
<WaitTimeout>PT3600S</WaitTimeout>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
</Settings>
<Principals>
<Principal id="Author">
<RunLevel>HighestAvailable</RunLevel>
<UserId>SMETEST\Administrator</UserId>
<LogonType>InteractiveTokenOrPassword</LogonType>
</Principal>
</Principals>
<Actions Context="Author">
<Exec>
<Command>C:\Program Files\NetApp\SnapManager for Exchange\SMEJobLauncher.exe</Command>
<Arguments>new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -Sto
rageGroup 'testingDB1' -UseMountPoint -MountPointDir 'C:\Program Files\MgrMountPoi
nt' -RemoteAdditionalCopyBackup $False</Arguments>
<WorkingDirectory>C:\Program Files\NetApp\SnapManager for Exchange\</WorkingDirectory>
</Exec>
</Actions>
</Task>
i just want the output to look like
new-backup -Server 'SME' -ManagementGroup 'Standard' -RetainBackups 8 -RetainUtmBackups 20 -StorageGroup 'testingDB1' -UseMountPoint -MountPointDir 'C:\Program Files\MgrMountPoint' -RemoteAdditionalCopyBackup $False
Instead of wasting time on regex with XML - use Select-Xml and XPath:
Select-Xml -Path .\Test.xml -Namespace #{
t = "http://schemas.microsoft.com/windows/2004/02/mit/task"
} -XPath //t:Arguments | foreach { $_.node.InnerText -replace '\n' }
I've remove newlines, assuming that's what you want.
I have run the my unit tests written with the google test 1.6.0 framework with the --gtest_output="xml:test-results.xml" flag and get a test result file like this:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="8" failures="0" disabled="0" errors="0" time="81.396" name="AllTests">
<testsuite name="TestSuite1" tests="8" failures="0" disabled="0" errors="0" time="81.396">
<testcase name="Test1" status="run" time="6.391" classname="Class1" />
<testcase name="Test2" status="run" time="6.1" classname="Class1" />
<testcase name="Test3" status="run" time="7.146" classname="Class1" />
<testcase name="Test4" status="run" time="16.164" classname="Class1" />
<testcase name="Test5" status="run" time="25.145" classname="Class1" />
<testcase name="Test6" status="run" time="6.099" classname="Class1" />
<testcase name="Test7" status="run" time="6.162" classname="Class1" />
<testcase name="Test8" status="run" time="8.187" classname="Class1" />
</testsuite>
</testsuites>
Based on what I have read in other posts the gtest xml output is supposed to be compatible with junit parsers. Related Post: Unable to get hudson to parse JUnit test output XML
The other possibility of error is in my bitten script. Running on Trac 0.12.2. Here is my bitten recipe for parsing the XML file using the java:junit parser:
<build xmlns:java="http://bitten.edgewall.org/tools/java">
<step id="parse_results" description="Gather Results" onerror="fail">
<java:junit file="/home/user/temp/test-results.xml" />
</step>
</build>
In trac, it says that the build was successful, but the test results are blank. 0 run, 0 failed, 0 ignored, 0 errors
Thanks.
I was able to solve the problem. It turns out that Trac's JUnit parser has a bug. It doesn't like the testsuites tag and it doesn't like having multiple testsuite sections. PHP allows the testsuites tag, but will not do multiple files. I chose to create a parser in Python that creates multiple XML files from the Gtest output file.
def move_results(results, results_dir):
# Moves all results into a temp folder to be consumed by Bitten
# Files with multiple test suite sections, split into individual files
for files in results:
fin = open(files)
test_id = 0
split_line = files.split('/')
file_name = split_line[len(split_line)-1].split('.xml')
for line in fin:
if not 'testsuites' in line:
if '<testsuite ' in line:
output_file = results_dir + file_name[0] + '-' + str(test_id) + '.xml'
test_id = test_id + 1
fout = open(output_file, 'w')
fout.write('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n')
fout.write(line)
elif '<testsuite\\>' in line:
fout.write(line)
fout.close()
elif not '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n' in line:
fout.write(line)
fin.close()
os.remove(files)
Alternatively, you could use Bitten to do convert using an XSLT. Also you could combine the output files into a single output (with something like nose's XUnit output) and use php:phpunit to parse the file.
I have a list of files generated on each build inside a directory C:\BuildArtifacts
The Contents of a directory looks like this:
TestBuild-1.0.0.1.zip
TestBuild-1.0.0.2.zip
TestBuild-1.0.0.3.zip
TestBuild-1.0.0.4.zip
TestBuild-1.0.0.5.zip
TestBuild-1.0.0.6.zip
Now, with each incremental build, I just want to retain two recent artifacts and delete the rest. So, in this example, I want to retain TestBuild-1.0.0.5.zip and TestBuild-1.0.0.6.zip
How can I do it with MSBuild?
Note:
I have managed to fetch the above list in an item
<Exec WorkingDirectory="$(Artifacts)\.." Command="dir /B /A:-D /O:-N" Outputs="ArchiveFileList" />
Well, we wrote a custom task to sort the files by their name and then output the list of the files to delete (excluding the first two in the list) into an Item
Custom Task:
<UsingTask
TaskName="Cleanup"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<TargetPath ParameterType="System.String" Required="true"/>
<BackupLength ParameterType="System.Int32" Required="true"/>
<FilesToExclude ParameterType="System.String[]" Output="true" />
<FilesToDelete ParameterType="System.String[]" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var diInfo = new DirectoryInfo(TargetPath);
if (diInfo.Exists)
{
var fiInfo = diInfo.GetFiles().OrderByDescending(file => file.Name);
FilesToExclude = fiInfo.Take(BackupLength).Select(file => file.FullName).ToArray();
FilesToDelete = fiInfo.Skip(BackupLength).Select(file => file.FullName).ToArray();
}
]]>
</Code>
</Task>
</UsingTask>
Usage:
<!-- Clean old archives. Keep the recent two and deletes rest. -->
<Cleanup TargetPath="$(PackageRoot)" BackupLength="2">
<Output TaskParameter="FilesToDelete" ItemName="FilesToClean" />
</Cleanup>
<Message Text="Cleaning Old Archives" Importance="High" />
<Delete Files="#(FilesToClean)" />
Please test this command then <exec ...> it with del instead of echo:
for /f %x in ('cmd /c "dir /B /A-D /O-N | more +2"') do echo %x