Such an example
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a id='a1' name='a1 is a Chinese pig'/>
<b text='b1'/>
<d test='test0' location='L0' text='c0'/>
<a id='a2' name='a2 is a Japanese pig'/>
<b text='b2'/>
<c test='test1' location='L1' text='c1 is a red pig'/>
<c test='test2' location='L2' text='c2 is a green pig'/>
<a id='a3' name='a3 is a American dog'/>
<b text='b3'/>
<c test='test3' location='L3' text='c3 is a lovely dog'/>
<c test='test4' location='L4' text='c4 is a ugly dog'/>
<c test='test5' location='L5' text='c5 is a smart dog'/>
<a id='a4' name='a4 is a Japanese bird'/>
<b text='b4'/>
<c test='test6' location='L6' text='c6 is a lovely bird'/>
<c test='test7' location='L7' text='c7 is a ugly bird'/>
<c test='test8' location='L8' text='c8 is a smart bird'/>
<a id='a5' name='a5 is a American pig'/>
<b text='b2'/>
<c test='test10' location='L10' text='c10 is a red pig'/>
<c test='test11' location='L11' text='c11 is a green pig'/>
<a id='a6' name='a6 is a Chinese dog'/>
<b text='b3'/>
<c test='test12' location='L12' text='c12 is a lovely dog'/>
<c test='test14' location='L14' text='c14 is a ugly dog'/>
<c test='test15' location='L15' text='c15 is a smart dog'/>
<a id='a7' name='a7 is a Chinese bird'/>
<b text='b4'/>
<c test='test16' location='L16' text='c16 is a lovely bird'/>
<c test='test17' location='L17' text='c17 is a ugly bird'/>
<c test='test18' location='L18' text='c18 is a smart bird'/>
</root>
I apply a xsl1.0 like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:key name="lookup" match="c" use="generate-id(preceding-sibling::a[1])" />
<xsl:template match="/root">
<xsl:apply-templates select="a[key('lookup', generate-id())]" />
</xsl:template>
<xsl:template match="a">dog:<xsl:value-of select="concat(#name[contains(.,'dog')], ':
', '
')" />
<xsl:apply-templates select="key('lookup', generate-id())" />
</xsl:template>
<xsl:template match="c">
<xsl:apply-templates select="#*" />
<xsl:value-of select="'
'" />
</xsl:template>
<xsl:template match="c/#*">
<xsl:value-of select="concat(local-name(), ':', ., ':
')" />
</xsl:template>
</xsl:stylesheet>
I want a output like this,the rule is
if there is one or more following c elements between a element and the next a , then we ouput these c elements
then according to the #name, check that value contain some string like pig,dog or bird ,these are key words, not just a hard code work here. we could group these information ,because there are more pigs,birds and dogs.not matter they are chinese or japanese or else,we group according to the dogs,bird ,pig these category we know before.
The output I want is like this
case1:the following are all pigs:
a2 is a Japanese pig
test:test1
location:L1
text:c1 is a red pig
test:test2
location:L2
text:c2 is a green pig
a5 is a American pig
test:test10
location:L10
text:c10 is a red pig
test:test11
location:L11
text:c11 is a green pig
case2:the following are all dogs:
a3 is a American dog
test:test3
location:L3
text:c3 is a lovely dog
test:test4
location:L4
text:c4 is a ugly dog
test:test5
location:L5
text:c5 is a smart dog
a6 is a Chinese dog
test:test12
location:L12
text:c12 is a lovely dog
test:test14
location:L14
text:c14 is a ugly dog
test:test15
location:L15
text:c15 is a smart dog
case3:the following are all birds:
a4 is a Japanese bird
test:test6
location:L6
text:c6 is a lovely bird
test:test7
location:L7
text:c7 is a ugly bird
test:test8
location:L8
text:c8 is a smart bird
a7 is a Chinese bird
test:test16
location:L16
text:c16 is a lovely bird
test:test17
location:L17
text:c17 is a ugly bird
test:test18
location:L18
text:c18 is a smart bird
The question is how to correct my xsl1.0? Another is this more easy to realize in xsl2.0?
with this xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/root">
<p>the followings are birds:</p>
<xsl:apply-templates select="a[contains(#name, 'bird')][key('bird_lookup', generate-id())]"/>
<p>the followings are pigs:</p>
<xsl:apply-templates select="a[contains(#name, 'pig')][key('pig_lookup', generate-id())]"/>
<p>the followings are dogs:</p>
<xsl:apply-templates select="a[contains(#name, 'dog')][key('dog_lookup', generate-id())]"/>
</xsl:template>
<xsl:key name="bird_lookup" match="c" use="generate-id(preceding-sibling::a[1][contains(#name, 'bird')])"/>
<xsl:template name="bird" match="a[contains(#name, 'bird')]">
<xsl:value-of select="'
'"/>
<xsl:value-of select="#name"/>
<xsl:value-of select="'
'"/>
<xsl:apply-templates select="key('bird_lookup', generate-id())" />
<xsl:apply-templates select="c/#*"/>
</xsl:template>
<xsl:key name="pig_lookup" match="c" use="generate-id(preceding-sibling::a[1][contains(#name, 'pig')])"/>
<xsl:template name="pig" match="a[contains(#name, 'pig')]">
<xsl:value-of select="'
'"/>
<xsl:value-of select="#name"/>
<xsl:value-of select="'
'"/>
<xsl:apply-templates select="key('pig_lookup', generate-id())" />
<xsl:apply-templates select="c/#*"/>
</xsl:template>
<xsl:key name="dog_lookup" match="c" use="generate-id(preceding-sibling::a[1][contains(#name, 'dog')])"/>
<xsl:template name="dog" match="a[contains(#name, 'dog')]">
<xsl:value-of select="'
'"/>
<xsl:value-of select="#name"/>
<xsl:value-of select="'
'"/>
<xsl:apply-templates select="key('dog_lookup', generate-id())" />
<xsl:apply-templates select="c/#*"/>
</xsl:template>
<xsl:template match="c">
<xsl:apply-templates select="#*"/>
<xsl:value-of select="'
'"/>
</xsl:template>
<xsl:template match="c/#*">
<xsl:value-of select="concat(local-name(), ':', ., '
')"/>
</xsl:template>
</xsl:stylesheet>
i can get
the followings are birds:
a4 is a Japanese bird
test:test6
location:L6
text:c6 is a lovely bird
test:test7
location:L7
text:c7 is a ugly bird
test:test8
location:L8
text:c8 is a smart bird
a7 is a Chinese bird
test:test16
location:L16
text:c16 is a lovely bird
test:test17
location:L17
text:c17 is a ugly bird
test:test18
location:L18
text:c18 is a smart bird
the followings are pigs:
a2 is a Japanese pig
test:test1
location:L1
text:c1 is a red pig
test:test2
location:L2
text:c2 is a green pig
a5 is a American pig
test:test10
location:L10
text:c10 is a red pig
test:test11
location:L11
text:c11 is a green pig
the followings are dogs:
a3 is a American dog
test:test3
location:L3
text:c3 is a lovely dog
test:test4
location:L4
text:c4 is a ugly dog
test:test5
location:L5
text:c5 is a smart dog
a6 is a Chinese dog
test:test12
location:L12
text:c12 is a lovely dog
test:test14
location:L14
text:c14 is a ugly dog
test:test15
location:L15
text:c15 is a smart dog
how to make this elegant?
Related
I have to pick text from translation.xml and update skeleton.xml's respective (where #xid =#id) nodes . in other words IF #id of translation.xml matches with #xid of skeleton.xml, the text content should be placed under skeleton.xml (+ if there is child element present the text content (inline element i, b, etc) also should go in respective parent element: Please see example below for more details:
Note: the inline element could be anything in skeleton.xml (its not restrict to b or i, so it should be generic)
skeleton.xml
<root>
<para a="b" b="c">
<text xid="1">This is first para <b xid="2" a="c" b="d">This is bold <i xid="3" b="d" c="e">This is italics</i> rest of bold</b> rest of para</text>
</para>
<para><text xid="4">This is second para</text></para>
<para><text xid="5">This is unchanged para</text></para>
<para><text xid="6">This is unchanged para</text></para>
</root>
translation.xml
<root>
<TU id="1">
<source>This is first para <g id="2" tagName="b">This is bold <g id="3" tagName="i">This is italics</g> rest of bold</g> rest of para</source>
<target>suum primum para <g id="2" tagName="b">Et hoc confidens, <g id="3" tagName="i">Hoc est, Te Deum</g> Reliqua autem audet,</g> reliqua autem verba haec</target>
</TU>
<TU id="4">
<source>This is second para</source>
<target>Hoc est secundum verba haec</target>
</TU>
</root>
UpdatedSkeleton.xml
<root>
<para a="b" b="c">
<text xid="1">suum primum para <b xid="2" a="c" b="d">Et hoc confidens, <i xid="3" b="d" c="e">Hoc est, Te Deum</i> Reliqua autem audet,</b> reliqua autem verba haec</text>
</para>
<para><text xid="4">Hoc est secundum verba haec</text></para>
<para><text xid="5">This is unchanged para</text></para>
<para><text xid="6">This is unchanged para</text></para>
</root>
I am trying with this code, but facing challenge to place text of inline content at the right place:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="translation" select="'file:/C:/Skeleton.xml'"></xsl:param>
<xsl:variable name="doc">
<xsl:copy-of select="doc($translation)"/>
</xsl:variable>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text">
<xsl:variable name="skelID" select="#xid"/>
<xsl:choose>
<xsl:when test="$doc//*[$skelID=#id]">
<xsl:apply-templates select="$doc//*[$skelID=#id]/target"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
You can copy the <text> node adding <xsl:copy> to your test block, and copy the xid attribute using <xsl:attribute>. For the unmodified nodes, you can use <xsl:copy-of> which copies the full tree, including attributes:
<xsl:when test="$doc//*[$skelID=#id]">
<xsl:copy>
<xsl:attribute name="xid">
<xsl:value-of select="$skelID"/>
</xsl:attribute>
<xsl:apply-templates select="$doc//*[$skelID=#id]/target"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
That will also copy the <target> element. You can remove it adding a template:
<xsl:template match="target">
<xsl:apply-templates/>
</xsl:template>
You also aren't replacing the <g> tag. I assume the #tagName attribute says what it's supposed to be transformed into. This should do it:
<xsl:template match="g">
<xsl:element name="{#tagName}">
<xsl:attribute name="xid">
<xsl:value-of select="#id"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
I might have missed something, but that will probably solve most of your problem.
I would like to be able to compare two nodes based on the values of their child nodes. Testing node equality with the = operator just compares the string values of the respective nodes. I would like to compare them based on values in their child nodes.
To be a bit more specific, I would like <a> and <b> (below) to be equal, because the values of #id are the same for <c> elements that have matching #type attributes also have matching #id attributes.
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
</b>
But these would be different:
<a>
<c type="type-one" id="5675"/>
</a>
<b>
<c type="type-one" id="2342"/>
</b>
The only solution I can begin to see involves a laborious comparison with a for-each statement, which I would like to avoid if possible.
If possible I would like to stick with XSLT 1.0. I am using xsltproc.
First of all, the relation called "equals" cannot have that name.
"Equals" means that the relation is an equivalence relation. By definition any equivalence relation ~ must be:
Reflexive: x ~ x .
Symmetric: if x ~ y then y ~ x
Transitive: if x ~ y and y ~ z then x ~ z .
Here is an example, showing that the proposed "equals" relation isn't transitive:
x is:
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
y is:
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-four" id="1234"/>
</b>
z is:
<b>
<c type="type-three" id="3333"/>
<c type="type-four" id="1234"/>
</b>
Now, we can see that x ~ y and y ~ z. However, clearly this doesn't hold: x ~ z
This said, I am calling the relation "matches" and it is relaxed and not "equals".
Here is a solution to the problem, with the above adjustment:
Do note that this cannot be expressed with a single XPath expression, because XPath 1.0 (used within an XSLT 1.0 transformation) doesn't have range variables.
<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="/*">
<xsl:call-template name="matches">
<xsl:with-param name="pElem1" select="a"/>
<xsl:with-param name="pElem2" select="b"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="matches">
<xsl:param name="pElem1" select="/.."/>
<xsl:param name="pElem2" select="/.."/>
<xsl:variable name="vMisMatch">
<xsl:for-each select="$pElem1/c[#type = $pElem2/c/#type]">
<xsl:if test=
"$pElem2/c[#type = current()/#type and not(#id = current()/#id)]">1</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="not(string($vMisMatch))"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
</b>
</t>
the wanted, correct result is produced:
true
When the same transformation is applied on this XML document:
<t>
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="9876"/>
</b>
</t>
again the correct result is produced:
false
Here is what I came up with. Given a toy data set like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>
<item key="x" value="123"/>
<item key="y" value="456"/>
<item key="z" value="789"/>
</a>
<b>
<item key="x" value="123"/>
<item key="z" value="789"/>
</b>
</root>
This stylesheet shows how to test equality, as defined in the question.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:set="http://exslt.org/sets" xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="set exsl">
<xsl:output method="text" version="1.0" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:variable name="values-are-equal">
<xsl:call-template name="equal">
<xsl:with-param name="A" select="/root/a"/>
<xsl:with-param name="B" select="/root/b"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$values-are-equal = 1">Equal</xsl:when>
<xsl:otherwise>Inequal</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="equal">
<xsl:param name="A" />
<xsl:param name="B" />
<xsl:variable name="common-keys" select="$A/item/#key[ count(set:distinct( . | $B/item/#key )) = count( set:distinct( $B/item/#key ) ) ]"/>
<xsl:variable name="differences">
<xsl:for-each select="$common-keys">
<xsl:if test="$A/item[#key = current()]/#value != $B/item[#key = current()]/#value">
<different/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="count( exsl:node-set($differences)/* ) > 0">0</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This uses some extensions, which are available in xsltproc and other processors.
I have a few strings containing a variant of Hexadecimal strings (source is framemaker if one would care). Strings could therefore look like
this is some sentence with some hex code\x27 s , and we need that
fixed.
and will need to be changed to
this is some sentence with some hex code's , and we need that fixed.
In reality there can be a few of these in a single string, so I'm looking on the best way to walk through the text, capture all hex codes (looking like \x## ) and replace all of these codes with the correct character. I have made a xml list / lookup table containing all the characters as follows :
<xsl:param name="reflist">
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</xsl:param>
For now I used a simple replace argument but there are simply too many characters to make this workable.
What's the best way to do this?
One can completely avoid using any "reference table" -- like this:
<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" exclude-result-prefixes="my xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[matches(., '\\x(\d|[a-f])+')]">
<xsl:analyze-string select="." regex="\\x(\d|[a-f])+" >
<xsl:matching-substring>
<xsl:value-of select=
"codepoints-to-string(my:hex2dec(substring(.,3), 0))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:function name="my:hex2dec" as="xs:integer">
<xsl:param name="pStr" as="xs:string"/>
<xsl:param name="pAccum" as="xs:integer"/>
<xsl:sequence select=
"if(not($pStr))
then $pAccum
else
for $char in substring($pStr, 1, 1),
$code in
if($char ge '0' and $char le '9')
then xs:integer($char)
else
string-to-codepoints($char) - string-to-codepoints('a') +10
return
my:hex2dec(substring($pStr,2), 16*$pAccum + $code)
"/>
</xsl:function>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>
<p>this is some sentence with some hex code\x27 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x28 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x29 s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2a s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2b s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2c s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2d s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2e s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code\x2f s ,
and we need that fixed.</p>
</t>
the wanted, correct result is produced:
<t>
<p>this is some sentence with some hex code' s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code( s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code) s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code* s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code+ s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code, s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code- s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code. s ,
and we need that fixed.</p>
<p>this is some sentence with some hex code/ s ,
and we need that fixed.</p>
</t>
Do note:
This transformation is generic and can correctly process any hexadecimal unicode code.
For example, if the same transformation is applied on this XML document:
<t>
<p>this is some sentence with some hex code\x0428\x0438\x0448 s ,
and we need that fixed.</p>
</t>
the correct result (containing the Bulgarian word for "grill" in Cyrillic) is produced:
<t>
<p>this is some sentence with some hex codeШиш s ,
and we need that fixed.</p>
</t>
Use analyze-string as in
<xsl:template match="text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="$reflist/Code[#Value = .]"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
I would also suggest to use a key e.g.
<xsl:param name="reflist" as="document-node()">
<xsl:document>
<Root>
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</Root>
</xsl:document>
</xsl:param>
<xsl:key name="code-by-value" match="Code" use="#Value"/>
then the lookup can be improved to
<xsl:template match="text/text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="key('code-by-value', ., $reflist)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
I have found some time to morph the suggestions made into working code, with the input being
<root>
<text>this is some sentence with some hex code\x27 s , and we need that \x28and this\x29 fixed.</text>
</root>
and the complete stylesheet being
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="reflist" as="document-node()">
<xsl:document>
<Root>
<Code Value="\x27">'</Code>
<Code Value="\x28">(</Code>
<Code Value="\x29">)</Code>
<Code Value="\x2a">*</Code>
<Code Value="\x2b">+</Code>
<!-- much more like these... -->
</Root>
</xsl:document>
</xsl:param>
<xsl:key name="code-by-value" match="Code" use="#Value"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()">
<xsl:analyze-string select="." regex="\\x[0-9a-f]{{2}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="key('code-by-value', ., $reflist)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
Saxon 9.4 transforms the input as follows:
<root>
<text>this is some sentence with some hex code' s , and we need that (and this) fixed.</text>
</root>
I need help from experts here to optimize the solution of updating string value in an element . I have this xml file as an input...
<?xml version="1.0" encoding="UTF-8"?>
<FullRequest>
<Header>
<Looptimes>3</Looptimes>
<SomeElement>blah!</SomeElement>
<AnotherElement>blah!!</AnotherElement>
</Header>
<RequestDetail>
<!-- Request Element is a string of fixed length (50 characters) -->
<Request>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG AGAIN!</Request>
<Request>THE TIME FOX JUMPED OVER THE LAZY DOG, PROGRESSED!</Request>
<Request>OWING TO THE WIDESPREAD KNOWLEDGE OF THE PHRASE AN</Request>
</RequestDetail>
</FullRequest>
Offset 5 in Request element will be unique and can be cross-referenced. Q, T and G are the IDs in the above request.
<?xml version="1.0" encoding="UTF-8"?>
<FullResponse>
<Header>
<Looptimes>3</Looptimes>
<SomeElement>blah!</SomeElement>
<AnotherElement>blah!!</AnotherElement>
</Header>
<ResponseDetail>
<!-- Response element repeats for 3 times as indicated by the value of Looptimes -->
<!-- Id has a unique value -->
<Response>
<Id>Q</Id>
<Value1>ABC</Value1>
<Value2>XYZ</Value2>
<Value3>FGK</Value3>
</Response>
<Response>
<Id>T</Id>
<Value1>123</Value1>
<Value2>YOK</Value2>
<Value3>DSL</Value3>
</Response>
<Response>
<Id>G</Id>
<Value1>BAT</Value1>
<Value2>TKR</Value2>
<Value3>LAF</Value3>
</Response>
</ResponseDetail>
</FullResponse>
Taking the above xml, offset positions 10, 15 and 20 need to be replaced with values Value1, Value2 and Value3 respectively.
I have this XSL which does the job. Not sure how good this solution will work with few thousand records of 5000 characters each (50 characters in the Request element shown as an example here) with about 20 locations to edit.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regexp="http://exslt.org/regular-expressions" exclude-result-prefixes="regexp">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:variable name="WebResponse" select="document('local:///ic1-data.xml')"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/FullRequest/RequestDetail/Request">
<xsl:variable name="currentLine" select="."/>
<xsl:variable name="id" select="substring($currentLine,5,1)"/>
<xsl:for-each select="$WebResponse/FullResponse/ResponseDetail/Response">
<xsl:variable name="ResId" select="Id"/>
<xsl:if test="$id = $ResId">
<xsl:element name="{name()}">
<!-- Update Value1 -->
<xsl:variable name="from" select="substring($currentLine,10,3)"/>
<xsl:variable name="UpdatedValue1"
select="regexp:replace($currentLine,$from,'',Value1)"/>
<!-- Update Value2 -->
<xsl:variable name="from2" select="substring($UpdatedValue1,15,3)"/>
<xsl:variable name="UpdatedValue2"
select="regexp:replace($UpdatedValue1,$from2,'',Value2)"/>
<!-- Update Value3 -->
<xsl:variable name="from3" select="substring($UpdatedValue2,20,3)"/>
<xsl:value-of select="regexp:replace($UpdatedValue2,$from3,'',Value3)"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The sample output will be as:
<?xml version="1.0" encoding="UTF-8"?>
<FullRequest>
<Header>
<Looptimes>3</Looptimes>
<SomeElement>blah!</SomeElement>
<AnotherElement>blah!!</AnotherElement>
</Header>
<RequestDetail>
<Response>THE QUICKABCOWXYZOXFGKMPS OVER THE LAZY DOG AGAIN!</Response>
<Response>THE TIME 123 JYOKEDDSLER THE LAZY DOG, PROGRESSED!</Response>
<Response>OWING TO BAT WTKRSPLAFD KNOWLEDGE OF THE PHRASE AN</Response>
</RequestDetail>
</FullRequest>
I can only use XSLT 1.0
Can you suggest how to make this better?
Thanks.
Another more flexible approach would be:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:key name="kResponseById" match="Response" use="Id"/>
<xsl:variable name="WebResponse" select="document('ic1-data.xml')"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Request/text()">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="$WebResponse">
<xsl:call-template name="replace">
<xsl:with-param name="pString" select="$vCurrent"/>
<xsl:with-param name="pValues"
select="key('kResponseById',
substring($vCurrent,5,1)
)/*[starts-with(local-name(),'Value')]"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="pString"/>
<xsl:param name="pValues"/>
<xsl:choose>
<xsl:when test="$pValues">
<xsl:variable name="pLimit"
select="substring-after(
local-name($pValues[1]),
'Value'
) * 5 + 4"/>
<xsl:call-template name="replace">
<xsl:with-param name="pString"
select="concat(
substring($pString, 1, $pLimit),
$pValues[1],
substring($pString, $pLimit + 4)
)"/>
<xsl:with-param name="pValues"
select="$pValues[position()!=1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output:
<FullRequest>
<Header>
<Looptimes>3</Looptimes>
<SomeElement>blah!</SomeElement>
<AnotherElement>blah!!</AnotherElement>
</Header>
<RequestDetail>
<!-- Request Element is a string of fixed length (50 characters) -->
<Request>THE QUICKABCOWXYZOXFGKMPS OVER THE LAZY DOG AGAIN!</Request>
<Request>THE TIME 123 JYOKEDDSLER THE LAZY DOG, PROGRESSED!</Request>
<Request>OWING TO BAT WTKRSPLAFD KNOWLEDGE OF THE PHRASE AN</Request>
</RequestDetail>
</FullRequest>
I have an XML like this:
<V>
<W>
<X>1</X>
</W>
<W>
<Y>1</Y>
</W>
<W>
<X>1555</X>
</W>
<W>
<X>1</X>
</W>
</V>
I want to make it something like this:
<entity ID="start">
<f ID="NewField">0001</f>
<f ID="NewField">0001</f>
<f ID="NewField">0002</f>
<f ID="NewField">0003</f>
</entity>
When the field is V/W/X then NewField should be incremented by 1 as many times the tag V/W/X is found.
Similarly for V/W/Y.
The XSL which I am using is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<entity ID="start">
<xsl:for-each select="V/W">
<xsl:if test="X">
<xsl:variable name="my_var">
<xsl:value-of select="concat('000',position())"/>
</xsl:variable>
<f ID="NewField"><xsl:value-of select="$my_var"/></f>
</xsl:if>
<xsl:if test="Y">
<xsl:variable name="my_var">
<xsl:value-of select="concat('000',position())"/>
</xsl:variable>
<f ID="NewField"><xsl:value-of select="$my_var"/></f>
</xsl:if>
</xsl:for-each>
</entity>
</xsl:template>
</xsl:stylesheet>
but it gives me a wrong result, something like this:
<entity ID="start">
<f ID="NewField">0001</f>
<f ID="NewField">0002</f>
<f ID="NewField">0003</f>
<f ID="NewField">0004</f>
</entity>
If you want to number nodes with XSLT then the xsl:number element can help:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<entity ID="start">
<xsl:apply-templates select="descendant::X | descendant::Y"/>
</entity>
</xsl:template>
<xsl:template match="X | Y">
<f ID="NewField"><xsl:number level="any" format="0000"/></f>
</xsl:template>
</xsl:stylesheet>
I think you're looking for something like count(preceding::X) expression. Of course you may want to make it more complex and then take care about number formatting, but that sounds like a starting point you're looking for.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="V">
<entity ID="start">
<xsl:apply-templates select="W/X|W/Y" />
</entity>
</xsl:template>
<xsl:template match="X|Y">
<f ID="NewField">
<xsl:variable name="counter" select="
count(
parent::W/preceding-sibling::W/*[name() = name(current())]
) + 1
" />
<xsl:value-of select="format-number($counter, '0000')" />
</f>
</xsl:template>
</xsl:stylesheet>
This:
parent::W/preceding-sibling::W/*[name() = name(current())]
selects all preceding elements of the same name as the current element. E.g., if the point of execution is on this node:
<X>1555</X>
It goes one level up (parent::W), then selects all preceding <W> siblings, and of those it selects any child (*) that has a name of X - since X is the name of the current() element.
The resulting node-set is counted and incremented by one. format-number() is used to generate a nice clean output:
<entity ID="start">
<f ID="NewField">0001</f>
<f ID="NewField">0001</f>
<f ID="NewField">0002</f>
<f ID="NewField">0003</f>
</entity>