Reputation: 23
I'm new to XML and so far I've managed to muddle along reading a few things and looking at answers to other peoples question but I'm stuck.
In the sample XML below and I need to transform <value>some interesting information</value>
to be <value web_label="3" mobile_label="3">some interesting information</value>
.
Value is variable so the correct value element is the sibling of <name>Special Description</name>
which is constant.
<job>
<job_type>job</job_type>
<third_party_id>AA123456-AA</third_party_id>
<custom_fields>
<custom_field>
<name>Date Promised</name>
<permission>read_only</permission>
<value>2020-01-01T00:00:00Z</value>
</custom_field>
<custom_field>
<name>Work Order</name>
<permission>read_only</permission>
<value>L1</value>
</custom_field>
<custom_field>
<name>Special Description</name>
<permission>read_only</permission>
<value>some interesting information</value>
</custom_field>
</custom_fields>
</job>
In my trial and error I've managed to set the attributes on all the value elements using the below.
I thought I could then replace match="/job/custom_fields/custom_field/value
with match="/job/custom_fields/custom_field[name=Special Description]/value"
to select the specific element but that errors.
Is there an xpath query I can use to achieve this/what is the best way to do this in my transform?
<xsl:template match="/job/custom_fields/custom_field/value">
<xsl:copy>
<xsl:attribute name="web_label">3</xsl:attribute>
<xsl:attribute name="mobile_label">3</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
The XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">
<xsl:output method="xml"
indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/job/custom_fields/custom_field[name=Special Description]/value">
<xsl:copy>
<xsl:attribute name="web_label">3</xsl:attribute>
<xsl:attribute name="mobile_label">3</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="custom_fields">
<xsl:copy>
<xsl:attribute name="type">array</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="third_party_id">
<xsl:copy>
<xsl:attribute name="web_label">2</xsl:attribute>
<xsl:attribute name="mobile_label">2</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I appreciate the help.
Upvotes: 1
Views: 185
Reputation: 23
Thanks to @michael.hor257 for suggesting
<xsl:template match="custom_field[name='Special Description']/value"> <value web_label="3" mobile_label="3"> <xsl:value-of select="."/> </value> </xsl:template>
For some reason when used in my existing XSLT I ended up with xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/
in the transformed XML.
<job>
<job_type>job</job_type>
<third_party_id web_label="2" mobile_label="2">AA123456-AA</third_party_id>
<custom_fields type="array">
<custom_field>
<name>Date Promised</name>
<permission>read_only</permission>
<value>2020-01-01T00:00:00Z</value>
</custom_field>
<custom_field>
<name>Work Order</name>
<permission>read_only</permission>
<value>L1</value>
</custom_field>
<custom_field>
<name>Special Description</name>
<permission>read_only</permission>
<value xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
web_label="3"
mobile_label="3">some interesting information</value>
</custom_field>
</custom_fields>
</job>
Using @michael.hor257's xpath query with xsl:copy and xsl:attribute from my original XSLT however did the trick, so I ended up with:
<xsl:template match="custom_field[name='Special Description']/value">
<xsl:copy>
<xsl:attribute name="web_label">3</xsl:attribute>
<xsl:attribute name="mobile_label">3</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
Resulting complete XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">
<xsl:output method="xml"
indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="custom_field[name='Special Description']/value">
<xsl:copy>
<xsl:attribute name="web_label">3</xsl:attribute>
<xsl:attribute name="mobile_label">3</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="custom_fields">
<xsl:copy>
<xsl:attribute name="type">array</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="third_party_id">
<xsl:copy>
<xsl:attribute name="web_label">2</xsl:attribute>
<xsl:attribute name="mobile_label">2</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And the transformed XML looks like this:
<job>
<job_type>job</job_type>
<third_party_id web_label="2" mobile_label="2">AA123456-AA</third_party_id>
<custom_fields type="array">
<custom_field>
<name>Date Promised</name>
<permission>read_only</permission>
<value>2020-01-01T00:00:00Z</value>
</custom_field>
<custom_field>
<name>Work Order</name>
<permission>read_only</permission>
<value>L1</value>
</custom_field>
<custom_field>
<name>Special Description</name>
<permission>read_only</permission>
<value web_label="3" mobile_label="3">some interesting information</value>
</custom_field>
</custom_fields>
</job>
Upvotes: 1
Reputation: 117100
If you only want to modify a specific value
element, match it using a predicate:
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="custom_field[name='Special Description']/value">
<value web_label="3" mobile_label="3">
<xsl:value-of select="."/>
</value>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1