Retrieving a text string from a xml document - powershell

I am trying to retrieve the string "This is my string" that belongs to text id="short_name
I have tried:
$SVGTemplate = Get-Content "C:\temp\sample.svg
$SVGTemplate = [XML]$SVGTemplate
$SVGTemplateShortName = Select-Xml -Xml $SVGTemplate -XPath '/s:svg/s:g/s:g[#id="short_name"]/s:text/text()' -Namespace #{s = "http://www.w3.org/2000/svg"}
$SVGTemplateShortName.node.value
But it returns an empty value. This used to work before and recently broke because I had to redesign the svg graphic.
I have tried to correct the XPath many times and it keeps returning an empty value. What could I be doing wrong?
Below is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 26.5.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<style type="text/css">
.st0{fill:#E30613;}
.st1{fill:#FFFFFF;}
.st2{fill:#009FE3;}
.st3{fill:none;}
.st4{font-family:'MyriadPro-Regular';}
.st5{font-size:88px;}
.st6{fill:#008D36;}
</style>
<g id="background">
<rect id="stroke" class="st0" width="500" height="500"/>
<rect id="Fill" x="12" y="12" class="st1" width="476" height="476"/>
</g>
<g id="short">
<rect id="short_fill" x="12" y="400" class="st2" width="476" height="100"/>
<rect y="412" class="st3" width="500" height="88"/>
<text id="short_name" transform="matrix(1 0 0 1 87.7305 474.4795)" class="st1 st4 st5">This is my string</text>
</g>
<g id="hud">
<rect id="left" x="12" y="12" class="st6" width="92" height="76"/>
<rect id="right" x="396" y="12" width="92" height="76"/>
</g>
<g id="graphic">
<g id="Card_Symbl">
<g id="Layer_10">
<polygon class="st0" points="309.9,387.1 396.3,124.5 303.1,105.8 96.5,251.1 "/>
</g>
</g>
</g>
</svg>

If you want to locate an element by its full path, an easier way is to use PowerShell's member access:
$SVGTemplate.svg.g.text | Where-Object id -eq 'short_name' | ForEach-Object '#text'
... or more succinct, using intrinsic .Where method:
$SVGTemplate.svg.g.text.Where{ $_.id -eq 'short_name'}.'#text'
XPath does have an advantage if you want to locate an element regardless of how deep it is nested (relative path, introduced by double slash):
$SVGTemplate |
Select-Xml -XPath '//s:text[#id="short_name"]' -Namespace #{s = "http://www.w3.org/2000/svg"} |
ForEach-Object { $_.Node.'#text' }
Of course it also works using the absolute path (introduced using single slash), but is much more lengthy than its member access counterpart:
$SVGTemplate |
Select-Xml -XPath '/s:svg/s:g/s:text[#id="short_name"]' -Namespace #{s = "http://www.w3.org/2000/svg"} |
ForEach-Object { $_.Node.'#text' }

The original XPath expects a <g> tag with an id attribute and a value of "short_name"... however the XML does not contain such a path. Was it supposed to read `/s:text[#id="short_name"]?
Other than that, yes anyone can use PS´s "human-friendly" access methods... though there is a bit of an inherent danger there when there is no schema attached to the XML or when trying to access optional nodes that might or might not be there for a given path.
PS works with lists. XML paths break when using $xmlDoc.tagA.tagB.tagC notation; assuming this is not $null and is a list longer than exactly one (1) node then we have no way of telling where in the XML and under possibly what conditions we got those nodes from.
... When in doubt: don't.

Related

How to auto-wrap if total html or component properties >= n?

As I understand with the default settings in vscode. The properties below will only auto-wrap when it reaches to the maxiumum wordWrapColumn. (See below)
<h1 className="sample" height="20" width="30" color="20" size="200" />
// auto separated
<h1
className="sample"
height="20"
width="30"
color="blue"
size="200"
/>
I wanna ask if its possible to change this behavior? Instead of basing to wordWrapColumn, we check the total properties of an element / component.
We auto-newline when it reaches to the specified total props.
totalHtmlProps = 3
## case 1
<h1 className="sample" height="20" />
## case 2
<h1
className="sample"
height="20"
color="blue"
/>
a quick update: i'm also using prettier with this.

URDF model keeps disappearing whenever I add intertial properties to a link? How can I resolve this issue?

I am trying to simulate a simple pendulum using pydrake and am having a couple of issues creating the urdf file to creat the model for the pendulum. I have no background knowledge on urdf or drake and am open to any suggestions to resolve this issue.
This is the code I have so far:
import os
from tempfile import mkdtemp
from pydrake.all import (
AddMultibodyPlantSceneGraph,
DiagramBuilder,
Meshcat,
MeshcatVisualizerCpp,
Parser,
Simulator,
)
from manipulation.meshcat_cpp_utils import (
StartMeshcat, MeshcatJointSlidersThatPublish)
# Start the visualizer.
meshcat = StartMeshcat()
tmp_dir = mkdtemp()
model_file = os.path.join(tmp_dir, "test_model.urdf")
model_text = f"""\
<?xml version="1.0"?>
<robot name="materials">
<material name="blue">
<color rgba="0 0 0.8 1"/>
</material>
<material name="white">
<color rgba="1 1 1 1"/>
</material>
<material name="green">
<color rgba="0.0 0.8 0.0 1.0"/>
</material>
<link name="base">
<visual>
<geometry>
<box size="0.5 0.5 0.1"/>
</geometry>
</visual>
<inertial>
<mass value="1"/>
</inertial>
</link>
<joint name="world_2_base" type="fixed">
<parent link="world"/>
<child link="base"/>
<origin xyz="0 0 0.5"/>
</joint>
<link name="arm">
<visual>
<geometry>
<cylinder length="0.5" radius="0.01"/>
</geometry>
<material name="blue"/>
</visual>
</link>
<joint name="base" type="revolute">
<parent link="base"/>
<child link="arm"/>
<axis xyz="1 0 0"/>
<origin xyz="0 0 -0.3"/>
</joint>
<link name="ball">
<visual>
<geometry>
<sphere radius="0.1"/>
</geometry>
<material name="green"/>
</visual>
</link>
<joint name="base2" type="fixed">
<parent link="arm"/>
<child link="ball"/>
<origin xyz="0 0 -0.35"/>
</joint>
</robot>
"""
# Write temporary file.
with open(model_file, "w") as f:
f.write(model_text)
builder = DiagramBuilder()
plant, scene_graph = AddMultibodyPlantSceneGraph(builder, time_step=0.0)
# Add pendulum model.
model = Parser(plant, scene_graph).AddModelFromFile(model_file)
# - Weld base at origin.
base_link = plant.GetBodyByName("base", model)
#plant.WeldFrames(plant.world_frame(), base_link.body_frame())
plant.Finalize()
visualizer = MeshcatVisualizerCpp.AddToBuilder(builder, scene_graph, meshcat)
diagram = builder.Build()
simulator = Simulator(diagram)
simulator.set_target_realtime_rate(1.0)
simulator.AdvanceTo(5.0)
This code gives me a blanck meshcat screen. But when I remove the inertia tags in the base link I can see a model of the pendulum. Please let me know what I am doing wrong. Also if you can reccomend some resources to learn about urdf and drake that would be really helpful.
Thank you very much.
I believe that the problem is that you have no mass in the arm, so when you go to simulate it, the simulation is ill-defined (probably you get NaNs in the state variables).
If you want to check just the visualization, I'd recommend doing
context = diagram.CreateDefaultContext()
diagram.Publish(context)
instead of the simulator step.
I have started working on a proper tutorial for precisely this. It's very much a draft right now, but in case it helps you can find it here. I intend for this to be one of the real Drake tutorials soon.

Modifying SVG files flutter

I have an svg file of the human body with groups of traces made for each muscle. I'd like to make a function that highlights certain muscles. Using regex is not the right way to go. I tried using xml but I'm not sure how to do it the right way and I'm not sure if xml truly is the right way.
This is the structure of my svg file:
<svg>
<g id="Muscles" transform="translate(-0.146 0.364)">
<g id="Abdominals">
<path id="path196" d="M3294.281-2851.382c-15.892,20.521-35.62,44.154-48.66,64.122,0,0,31.81-113.859-15.028-518.839,0,0,65.728,78.144,94.49,251.4,0,0,10.452,150.03-30.8,203.315" transform="translate(-1825.079 4663.033)" fill="#95999f"/>
<path id="path234" d="M3671.994-2915.5c15.893,20.542,35.6,44.178,48.659,64.119,0,0-23.994-49.737,22.844-454.717,0,0-65.7,78.144-94.491,251.4,0,0-18.265,85.908,22.988,139.2" transform="translate(-1494.489 4663.033)" fill="#95999f"/>
</g>
...
...
<g id="Triceps">
<path id="path198" d="M3172.882-3654.165s-144.517,96.743-129.823,251.042c14.692,154.272,59.807,225.4,10.234,308.571,0,0,127.978-21.957,152.021-141.282,24.066-119.325-28.764-221.175-32.433-418.331" transform="translate(-1975.527 4385.352)" fill="#95999f"/>
<path id="path236" d="M3832.757-3654.165s144.494,96.743,129.8,251.042c-14.694,154.272-59.806,225.4-10.236,308.571,0,0-127.952-21.957-152.02-141.282s28.787-221.175,32.456-418.331" transform="translate(-1375.567 4385.352)" fill="#95999f"/>
</g>
</g>
</svg>
I'm trying to change the color of a given muscles.
Can you do that:
let abd = document.getElementById("Abdominals").getElementsByTagName("path")
for (let path of abd) {
path.setAttribute("fill", "red");
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="1066.48291015625 731.18701171875 1521.556640625 1144.586181640625" version="1.1" >
<g id="Muscles" transform="translate(-0.146 0.364)">
<g id="Abdominals">
<path id="path196" d="M3294.281-2851.382c-15.892,20.521-35.62,44.154-48.66,64.122,0,0,31.81-113.859-15.028-518.839,0,0,65.728,78.144,94.49,251.4,0,0,10.452,150.03-30.8,203.315" transform="translate(-1825.079 4663.033)" fill="#95999f"/>
<path id="path234" d="M3671.994-2915.5c15.893,20.542,35.6,44.178,48.659,64.119,0,0-23.994-49.737,22.844-454.717,0,0-65.7,78.144-94.491,251.4,0,0-18.265,85.908,22.988,139.2" transform="translate(-1494.489 4663.033)" fill="#95999f"/>
</g>
<g id="Triceps">
<path id="path198" d="M3172.882-3654.165s-144.517,96.743-129.823,251.042c14.692,154.272,59.807,225.4,10.234,308.571,0,0,127.978-21.957,152.021-141.282,24.066-119.325-28.764-221.175-32.433-418.331" transform="translate(-1975.527 4385.352)" fill="#95999f"/>
<path id="path236" d="M3832.757-3654.165s144.494,96.743,129.8,251.042c-14.694,154.272-59.806,225.4-10.236,308.571,0,0-127.952-21.957-152.02-141.282s28.787-221.175,32.456-418.331" transform="translate(-1375.567 4385.352)" fill="#95999f"/>
</g>
</g>
</svg>
It's the easiest way e.g. to get your path elements via their IDs and then change their attributes as usual. You don't have to change attributes of all path children elements of your groups since you retrieve their entire collection etc.

Heatbar, dynamic position offset of text

I want to insert a new column in my report to graphically display a "needle" displaying the position in a range of 0% - 100% like so:
I am looking for ideas please!
My idea
Use graphic (rainbow 0%-100% in every detail column).
Use a pixel offset (somehow??) where I simply place an overly large "|" type. So if 0%-100% are 200px that would mean that to display the needle at 50% I would offset the type 100px!
The easiest / most straightforward option I have found and implemented:
<image scaleImage="RetainShape" onErrorType="Blank">
<reportElement x="575" y="1" width="100" height="13" uuid="7cf54aff-65ac-40e5-8dc9-7f95d6f34d49"/>
<imageExpression>
<![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("
<svg id=\"Layer_1\" data-name=\"Layer 1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100\" height=\"13\" viewBox=\"0 0 100 13\">
<defs>
<linearGradient id=\"linear-gradient\" x1=\"-176.88\" y1=\"316.13\" x2=\"-150.45\" y2=\"316.13\" gradientTransform=\"matrix(3.78, 0, 0, -3.78, 689.01, 1202.56)\" gradientUnits=\"userSpaceOnUse\">
<stop offset=\"0\" stop-color=\"green\"/>
<stop offset=\"1\" stop-color=\"green\" stop-opacity=\"0\"/>
</linearGradient>
</defs>
<path d=\"M0,1.26H100v10.4H0Z\" style=\"fill: url(#linear-gradient)\"/>
<path d=\"M" + !!! OFFSET CALCULATION !!! + ",0V13\" style=\"fill: none;stroke: #000\"/>
</svg>
")]]></imageExpression>
</image>
Result:

removing extra space from top and left when using formitems in flex

I want to remove the extra space/padding/gap from FormItem I,m using in form for a mobile project in flex 4.6
I have tried making gap and padding to 0 but there the form and it's fields are still have indent to the right and top ?
<s:FormItem label="Start Date" width="150">
<s:HGroup>
<s:TextInput id="startDate" width="90" editable="false"/>
<s:Button id="setStartDate" label="set" height="34" fontSize="10" click="setStartDate_clickHandler(event)"/>
</s:HGroup>
</s:FormItem>
<s:FormItem label="End Date" width="150">
<s:HGroup>
<s:TextInput id="endDate" width="90" editable="false"/>
<s:Button id="setEndDate" label="set" height="34" fontSize="10" click="setEndDate_clickHandler(event)"/>
</s:HGroup>
</s:FormItem>
</s:HGroup>
</s:Form>
Gap between two form items, verticalGap.(set it on Form container)
Default hgroup gap is 6, set it to 0.
If gap between two items withn the FormItem, verticalGap (set this on the form container)
Form has FormHeading, set paddingTop=0 on this FormHeading comp. Also, try to set the height of this to 0 and PaddingTop = 0 on the form to reduce the gap btwn the first element and the top of the Form container.
This has good information about it http://livedocs.adobe.com/flex/3/html/help.html?content=layouts_08.html