Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

XSLT code

Question posted by: paul_0403@yahoo.com (Guest) on June 27th, 2008 07:07 PM
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

Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Joseph J. Kesselman's Avatar
Joseph J. Kesselman
Guest
n/a Posts
June 27th, 2008
07:07 PM
#2

Re: XSLT code
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.

paul_0403@yahoo.com's Avatar
paul_0403@yahoo.com
Guest
n/a Posts
June 27th, 2008
07:07 PM
#3

Re: XSLT code
On Jun 5, 6:26*pm, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
Quote:
Originally Posted by
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


Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:07 PM
#4

Re: XSLT code
Join Bytes! wrote:
Quote:
Originally Posted by
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/

paul_0403@yahoo.com's Avatar
paul_0403@yahoo.com
Guest
n/a Posts
June 27th, 2008
07:07 PM
#5

Re: XSLT code
On Jun 6, 9:12*am, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Originally Posted by
paul_0...@yahoo.com wrote:
Quote:
Originally Posted by
Can somebody show me how to insert values associated with a variable
into the XML so the output will look like this:

>
Quote:
Originally Posted by
<?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!!!





Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:07 PM
#6

Re: XSLT code
Join Bytes! wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
> <xsl:template match="ResponseFile[not(node())]">
> <xsl:copy>
> <xsl:value-of select="$response_file"/>
> </xsl:copy>
> </xsl:template>

Quote:
Originally Posted by
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>

Quote:
Originally Posted by
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/

paul_0403@yahoo.com's Avatar
paul_0403@yahoo.com
Guest
n/a Posts
June 27th, 2008
07:07 PM
#7

Re: XSLT code
On Jun 6, 10:23*am, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Originally Posted by
paul_0...@yahoo.com wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
* *<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.

>
Quote:
Originally Posted by
*<?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>
>
Quote:
Originally Posted by
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

Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:07 PM
#8

Re: XSLT code
Join Bytes! wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
>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.
Quote:
Originally Posted by
Quote:
Originally Posted by
> <xsl:value-of select="$response_file"/>
> </xsl:copy>
> </xsl:template>



--

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

 
Not the answer you were looking for? Post your question . . .
182,318 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors