XSL stylesheet not preserving the encoded values - java

I have an XML file that i am trying to read & write witin my java program.I am using SAXON 2.0 for this exercise. Below is a sample of the XML file that i have
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<actionOverrides>
<actionName>Accept</actionName>
<type>Default</type>
</actionOverrides>
<fields>
<fullName>Dev__c</fullName>
<externalId>false</externalId>
<formula>Sprint__r.Dev_Lead__r.FirstName + &apos; &apos; + Sprint__r.Dev_Lead__r.LastName</formula>
<formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
<label>Dev Lead</label>
<required>false</required>
<trackHistory>false</trackHistory>
<trackTrending>false</trackTrending>
<type>Text</type>
<unique>false</unique>
</fields>
<fields>
<fullName>Manager__c</fullName>
<externalId>false</externalId>
<formula>Sprint__r.Dev_Manager__r.FirstName +&apos; &apos;+
Sprint__r.Dev_Manager__r.LastName</formula>
<formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
<label>Dev Manager</label>
<required>false</required>
<trackHistory>false</trackHistory>
<trackTrending>false</trackTrending>
<type>Text</type>
<unique>false</unique>
</fields>
</CustomObject>
What happen is that when i run my data.xsl stylsheet with below code, all of the encoding is replaced with the actual value. In this case :
&apos; &apos;
is replace with ' '. What i want is to preserve the encoded values in the file. How can i make that happen ?. Also, the first line is replaced with . I want to preserve the xmlns value also i the file.
data.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://soap.sforce.com/2006/04/metadata"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="#* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Conceptually, an XSLT processor operates on trees of nodes. The input tree of nodes is constructed by an XML parser. It's the job of an XML parser to extract the information content of the XML, and to discard irrelevant distinctions that have no bearing on the information content - such as whitespace between the attributes in a start tag, the difference between single and double quotes around attribute values, the order of attributes, and the distinction between " and ". The XSLT processor doesn't even know how the XML was written, so it can't preserve such details.
As for the namespaces, when you do <xsl:element name="{local-name(.)}"> you are explicitly copying the local name but not the namespace. If you want to copy both, use <xsl:copy>, or <xsl:element name="{local-name(.)}" namespace="{namespace-url(.)">.

Related

How to update a range of an XMl file tag?

