Question

I wrote RDF file and was asked to present it using XSL.

The RDF code (changed)

    <?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:clinic="http://www.computing-services.co.il"
xml:base="http://www.computing-services.co.il">
<rdfs:Class rdf:ID="Patient">
<rdfs:comment>
the class of patient
</rdfs:comment>
</rdfs:Class>
<rdf:Property rdf:ID="ID">
<rdfs:comment>
Patient's ID
</rdfs:comment>
<rdfs:domain rdf:resource="#Patient"/>
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
</rdf:Property>
<rdf:Property rdf:ID="Name">
<rdfs:comment>
Patient's Name
</rdfs:comment>
<rdfs:domain rdf:resource="#Patient"/>
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
</rdf:Property>
  <rdf:Property rdf:ID="Age">
    <rdfs:comment>
      Patient's Age
    </rdfs:comment>
    <rdfs:domain rdf:resource="#Patient"/>
    <rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
  </rdf:Property>
  <rdf:Property rdf:ID="Sex">
    <rdfs:comment>
      Patient's Sex
    </rdfs:comment>
    <rdfs:domain rdf:resource="#Patient"/>
    <rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"/>
  </rdf:Property>
<rdfs:Class rdf:ID="Disease">
<rdfs:comment>
the class of diseases
</rdfs:comment>
</rdfs:Class>
  <rdfs:Class rdf:ID="CancDiseases">
    <rdfs:comment>
      the class of cancerous diseases
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Disease"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="CntDiseases">
    <rdfs:comment>
      the class of contagious diseases
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Disease"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="Traumas">
    <rdfs:comment>
      the class of traumas
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Disease"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="IntDiseases">
    <rdfs:comment>
      the class of Internal diseases
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Disease"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="EmIntDiseases">
    <rdfs:comment>
      the class of Emerging diseases
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#IntDiseases"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="ChIntDiseases">
    <rdfs:comment>
      the class of Chronic diseases
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#IntDiseases"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="Treatment">
    <rdfs:comment>
      the class of treatment
    </rdfs:comment>
  </rdfs:Class>
  <rdfs:Class rdf:ID="Medication">
    <rdfs:comment>
      the class of medications
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Treatment"/>
  </rdfs:Class>
  <rdfs:Class rdf:ID="Procedure">
    <rdfs:comment>
      the class of procedures
    </rdfs:comment>
    <rdfs:subClassOf rdf:resource="#Treatment"/>
  </rdfs:Class>


