Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2008, 04:07 PM
Registered User
 
Join Date: Apr 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT problems

Hi, I'm a beginner programming in XSLT.
I have an XML that I cannot change, I have to submit to it.
The xml has always the same element name changes the attribute tag that allows to individuate what I need.
So imagine that I have an XML with titles(tag="00080104") and text (tag="0040A160"), I can individuate them because they have always the same attribute value one and another. This XML has a lot of titles and text, so the same attibute value is repeat along the file. And always in the same order: first title then text, however, the title exists always, the text not always. I want to pick the couple title/text only when the two of them exist in the XML file.

I decide to put here the xml:

<?xml version="1.0" encoding="UTF-8" ?>
- <dicom>
  <attr tag="00080023" vr="DA" len="8">20001117</attr>
  <attr tag="00080030" vr="TM" len="0" />
  <attr tag="00080033" vr="TM" len="6">120216</attr>
  <attr tag="00080050" vr="SH" len="0" />
  <attr tag="00080060" vr="CS" len="2">SR</attr>
  <attr tag="00080070" vr="LO" len="0" />
  <attr tag="00080090" vr="PN" len="28">Fukuda^Katherine M.^^^M. D.</attr>
  <attr tag="00081030" vr="LO" len="34">OFFIS Structured Reporting Samples</attr>
  <attr tag="0008103E" vr="LO" len="18">Basic Text Report</attr>
  <attr tag="00081111" vr="SQ" len="0" />
  <attr tag="00100010" vr="PN" len="20">Osterman^Phillip^B.</attr>
  <attr tag="00100020" vr="LO" len="0" />
  <attr tag="00100030" vr="DA" len="8">19220909</attr>
  <attr tag="00100040" vr="CS" len="2">M</attr>
  <attr tag="00200010" vr="SH" len="0" />
  <attr tag="00200011" vr="IS" len="2">1</attr>
  <attr tag="00200013" vr="IS" len="2">1</attr>
  <attr tag="0040A040" vr="CS" len="10">CONTAINER</attr>
- <attr tag="0040A043" vr="SQ" len="104">
- <item off="954" len="96">
  <attr tag="00080100" vr="SH" len="4">1111</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="20">Consultation Report</attr>
  </item>
  </attr>
  <attr tag="0040A050" vr="CS" len="8">SEPARATE</attr>
  <attr tag="0040A372" vr="SQ" len="0" />
  <attr tag="0040A491" vr="CS" len="8">PARTIAL</attr>
  <attr tag="0040A493" vr="CS" len="10">UNVERIFIED</attr>
- <attr tag="0040A730" vr="SQ" len="2304">
- <item off="1132" len="180">
  <attr tag="0040A010" vr="CS" len="16">HAS OBS CONTEXT</attr>
  <attr tag="0040A040" vr="CS" len="6">PNAME</attr>
- <attr tag="0040A043" vr="SQ" len="98">
- <item off="1190" len="90">
  <attr tag="00080100" vr="SH" len="4">1112</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="14">Observer Name</attr>
  </item>
  </attr>
  <attr tag="0040A123" vr="PN" len="24">Packer^David M.^^^M. D.</attr>
  </item>
- <item off="1320" len="186">
  <attr tag="0040A010" vr="CS" len="16">HAS OBS CONTEXT</attr>
  <attr tag="0040A040" vr="CS" len="4">TEXT</attr>
- <attr tag="0040A043" vr="SQ" len="110">
- <item off="1376" len="102">
  <attr tag="00080100" vr="SH" len="4">1113</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="26">Observer Organization Name</attr>
  </item>
  </attr>
  <attr tag="0040A160" vr="UT" len="16">Redlands Clinic</attr>
  </item>
- <item off="1514" len="1186">
  <attr tag="0040A010" vr="CS" len="8">CONTAINS</attr>
  <attr tag="0040A040" vr="CS" len="4">TEXT</attr>
- <attr tag="0040A043" vr="SQ" len="96">
- <item off="1562" len="88">
  <attr tag="00080100" vr="SH" len="4">2222</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="12">Description</attr>
  <attr tag="0008010C" vr="UI" len="26">1.2.276.0.7230010.3.0.0.1</attr>
  </item>
  </attr>
  <attr tag="0040A160" vr="UT" len="1038">This 78-year-old gentleman referred by Dr. Fukuda</attr>
  </item>
- <item off="2708" len="298">
  <attr tag="0040A010" vr="CS" len="8">CONTAINS</attr>
  <attr tag="0040A040" vr="CS" len="4">TEXT</attr>
- <attr tag="0040A043" vr="SQ" len="94">
- <item off="2756" len="86">
  <attr tag="00080100" vr="SH" len="4">2223</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="10">Diagnosis</attr>
  </item>
  </attr>
  <attr tag="0040A160" vr="UT" len="152">involving the skin of the left external ear.</attr>
  </item>
- <item off="3014" len="414">
  <attr tag="0040A010" vr="CS" len="8">CONTAINS</attr>
  <attr tag="0040A040" vr="CS" len="4">TEXT</attr>
- <attr tag="0040A043" vr="SQ" len="94">
- <item off="3062" len="86">
  <attr tag="00080100" vr="SH" len="4">2224</attr>
  <attr tag="00080102" vr="SH" len="14">99_OFFIS_DCMTK</attr>
  <attr tag="00080104" vr="LO" len="10">Treatment</attr>
  <attr tag="0008010C" vr="UI" len="26">1.2.276.0.7230010.3.0.0.1</attr>
  </item>
  </attr>
  <attr tag="0040A160" vr="UT" len="268">The plan of treatment is as follows.</attr>
  </item>
  </attr>
  </dicom>
I put here the XML, maybe it would be easy to understand.

My idea was:
count all the title tags then positionate in the first and see if after there is a text tag if there is pick them. If not, continue, positionate in the second and do the same thing till the end.

Can anybody help me to do it?

this is what I try to do... I know its NOT good, and that doens't...

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:text>STRUCTURED REPORT</xsl:text>
<xsl:variable name="count_text" select="count(@tag='00080104')/>

<xsl:for-each (when is in the first @tag='00080104')
    <xsl:if test="@tag='0040A160'") >
         <p><xsl:value-of select="."/></p>
        </xsl:if>
<p><xsl:value-of select="."/></p>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Thank very much!

Emília:)

 
Old April 3rd, 2008, 04:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't really understand your structure - what is the significance of the "item" elements?

It's probably a good idea to think of this (and perhaps to implement it) as two stages of transformation. The first is quite easy, and produces an output like:

<title>first title</title>
<title>second title</title>
<text>some text</text>
<title>third title</title>

The next stage is to group a title with its following text element. This is easy in XSLT 2.0, which has a special instruction

<xsl:for-each-group select="*" group-starting-with="title">

It's much harder in XSLT 1.0, and isn't a task that I would recommend beginners to tackle. The task is called "positional grouping" and usual solution is "sibling recursion" which involves recursive processing of the sequence of siblings; you will find information by googling either of these terms.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 3rd, 2008, 09:17 AM
Registered User
 
Join Date: Apr 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael!

First of all thank you for replaying!

I did this:

<xsl:for-each select="//item">
    <xsl:if test="attr/item/attr[@tag='00080104']">
        <xsl:if test="attr[@tag='0040A160']">
            <b><xsl:value-of select="attr/item/attr[@tag='00080104']"/></b><br/>
            <xsl:value-of select="attr[@tag='0040A160']"/>
            <p/>
        </xsl:if>
    </xsl:if>
</xsl:for-each>

But it only works when there is no tag='0040A160' I need another condition when it exists but its emply in the XML like:

<attr tag='0040A160'/>

can u help me?
I thought in put something like another if after the first one that does something like <xsl:if test!=text()> but it doens't work...or maybe a double condition in the if...

Have any idea?

Thank you!
Emilia







Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT - 2 problems ... prichardson XSLT 1 July 17th, 2007 12:38 PM
XSLT rendering problems in IE7 for Vista tripecac XSLT 17 July 4th, 2007 10:07 AM
namespace problems in xslt stekker XSLT 2 June 16th, 2006 09:59 AM
translate() problems in XSLT 2.0 dsanders XSLT 3 February 23rd, 2006 11:14 AM
xslt variable problems lincsimp XSLT 2 August 17th, 2005 02:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.