My problem is I don't know How to update a XML file. In the following XML file I want to include some tags inside another tag which are already exist in the file.
**My XML file is as following: **
<?xml version="1.0" encoding="UTF-8"?>
<root>
<PayrunDetails>
<PayrunNumber>000777</PayrunNumber>
</PayrunDetails>
<PayLocation>
<LocationCode>ACT</LocationCode>
<LocationDescription>ACT</LocationDescription>
<CompanyDetails>
<CName>APPLE Limited</CName>
<Payslip>
<StaffNumber>12345</StaffNumber>
<PayDetails>
<AmountGross>9999</AmountGross>
<ComponentDetails>
<ComponentType>SALARY</ComponentType>
<Amount>1999</Amount>
<YTDAmount>10616</YTDAmount>
</ComponentDetails>
<ComponentDetails>
<ComponentType>SALARY</ComponentType>
<Amount>7305</Amount>
<YTDAmount>76703</YTDAmount>
</ComponentDetails>
</PayDetails>
</Payslip>
</CompanyDetails>
</PayLocation>
</root>
My desired output file is as following:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<PayrunDetails>
<PayrunNumber>000777</PayrunNumber>
</PayrunDetails>
<PayLocation>
<LocationCode>ACT</LocationCode>
<LocationDescription>ACT</LocationDescription>
<CompanyDetails>
<CName>APPLE Limited</CName>
<Payslip>
<StaffNumber>12345</StaffNumber>
<PayDetails>
<AmountGross>9999</AmountGross>
<ComponentDetails>
<ComponentType ID="SALARY">
<Amount>1999</Amount>
<YTDAmount>10616</YTDAmount>
</ComponentType>
</ComponentDetails>
<ComponentDetails>
<ComponentType ID="TAX">
<Amount>7305</Amount>
<YTDAmount>76703</YTDAmount>
</ComponentType>
</ComponentDetails>
</PayDetails>
</Payslip>
</CompanyDetails>
</PayLocation>
</root>
In the above desired file you will find that ComponentType tag has included the rest of the tags exist inside the ComponentDetails tag.
For the above said problem I want to use XSLT but I don't know what code should I write to get the solution.
I'm fairly new to XSLT so please excuse the potential novice question. Any guidance would be appreciated here.
Thanks in advance.
First read up on the identity transform in XSLT, which involves this template
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
(If you could use XSLT 3.0, you could just write <xsl:mode on-no-match="shallow-copy"/> instead)
This will copy across all your nodes and attributes as-is, which in your case gets you almost there.
There are a number of ways you could the transform of the nodes you want. One way is to match the ComponentDetails tag, to create a new ComponentType in the output, along with code to select the other child nodes.
<xsl:template match="ComponentDetails">
<xsl:copy>
<ComponentType ID="{ComponentType}">
<xsl:apply-templates />
</ComponentType>
</xsl:copy>
</xsl:template>
This makes use of Attribute Value Templates to create the ID attribute.
Note that <xsl:apply-templates /> is short-hand for <xsl:apply-templates select="node()" /> and so this will still select the existing ComponentType element in the input document, which will then be matched by the identity template. To stop ComponentType being output twice, you need to add a template to match and ignore it.
<xsl:template match="ComponentType" />
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" html-version="5"/>
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ComponentDetails">
<xsl:copy>
<ComponentType ID="{ComponentType}">
<xsl:apply-templates />
</ComponentType>
</xsl:copy>
</xsl:template>
<xsl:template match="ComponentType" />
</xsl:stylesheet>

add namespace and prefix to xml

I am using camel to route messages to a Webservice. The Messages are like XML but without namespaces/prefixes. The problem now is that the Webservice expects the XML but with the appropriate namespaces for each element. So as an example:
<a>
<b>value_b</b>
<c>value_c</c>
</a>
is what im getting in, but what needs to be sent out should look like this
<a xmlns:n1="http://yadda-ns1.com" xmlns:n2="http://yadda-ns2.com">
<ns1:b>value_b</ns1:b>
<ns2:c>value_c</ns2:c>
</a>
if it was the same namespace on all elements i would have just used an xslt to add it. But its mostly 2 or 3 different namespaces.
Now is it possible to add the namespaces in my camel route? I had the idea to use jaxb to marshal from the "incomplete" XML to the "complete" one (with XML), would this work? I was trying this but did not succeed yet.
Or does someone have a different idea? What i also have in my project is the XSDs and JAXB Annotated Classes so these can also be used and the messages are identical apart from the missing namespace.
Best Regards
Thomas
You could transform the XML with a stylesheet such as the one below to modify the elements to be bound to the appropriate namespaces:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ns1="http:yadayada-ns1.com"
xmlns:ns2="http:yadayada-ns2.com">
<xsl:output indent="yes"/>
<!-- identity template that copies content(unless more specific templates match) -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Make the "a" elements in the ns1 namespace -->
<xsl:template match="a">
<xsl:element name="ns1:{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<!-- Make the "b" and "c" elements in the ns2 namespace -->
<xsl:template match="b|c">
<xsl:element name="ns2:{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

XSLT dont add XMLNS to elements