<rdf:Property rdf:ID="isSick">
<rdfs:domain rdf:resource="#Patient"/>
<rdfs:range rdf:resource="#Disease"/>
</rdf:Property>
  <rdf:Property rdf:ID="gets">
    <rdfs:domain rdf:resource="#Patient"/>
    <rdfs:range rdf:resource="#Treatment"/>
  </rdf:Property>
  <rdf:Property rdf:ID="helpsFrom">
    <rdfs:domain rdf:resource="#Treatment"/>
    <rdfs:range rdf:resource="#Disease"/>
  </rdf:Property>







          <clinic:ChIntDiseases rdf:ID="chrone"/>
          <clinic:ChIntDiseases rdf:ID="diabetes"/>
          <clinic:Traumas rdf:ID="BrokenLeg"/>
          <clinic:EmIntDiseases rdf:ID="Appendicite"/>
          <clinic:CancDiseases rdf:ID="BreastCancer"/>
          <clinic:CancDiseases rdf:ID="LungCancer"/>






  <clinic:Medication rdf:ID="Insuline">
    <clinic:helpsFrom rdf:resource="#diabetes"/>
  </clinic:Medication>
  <clinic:Medication rdf:ID="Pentassa">
    <clinic:helpsFrom rdf:resource="#chrone"/>
  </clinic:Medication>
  <clinic:Medication rdf:ID="Raffasal">
    <clinic:helpsFrom rdf:resource="#chrone"/>
  </clinic:Medication>
  <clinic:Medication rdf:ID="Advil">
    <clinic:helpsFrom rdf:resource="#BrokenLeg"/>
    <clinic:helpsFrom rdf:resource="#Appendicite"/>
  </clinic:Medication>
  <clinic:Procedure rdf:ID="Sigmoscopia">
    <clinic:helpsFrom rdf:resource="#chrone"/>
  </clinic:Procedure>
  <clinic:Procedure rdf:ID="Chemo">
    <clinic:helpsFrom rdf:resource="#BreastCancer"/>
    <clinic:helpsFrom rdf:resource="#LungCancer"/>
  </clinic:Procedure>



          <clinic:Patient rdf:ID="p23456">
            <clinic:Name>Linda Jones</clinic:Name>
            <clinic:Sex>Female</clinic:Sex>
            <clinic:Age>56</clinic:Age>
            <clinic:isSic>
              <rdf:Bag>
                <rdf:li rdf:resource="#chrone"/>
                <rdf:li rdf:resource="#diabetes"/>
              </rdf:Bag>
            </clinic:isSic>
            <clinic:gets>
              <rdf:Bag>
                <rdf:li rdf:resource="#Pentassa"/>
                <rdf:li rdf:resource="#Insuline"/>
              </rdf:Bag>
            </clinic:gets>
          </clinic:Patient>
  <clinic:Patient rdf:ID="p12345">
    <clinic:Name>Moshe Koen</clinic:Name>
    <clinic:Sex>Male</clinic:Sex>
    <clinic:Age>70</clinic:Age>
    <clinic:isSic>
      <rdf:Bag>
        <rdf:li rdf:resource="#diabetes"/>
      </rdf:Bag>
    </clinic:isSic>
    <clinic:gets>
      <rdf:Bag>
        <rdf:li rdf:resource="#Insuline"/>
      </rdf:Bag>
    </clinic:gets>
  </clinic:Patient>
  <clinic:Patient rdf:ID="p34567">
    <clinic:Name>Ofer Meleh</clinic:Name>
    <clinic:Sex>Male</clinic:Sex>
    <clinic:Age>25</clinic:Age>
    <clinic:isSic>
      <rdf:Bag>
        <rdf:li rdf:resource="#BrokenLeg"/>
      </rdf:Bag>
    </clinic:isSic>
    <clinic:gets>
      <rdf:Bag>
        <rdf:li rdf:resource="#Advil"/>
      </rdf:Bag>
    </clinic:gets>
  </clinic:Patient>
  <clinic:Patient rdf:ID="p45678">
    <clinic:Name>Itzhak Rabinovich</clinic:Name>
    <clinic:Sex>Male</clinic:Sex>
    <clinic:Age>38</clinic:Age>
    <clinic:isSic>
      <rdf:Bag>
        <rdf:li rdf:resource="#LungCancer"/>
      </rdf:Bag>
    </clinic:isSic>
    <clinic:gets>
      <rdf:Bag>
        <rdf:li rdf:resource="#Chemo"/>
      </rdf:Bag>
    </clinic:gets>
  </clinic:Patient>
  <clinic:Patient rdf:ID="p56789">
    <clinic:Name>Golda Zilber</clinic:Name>
    <clinic:Sex>Female</clinic:Sex>
    <clinic:Age>45</clinic:Age>
    <clinic:isSic>
      <rdf:Bag>
        <rdf:li rdf:resource="#BreastCancer"/>
        <rdf:li rdf:resource="#Appendicite"/>
      </rdf:Bag>
    </clinic:isSic>
    <clinic:gets>
      <rdf:Bag>
        <rdf:li rdf:resource="#Chemo"/>
        <rdf:li rdf:resource="#Advil"/>
      </rdf:Bag>
    </clinic:gets>
  </clinic:Patient>




</rdf:RDF>

