Xpath Query Help. Selecting data from multiple paths as a single data set - xpath

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>

Related

XSLT Sort on child subelements

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>

XSLT 1.0 to modify config file replace function

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>

Split variables separated by a comma, and perform an action on each

I am using XSLT v1.0 and running it through Microsoft Visual Studio.
I have data (which will be different in every node) but is structured like this:
<ItemGroupData ItemGroupOID="DDG4">
<ItemDataString ItemOID="DDLOCC" AuditRecordID="AR.8452551">5,8,9,2,3</ItemDataString>
<ItemDataString ItemOID="DDLOCL" AuditRecordID="AR.8452551">1,7</ItemDataString>
<ItemDataString ItemOID="DDLOCR" AuditRecordID="AR.8452551">1</ItemDataString>
</ItemGroupData>
There can be any number of values separated by commas in each of the 3 fields.
I am trying to split the data so I can work with each individual integer, and have tried the method suggested by Dimitre Novatchev here: split function in xslt 1.0, but it keeps giving me the error:
"'template' is not a recognized extension element. An error occurred at (0,0).".
The new split/mark templates I created are inside my overall template, which is being used to convert my XML to a CSV file.
Can I have a template within a template? Or do I need to define it outside the main template? Bit of a N00b with XML so any help would be greatly appreciated.
My new templates (note the processedItem bit has been simplified for demonstration purposes):
<xsl:template match="mark">
<xsl:variable name="vrtfSplit">
<xsl:apply-templates/>
</xsl:variable>
<xsl:for-each select="ext:node-set($vrtfSplit)/*">
<processedItem>
<xsl:if test="$varLOCOID='DDLOCL'">
<xsl:value-of select="current() * 100"/>
</xsl:if>
<xsl:if test="$varLOCOID='DDLOCC'">
<xsl:value-of select="current() * 10"/>
</xsl:if>
<xsl:if test="$varLOCOID='DDLOCR'">
<xsl:value-of select="current() * 150"/>
</xsl:if>
</processedItem>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText) > 0">
<item>
<xsl:value-of select="substring-before(concat($pText, ','), ',')"/>
</item>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Consider the following example:
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:template match="/ItemGroupData">
<output>
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="ItemDataString">
<items>
<xsl:call-template name="tokenize-and-process">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="factor">
<xsl:choose>
<xsl:when test="#ItemOID='DDLOCL'">100</xsl:when>
<xsl:when test="#ItemOID='DDLOCC'">10</xsl:when>
<xsl:when test="#ItemOID='DDLOCR'">150</xsl:when>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</items>
</xsl:template>
<xsl:template name="tokenize-and-process">
<xsl:param name="text"/>
<xsl:param name="factor" select="1"/>
<xsl:param name="delimiter" select="','"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<item>
<xsl:value-of select="$token * $factor"/>
</item>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize-and-process">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
<xsl:with-param name="factor" select="$factor"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Applied to your example input XML, the result will be:
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<items>
<item>50</item>
<item>80</item>
<item>90</item>
<item>20</item>
<item>30</item>
</items>
<items>
<item>100</item>
<item>700</item>
</items>
<items>
<item>150</item>
</items>
</output>
P.S. No, a template cannot be a child of another template.

XSLT for each list value for specified id

I try list value in deep level parameter, but only specified parameter.
I do for each test id for 706 number, when true I do for each and list value name. I need too semicolon between values but not at the beginning and at the end.
XML:
<parameters>
<section id="27" name="Parametry produktu"/>
<parameter id="23" name="kolor">
<value id="42" name="jasny róż"/>
</parameter>
<parameter id="25" name="skład">
<value id="43" name="97% bawełna, 3% poliamid"/>
</parameter>
<parameter id="706" name="rozmiar (ukryć)"">
<value id="717" name="46"/>
<value id="718" name="47"/>
<value id="719" name="48"/>
</parameter>
<parameter id="142" name="płeć (ukryć)">
<value id="746" name="ona"/>
</parameter>
</parameters>
XSLT:
<sizes3>
<xsl:for-each select="parameters">
<xsl:if test="parameter/#id = 706">
<xsl:for-each select="parameter">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="value/#name" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('; ', value/#name)" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</sizes3>
Result is:
<sizes3>jasny róż; 97% bawełna, 3% poliamid;46; ona;</sizes3>
But i need:
<sizes3>46;47;48</sizes3>
Here's one way:
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:template match="/parameters">
<!-- other stuff ? -->
<sizes3>
<xsl:for-each select="parameter[#id=706]/value">
<xsl:value-of select="#name" />
<xsl:if test="position()!=last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
</sizes3>
<!-- more stuff ? -->
</xsl:template>
</xsl:stylesheet>
Simpler, shorter, no <xsl:for-each>, no XSLT conditional operators:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<size3><xsl:apply-templates/></size3>
</xsl:template>
<xsl:template match="parameter[#id=706]/value">
<xsl:apply-templates select="#name"/>
</xsl:template>
<xsl:template match="parameter[#id=706]/value[position() > 1]" priority="2">
<xsl:text>;</xsl:text><xsl:apply-templates select="#name"/>
</xsl:template>
</xsl:stylesheet>

Output Context Node (full path) in XSLT 1.0?

For debugging purposes it would be handy to output the full path of the context node from within a template, is there unabbreviated xpath or function to report this ?
Example Template:
<xsl:template match="#first">
<tr>
<td>
<xsl:value-of select="??WHAT TO PUT IN HERE??"/>
</td>
</tr>
</xsl:template>
Example (Abridged) input document:
<people>
<person>
<name first="alan">
...
The output from the template would be something like:
people / person / name / #first
Or something similar.
This transformation produces an XPath expression for the wanted node:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="vNode" select=
"/*/*[2]/*/#first"/>
<xsl:apply-templates select="$vNode" mode="path"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:variable name="vnumFollSiblings" select=
"count(following-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings or $vnumFollSiblings">
<xsl:value-of select=
"concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="#*" mode="path">
<xsl:apply-templates select="ancestor::*" mode="path"/>
<xsl:value-of select="concat('/#', name())"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<people>
<person>
<name first="betty" last="jones"/>
</person>
<person>
<name first="alan" last="smith"/>
</person>
</people>
the wanted, correct result is produced:
/people/person[2]/name/#first
Here's a stylesheet (of dubious value) that prints the path to every element and attribute in a document:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:param name="pathToHere" select="''" />
<xsl:variable name="precSiblings"
select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:variable name="follSiblings"
select="count(following-sibling::*[name()=name(current())])" />
<xsl:variable name="fullPath"
select="concat($pathToHere, '/', name(),
substring(concat('[', $precSiblings + 1, ']'),
1 div ($follSiblings or $precSiblings)))" />
<xsl:value-of select="concat($fullPath, '
')" />
<xsl:apply-templates select="#*|*">
<xsl:with-param name="pathToHere" select="$fullPath" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="#*">
<xsl:param name="pathToHere" select="''" />
<xsl:value-of select="concat($pathToHere, '/#', name(), '
')" />
</xsl:template>
</xsl:stylesheet>
When applied to this input:
<people>
<person>
<name first="betty" last="jones" />
</person>
<person>
<name first="alan" last="smith" />
</person>
<singleElement />
</people>
Produces:
/people
/people/person[1]
/people/person[1]/name
/people/person[1]/name/#first
/people/person[1]/name/#last
/people/person[2]
/people/person[2]/name
/people/person[2]/name/#first
/people/person[2]/name/#last
/people/singleElement

Resources