I have a simple HTML fragment similar to this:
link
I need to transform it to
<abc:href var="123">link</abc:href>
I do it with XSLT, so I had to add the namespace in xsl:stylesheet
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://abc.ru">
It works almost fine, unfortunately the XSLT transform keeps on adding a XMLNS to the output, like here:
<abc:href var="123" xmlns:abc="http://abc.ru">link</abc:href>
I don't need the xmlns definition, can I remove it?
Although it really goes against the grain, and I advise strongly against it, if you need to produce this malformed XML, then you can use an instruction like...
<xsl:value-of disable-output-escaping="yes" select="
concat('<abc:href var="',$href,'">',$link,'</abc:href>')
"/>
... where $href and $link are place-markers for the appropriate expression.
Update
In response to the OP's comment, one could use a template like this...
<xsl:template match="a">
<xsl:value-of disable-output-escaping="yes" select="
concat('<abc:href var="',#href,'">',.,'</abc:href>')
"/>
</xsl:template>
This ugly solution should be used only as a last resort. A much better solution would be to use XSLT to produce your WHOLE document, not just an invalid fragment of it. This way you document would be well formed and you could bring to bear the full power and simplicity of XSLT.
It works almost fine, unfortunately the XSLT transform keeps on adding
a XMLNS to the output, like here:
<abc:href var="123" xmlns:abc="http://abc.ru">link</abc:href>
I don't need the xmlns definition, can I remove it?
The wanted removal of the namespace declaration would produce a (namespace-)non-well-formed XML document and for this reason the XSLT processor adds the namespace declaration -- as required by the W3C XSLT specifications.
You can cause these namespace declarations to "disappear" by placing the namespace declaration on a common ancestor (such as the top element of the generated XML document).
Here is a complete example:
<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="/*">
<top xmlns:abc="http://abc.ru">
<xsl:apply-templates/>
</top>
</xsl:template>
<xsl:template match="a[#href]">
<xsl:element name="abc:href" namespace="http://abc.ru">
<xsl:attribute name="var">
<xsl:apply-templates/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following document:
<html>
link1
link2
link3
link4
</html>
the wanted, correct result is produced:
<top xmlns:abc="http://abc.ru">
<abc:href var="link1"/>
<abc:href var="link2"/>
<abc:href var="link3"/>
<abc:href var="link4"/>
</top>
This is sad, but I really need an invalid xml
XSLT is designed to prevent you producing bad XML. If you want to produce bad XML, don't use XSLT.
Try it with exclude-result-prefixes, like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://abc.ru"
exclude-result-prefixes="abc">
<xsl:template match="/">
<xsl:apply-templates select="#* | node()"/>
</xsl:template>
<xsl:template match="a">
<href var="{#href}"><xsl:value-of select="."/></href>
</xsl:template>
</xsl:stylesheet>

Create xmlns attribute in the XML using XSLT Transformation

I am trying to add the xmlns attribute to the resulting XML with a value passed by parameter during XSLT transformation using JDK Transformer (Oracle XML v2 Parser or JAXP) but it always defaults to http://www.w3.org/2000/xmlns/
My source XML
<test/>
My XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://example.com">
<xsl:param name="myNameSpace" select="'http://neilghosh.com'"/>
<xsl:template match="/">
<process>
<xsl:attribute name="xmlns:neil">
<xsl:value-of select="$myNameSpace"/>
</xsl:attribute>
</process>
</xsl:template>
</xsl:stylesheet>
My Result
<?xml version="1.0"?>
<process xmlns="http://www.w3.org/2000/xmlns/" xmlns:neil="neilghosh.com">
</process>
My Desired Result
<?xml version="1.0"?>
<process xmlns="http://example.com" xmlns:neil="neilghosh.com">
</process>
Firstly, in the XSLT data model, you don't want to create an attribute node, you want to create a namespace node.
Namespace nodes are usually created automatically: if you create an element or attribute in a particular namespace, the requisite namespace node (and hence, when serialized, the namespace declaration) are added automatically by the processor.
If you want to create a namespace node that isn't necessary (because it's not used in the name of any element or attribute) then in XSLT 2.0 you can use xsl:namespace. If you're stuck with XSLT 1.0 then there's a workaround, that involves creating an element in the relevant namespace and then copying its namespace node:
<xsl:variable name="ns">
<xsl:element name="neil:dummy" namespace="{$param}"/>
</xsl:variable>
<process>
<xsl:copy-of select="$ns/*/namespace::neil"/>
</process>
Michael Kay provided you with the correct answer, but based on your comments, you aren't sure how to use it in your transformation.
Here is a complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pNamespace" select="'neilghosh.com'"/>
<xsl:variable name="vDummy">
<xsl:element name="neil:x" namespace="{$pNamespace}"/>
</xsl:variable>
<xsl:template match="/*">
<xsl:element name="process" namespace="http://example.com">
<xsl:copy-of select="namespace::*"/>
<xsl:copy-of select="ext:node-set($vDummy)/*/namespace::*[.=$pNamespace]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<test/>
the wanted, correct result is produced:
<process xmlns="http://example.com" xmlns:neil="neilghosh.com" />
Namespace declarations in XML are not attributes even though they look like attributes. In XSLT 2.0 you can use <xsl:namespace name="neil" select="$myNameSpace" /> to add a namespace declaration to the result tree dynamically but that feature is not available in XSLT 1.0.
Don't try to create "xmlns" attributes yourself. Create the namespaces in the XSLT and they will be done automatically.
This XSLT works (tested with Saxon 9.4):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:neil="neilghosh.com"
xpath-default-namespace="http://example.com"
xmlns="http://example.com" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="myDynamicNamespace" select="'http://neilghosh.com'"/>
<xsl:template match="/">
<xsl:element name="process">
<xsl:namespace name="neil" select="$myDynamicNamespace"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And gives the following output:
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://example.com" xmlns:neil="http://neilghosh.com"/>
Finally got an workaround which worked with my XSLT Processor (Oracle XML V2 Parser)
I had to transform it to a DOM Document and then persist that DOM to filesystem instead of outputting directly to StreamResult
I used DOMResult in the transform method
Following XSLT fragment worked but there was an extra xmlns:xmlns="http://www.w3.org/2000/xmlns/" which was probably absorbed by Document and did not appear in the final output when I persisted to file system.
<process>
<xsl:attribute name="xmlns">
<xsl:value-of select="'http://example.com'"/>
</xsl:attribute>
<process>
I know this is not the best way to do but given the parse constraint this is the only choice I have now.