`

There are patients, their diseases and treatments (I changed previous RDFS part) I need to make ordered list of patients (with their personal data) and for each patient - table of his diseases and treatments.

It should be something like that:

That's is my new try to write xsl

        <?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:clinic="http://www.infomed.co.il">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/rdf:RDF/clinic:Patient">
    <html>
      <body>
        <ol>
          <xsl:apply-templates select="/rdf:RDF/clinic:Patient" />
        </ol>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="clinic:Patient">
    <li>
      Name <xsl:value-of select="clinic:Name"/>
      Age <xsl:value-of select="clinic:Age"/>
      Sex <xsl:value-of select="clinic:Sex"/>
      <table>
        <tr>
          <th>Disease</th>
          <xsl:apply-templates select="clinic:isSic" />
          <th>Treatment</th>
          <xsl:apply-templates select="clinic:gets" />
        </tr>


      </table>
    </li>
  </xsl:template>

  <xsl:template match="clinic:isSic">
    <xsl:variable name="resource" select="@rdf:resource"/>
      <td>
        <xsl:value-of select="translate(@rdf:resource,'#','')"/>
      </td>  
  </xsl:template>
  <xsl:template match="clinic:gets">
    <xsl:variable name="resource" select="@rdf:resource"/>
    <td>
      <xsl:value-of select="translate(@rdf:resource,'#','')"/>
    </td>
  </xsl:template>


</xsl:stylesheet>

It still doesn't work, but at least I see now the patients data. It looks like this:

the class of patient Patient's ID Patient's Name Patient's Age Patient's Sex the class of diseases the class of cancerous diseases the class of contagious diseases the class of traumas the class of Internal diseases the class of Emerging diseases the class of Chronic diseases the class of treatment the class of medications the class of procedures Linda Jones Female 56 Moshe Koen Male 70 Ofer Meleh Male 25 Itzhak Rabinovich Male 38 Golda Zilber Female 45

I"ll appreciate any help to make it like this:

Was it helpful?

Solution

Updated answer to reflect changes in the question:

Your updated stylesheet still had a couple of issues:

  • The namespace for clinic was declared with the incorrect namespace-uri in your XSLT. It needs to be mapped to http://www.computing-services.co.il in order for your XPath statements to match.
  • I changed the template that was matching on /rdf:RDF/clinic:Patient to match on the root node / in order to ensure control of how the stylesheet processes. Otherwise, the default templates would have matched on the elements and text nodes (emitting text and whitespace) until it hit on the clinic:Patent element and executed the template.
  • If you want separate rows for Disease and Treatment, then you need to close the tr and start a new one before <tr>Treatment</tr>.
  • In the templates for clinic:isSic and clinic:gets, you don't need the resource variable, and the path to the @rdf:Resourceisrdf:Bag/rdf:li/@rdf:resource`.

I made those adjustments to your stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:clinic="http://www.computing-services.co.il">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html>
      <body>
        <ol>
          <xsl:apply-templates select="/rdf:RDF/clinic:Patient" />
        </ol>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="clinic:Patient">
    <li>
      Name <xsl:value-of select="clinic:Name"/>
      Age <xsl:value-of select="clinic:Age"/>
      Sex <xsl:value-of select="clinic:Sex"/>
      <table border="1">
        <tr>
          <th>Disease</th>
          <xsl:apply-templates select="clinic:isSic" />
        </tr>
        <tr>
          <th>Treatment</th>
          <xsl:apply-templates select="clinic:gets" />
        </tr>
      </table>
    </li>
  </xsl:template>

  <xsl:template match="clinic:isSic">
      <xsl:apply-templates select="rdf:Bag/rdf:li/@rdf:resource"/>
  </xsl:template>

  <xsl:template match="clinic:gets"> 
      <xsl:apply-templates select="rdf:Bag/rdf:li/@rdf:resource"/>
  </xsl:template>

  <xsl:template match="rdf:Bag/rdf:li/@rdf:resource">
    <td>
      <xsl:value-of select="translate(.,'#','')"/>
    </td>
  </xsl:template>
</xsl:stylesheet>

As an asside, the current logic seems to assume that there is a 1-to-1 mapping of the Diseases and Treatments. If there were multiple Treatments how would you differentiate or know which ones mapped to which disease?

OTHER TIPS

If you are trying to display RDF as HTML, I think you should be using RDFa. You can generate HTML with the RDFa markup quite easily and it has the added benefit of anyone viewing the page with an RDFa aware plugin or agent can pick up and use the RDF. If you just using XSL to turn it into HTML, you 'lose' the RDF.

RDF must not be parsed using XML techniques!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top