472,784 Members | 971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

XSLT code

I have the following XML file

<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
<RequestInfo>
<ResponseFile Mode="Overwrite"></ResponseFile>
<StatusFile></StatusFile>
</RequestInfo>
</CentralServerRequest>

I tried the following xslt code but can't seem to get it working.

response_file=e:/tmp/response.xml
status_file=d:/tmp/status.xml

<xsl:template match="StatusFile[. = '' ">
<xsl:copy>
<xsl:value-of select="$status_file"/>
</xsl:copy>
</xsl:template
Can somebody show me how to insert values associated with a variable
into the XML so the output will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
<RequestInfo>
<ResponseFile Mode="Overwrite">e:/tmp/response.xml</
ResponseFile>
<StatusFile>d:/tmp/status.xml</StatusFile>
</RequestInfo>
</CentralServerRequest>
Thanks in advance to all who answer

Jun 27 '08 #1
7 1610
In general, to "insert something into" or "remove something from" an XML
document using XSLT:

Start with the identity transformation. (See the XSLT Recommendation or
any good XSLT tutorial). That handles copying everything that should
pass through unchanged.

Then add a template which recognizes the place where you want to do
something different, and give it a body that Does The Right Thing at
that point.
Jun 27 '08 #2
On Jun 5, 6:26*pm, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
In general, to "insert something into" or "remove something from" an XML
document using XSLT:

Start with the identity transformation. (See the XSLT Recommendation or
any good XSLT tutorial). That handles copying everything that should
pass through unchanged.

Then add a template which recognizes the place where you want to do
something different, and give it a body that Does The Right Thing at
that point.
Thanks for your response. I was sort of hoping you can tell me what I
was doing wrong in my example or maybe give me a similar working
example

Jun 27 '08 #3
pa*******@yahoo.com wrote:
Can somebody show me how to insert values associated with a variable
into the XML so the output will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
<RequestInfo>
<ResponseFile Mode="Overwrite">e:/tmp/response.xml</
ResponseFile>
<StatusFile>d:/tmp/status.xml</StatusFile>
</RequestInfo>
</CentralServerRequest>
Here is a sample stylesheet that uses the identity transformation
template and two templates to modify the ResponseFile and StatusFile
elements:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:param name="response_file" select="'e:/tmp/response.xml'"/>
<xsl:param name="status_file" select="'d:/tmp/status.xml'"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ResponseFile[not(node())]">
<xsl:copy>
<xsl:value-of select="$response_file"/>
</xsl:copy>
</xsl:template>

<xsl:template match="StatusFile[not(node())]">
<xsl:copy>
<xsl:value-of select="$status_file"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #4
On Jun 6, 9:12*am, Martin Honnen <mahotr...@yahoo.dewrote:
paul_0...@yahoo.com wrote:
Can somebody show me how to insert values associated with a variable
into the XML so the output will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
* * <RequestInfo>
* * * * <ResponseFile Mode="Overwrite">e:/tmp/response.xml</
ResponseFile>
* * * * <StatusFile>d:/tmp/status.xml</StatusFile>
* * </RequestInfo>
</CentralServerRequest>

Here is a sample stylesheet that uses the identity transformation
template and two templates to modify the ResponseFile and StatusFile
elements:

<xsl:stylesheet
* *xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
* *version="1.0">

* *<xsl:param name="response_file" select="'e:/tmp/response.xml'"/>
* *<xsl:param name="status_file" select="'d:/tmp/status.xml'"/>

* *<xsl:template match="@* | node()">
* * *<xsl:copy>
* * * *<xsl:apply-templates select="@* | node()"/>
* * *</xsl:copy>
* *</xsl:template>

* *<xsl:template match="ResponseFile[not(node())]">
* * *<xsl:copy>
* * * *<xsl:value-of select="$response_file"/>
* * *</xsl:copy>
* *</xsl:template>

* *<xsl:template match="StatusFile[not(node())]">
* * *<xsl:copy>
* * * *<xsl:value-of select="$status_file"/>
* * *</xsl:copy>
* *</xsl:template>

</xsl:stylesheet>

--

* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/
Martin,

The example for Statusfile works but the example for the ResponseFile
does not work.

Note the elments are a bit different since there is an attrtibute
"Mode" in the ResponseFile element
as there is no attribute in the StatusFile element.

<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
<RequestInfo>
<ResponseFile Mode="Overwrite"></ResponseFile>
<StatusFile></StatusFile>
</RequestInfo>
</CentralServerRequest>
Based on that, I am assumming the XSLT code has to look a bit
different when doing the match? Is there some sytnax to specify the
atttibute name in the matc?. It would be okay with matching
"ResponseFile Mode" and skipping the value "OverWrite" since my Mode
can have several different
values that I may want to ignore.

THanks in advance for all your help!!!


Jun 27 '08 #5
pa*******@yahoo.com wrote:
> <xsl:template match="ResponseFile[not(node())]">
<xsl:copy>
<xsl:value-of select="$response_file"/>
</xsl:copy>
</xsl:template>
Note the elments are a bit different since there is an attrtibute
"Mode" in the ResponseFile element
as there is no attribute in the StatusFile element.

<?xml version="1.0" encoding="UTF-8"?>
<CentralServerRequest>
<RequestInfo>
<ResponseFile Mode="Overwrite"></ResponseFile>
If you want to copy the attribute use

<xsl:template match="ResponseFile[not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$response_file"/>
</xsl:copy>
</xsl:template>

Based on that, I am assumming the XSLT code has to look a bit
different when doing the match? Is there some sytnax to specify the
atttibute name in the matc?. It would be okay with matching
"ResponseFile Mode" and skipping the value "OverWrite" since my Mode
can have several different
values that I may want to ignore.
If you want to add the text if the Mode attribute exists then use

<xsl:template match="ResponseFile[@Mode and not(node())]">
<xsl:copy>
<xsl:value-of select="$response_file"/>
</xsl:copy>
</xsl:template>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #6
On Jun 6, 10:23*am, Martin Honnen <mahotr...@yahoo.dewrote:
paul_0...@yahoo.com wrote:
* *<xsl:template match="ResponseFile[not(node())]">
* * *<xsl:copy>
* * * *<xsl:value-of select="$response_file"/>
* * *</xsl:copy>
* *</xsl:template>
Note the elments are a bit different since there is an attrtibute
"Mode" in the ResponseFile element
as there is no attribute in the StatusFile element.
*<?xml version="1.0" encoding="UTF-8"?>
*<CentralServerRequest>
* * *<RequestInfo>
* * * * *<ResponseFile Mode="Overwrite"></ResponseFile>

If you want to copy the attribute use

* *<xsl:template match="ResponseFile[not(node())]">
* * *<xsl:copy>
* * * *<xsl:apply-templates select="@*"/>
* * * *<xsl:value-of select="$response_file"/>
* * *</xsl:copy>
* *</xsl:template>
Based on that, I am assumming the XSLT code has to look a bit
different when doing the match? Is there some sytnax to specify the
atttibute name in the matc?. It would be okay with matching
"ResponseFile Mode" and skipping the value *"OverWrite" since my Mode
can have several different
values that I may want to ignore.

If you want to add the text if the Mode attribute exists then use

* *<xsl:template match="ResponseFile[@Mode and not(node())]">
* * *<xsl:copy>
* * * *<xsl:value-of select="$response_file"/>
* * *</xsl:copy>
* *</xsl:template>

--

* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/
Thanks I appreciate your expertise and help
Jun 27 '08 #7
pa*******@yahoo.com wrote:
>If you want to add the text if the Mode attribute exists then use

<xsl:template match="ResponseFile[@Mode and not(node())]">
<xsl:copy>
Depending on your needs you might also want to copy the attributes with
<xsl:apply-templates select="@*"/>
here.
> <xsl:value-of select="$response_file"/>
</xsl:copy>
</xsl:template>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
1
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
4
by: Ringo Langly | last post by:
Hi all, I'm a seasoned web programmer, but I've never touched XSLT. It's always been one of those acronyms I've never needed to educate myself on. Now... we're working with a web content...
8
by: Maciej Wegorkiewicz | last post by:
Hi, I have small experience in XSLT processing and I have a problem which I cannot solve. Can you look at it? I have an input file containing info about bank accounts like this: (...) <acc...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
0
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag,...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
1
by: Dan | last post by:
I have a C# program which executes some XSLT transformations. The XSLT code requires an input intermediate file, generated by other transformations, as its duty is copying some data from an input...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.