Populate XML template-file from XPath Expressions?

What would be the best way to populate (or generate) an XML template-file from a mapping of XPath expressions?
The requirements are that we will need to start with a template (since this might contain information not otherwise captured in the XPath expressions).
For example, a starting template might be:
<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
<article xmlns:ns1='http://predic8.com/material/1/'>
<name>?XXX?</name>
<description>?XXX?</description>
<price xmlns:ns1='http://predic8.com/common/1/'>
<amount>?999.99?</amount>
<currency xmlns:ns1='http://predic8.com/common/1/'>???</currency>
</price>
<id xmlns:ns1='http://predic8.com/material/1/'>???</id>
</article>
</ns1:create>
</s11:Body>
</s11:Envelope>
Then we are supplied, something like:
expression: /create/article[1]/id => 1
expression: /create/article[1]/description => bar
expression: /create/article[1]/name[1] => foo
expression: /create/article[1]/price[1]/amount => 00.00
expression: /create/article[1]/price[1]/currency => USD
expression: /create/article[2]/id => 2
expression: /create/article[2]/description => some name
expression: /create/article[2]/name[1] => some description
expression: /create/article[2]/price[1]/amount => 00.01
expression: /create/article[2]/price[1]/currency => USD
We should then generate:
<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
<article xmlns:ns1='http://predic8.com/material/1/'>
<name xmlns:ns1='http://predic8.com/material/1/'>foo</name>
<description>bar</description>
<price xmlns:ns1='http://predic8.com/common/1/'>
<amount>00.00</amount>
<currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency>
</price>
<id xmlns:ns1='http://predic8.com/material/1/'>1</id>
</article>
<article xmlns:ns1='http://predic8.com/material/2/'>
<name>some name</name>
<description>some description</description>
<price xmlns:ns1='http://predic8.com/common/2/'>
<amount>00.01</amount>
<currency xmlns:ns1='http://predic8.com/common/2/'>USD</currency>
</price>
<id xmlns:ns1='http://predic8.com/material/2/'>2</id>
</article>
</ns1:create>
I am implemented in Java, although I would prefer an XSLT-based solution if one is possible.
PS: This question is the reverse of another question I recently asked.
This transformation creates from the "expressions" an XML document that has the structure of the wanted result -- it remains to transform this result into the final result:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vPop" as="element()*">
<item path="/create/article[1]/id">1</item>
<item path="/create/article[1]/description">bar</item>
<item path="/create/article[1]/name[1]">foo</item>
<item path="/create/article[1]/price[1]/amount">00.00</item>
<item path="/create/article[1]/price[1]/currency">USD</item>
<item path="/create/article[1]/price[2]/amount">11.11</item>
<item path="/create/article[1]/price[2]/currency">AUD</item>
<item path="/create/article[2]/id">2</item>
<item path="/create/article[2]/description">some name</item>
<item path="/create/article[2]/name[1]">some description</item>
<item path="/create/article[2]/price[1]/amount">00.01</item>
<item path="/create/article[2]/price[1]/currency">USD</item>
</xsl:variable>
<xsl:template match="/">
<xsl:sequence select="my:subTree($vPop/#path/concat(.,'/',string(..)))"/>
</xsl:template>
<xsl:function name="my:subTree" as="node()*">
<xsl:param name="pPaths" as="xs:string*"/>
<xsl:for-each-group select="$pPaths"
group-adjacent=
"substring-before(substring-after(concat(., '/'), '/'), '/')">
<xsl:if test="current-grouping-key()">
<xsl:choose>
<xsl:when test=
"substring-after(current-group()[1], current-grouping-key())">
<xsl:element name=
"{substring-before(concat(current-grouping-key(), '['), '[')}">
<xsl:sequence select=
"my:subTree(for $s in current-group()
return
concat('/',substring-after(substring($s, 2),'/'))
)
"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="current-grouping-key()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each-group>
</xsl:function>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the result is:
<create>
<article>
<id>1</id>
<description>bar</description>
<name>foo</name>
<price>
<amount>00.00</amount>
<currency>USD</currency>
</price>
<price>
<amount>11.11</amount>
<currency>AUD</currency>
</price>
</article>
<article>
<id>2</id>
<description>some name</description>
<name>some description</name>
<price>
<amount>00.01</amount>
<currency>USD</currency>
</price>
</article>
</create>
Note:
You need to transform the "expressions" you are given into the format used in this transformation -- this is easy and straightforward.
In the final transformation you need to copy every node "as-is" (using the identity rule), with the exception that the top node should be generated in the "http://predic8.com/wsdl/material/ArticleService/1/" namespace. Note that the other namespaces present in the "template" are not used and can be safely ommitted.
This solution requires you to re-organise your XPATH input information slightly, and to allow a 2-step transformation. The first transformation will write the stylesheet, which will be executed in the second transformation - Thus the client is required to do two invocations of the XSLT engine. Let us know if this is a problem.
Step One
Please re-organise your XPATH information into an XML document like so. It should not be difficult to do, and even an XSLT script could be written to do the job.
<paths>
<rule>
<match>article[1]/id[1]</match>
<namespaces>
<namespace prefix="ns1">http://predic8.com/wsdl/material/ArticleService/1/</namespace>
<!-- The namespace node declares a namespace that is used in the match expression.
There can be many of these. It is not required to define the s11: namespace,
nor the ns1 namespace. -->
</namespaces>
<replacement>1</replacement>
</rule>
<rule>
<match>article[1]/description[1]</match>
<namespaces/>
<replacement>bar</replacement>
</rule>
... etc ...
</paths>
Solution constraints
In the above rules document we are constrained so that:
The match is implicitly prefixed 'expression: /create/'. Don't put that explicitly.
All matches must begin like article[n] where n is some ordinal number.
We can't have zero rules.
Any prefixes that you use in the match, other than s11="http://schemas.xmlsoap.org/soap/envelope/" and ns1="http://predic8.com/wsdl/material/ArticleService/1/". (Note: I don't think it is valid for namespaces to end in '/' - but not sure about that), are defined in the namespaces node.
The above is the input document to the step one transformation. Apply this document to this style-sheet ...
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:step2="http://www.w3.org/1999/XSL/Transform-step2"
xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://predic8.com/wsdl/material/ArticleService/1/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes='xsl'>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:namespace-alias stylesheet-prefix="step2" result-prefix="xsl"/>
<xsl:template match="/">
<step2:stylesheet version="2.0">
<step2:output method="xml" indent="yes" encoding="UTF-8" />
<step2:variable name="replicated-template" as="element()*">
<step2:apply-templates select="/" mode="replication" />
</step2:variable>
<step2:template match="#*|node()" mode="replication">
<step2:copy>
<step2:apply-templates select="#*|node()" mode="replication" />
</step2:copy>
</step2:template>
<step2:template match="/s11:Envelope/s11:Body/ns1:create/article" mode="replication">
<step2:variable name="replicant" select="." />
<step2:for-each select="for $i in 1 to
{max(for $m in /paths/rule/match return
xs:integer(substring-before(substring-after($m,'article['),']')))}
return $i">
<step2:for-each select="$replicant">
<step2:copy>
<step2:apply-templates select="#*|node()" mode="replication" />
</step2:copy>
</step2:for-each>
</step2:for-each>
</step2:template>
<step2:template match="#*|node()">
<step2:copy>
<step2:apply-templates select="#*|node()"/>
</step2:copy>
</step2:template>
<step2:template match="/">
<step2:apply-templates select="$replicated-template" />
</step2:template>
<xsl:apply-templates select="paths/rule" />
</step2:stylesheet>
</xsl:template>
<xsl:template match="rule">
<step2:template match="s11:Envelope/s11:Body/ns1:create/{match}">
<xsl:for-each select="namespaces/namespace">
<xsl:namespace name="{#prefix}" select="." />
</xsl:for-each>
<step2:copy>
<step2:apply-templates select="#*"/>
<step2:value-of select="'{replacement}'"/>
<step2:apply-templates select="*"/>
</step2:copy>
</step2:template>
</xsl:template>
</xsl:stylesheet>
Step Two
Apply your soap envelope file, as an input document, to the style-sheet which was output from step one. The result is the original soap document, altered as required. This is a sample of a step two style-sheet, with just the first rule (/create/article[1]/id => 1) being considered for the sake of simplicity of illustration.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template xmlns:ns1="http://predic8.com/wsdl/material/ArticleService/1/"
match="/s11:Envelope/s11:Body/ns1:create[1]/article[1]/id[1]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:value-of select="'1'"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
More solution constraints
The template document must contain at least one /s11:Envelope/s11:Body/ns1:create/article . Only the article node is replicated (deeply) as required by rules. Other than than it can be any structure.
The template document cannot contain nested levels of the s11:Envelope/s11:Body/ns1:create node.
Explanation
You will notice that your XPATH expressions are not far removed from a match condition of template. Therefore it is not too difficult to write a stylesheet which re-expresses your XPATH and replacement values as template rules. When writing a style-sheet writing style-sheet the xsl:namespace-alias enables us to disambiguate "xsl:" as an instruction and "xsl:" as intended output. When XSLT 3.0 comes along, we are quiet likely to be able to reduce this algorithm into one step, as it will allow dynamic XPATH evaluation, which is really the nub of your problem. But for the moment we must be content with a 2-step process.
The second style-sheet is a two-phase transformation. The first stage replicates the template from the article level, as many times as needed by the rules. The second phase parses this replicated template, and applies the dynamic rules substituting text values as indicated by the XPATHs.
UPDATE
My original post was wrong. Thanks to Dimitre for pointing out the error. Please find updated solution above.
After-thought
If a two-step solultion is too complicated, and you are running on a wintel platform, you may consider purchasing the commercial version of Saxon. I believe that the commercial version has a dynamic XPATH evaluation function. I can't give you such a solution because I don't have the commercial version. I imagine a solution using an evaluate() function would be a lot simpler. XSLT is just a hobby for me. But if you are using XSLT for business purposes, the price is quiet reasonable.

Categories