【问题标题】:Present RDF with XSL. Updated使用 XSL 呈现 RDF。更新
【发布时间】:2012-08-28 21:45:42
【问题描述】:

我编写了 RDF 文件并被要求使用 XSL 呈现它。

RDF 代码(已更改)

    <?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>

`

有病人,他们的疾病和治疗方法(我改变了之前的 RDFS 部分) 我需要制作有序的患者列表(以及他们的个人数据)并为每个患者 - 他的疾病和治疗表。

应该是这样的:

这是我写 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>

它仍然不起作用,但至少我现在看到了患者数据。它看起来像这样:

患者类别 患者 ID 患者姓名 患者年龄 患者性别 疾病类别 癌症疾病类别 传染病类别 外伤类别 内科疾病类别 新发疾病类别 慢性疾病类别治疗类别 药物类别 程序类别 Linda Jones 女 56 Moshe Koen 男 70 Ofer Meleh 男 25 Itzhak Rabinovich 男 38 Golda Zilber 女 45

我会很感激任何帮助使它变成这样:

【问题讨论】:

  • 请编辑问题并提供准确的、想要的转换结果——读者不必猜测,这会阻止他们中的许多人回答。
  • 感谢您的建议!我编辑了帖子
  • @Michael Shaf,如果这是其中之一,或者您可以完全手动控制 RDF/XML 文件结构,那么可以这样做。但请记住,RDF/XML 可以通过多种方式序列化相同的 RDF 数据。换句话说:具有非常不同的 XML 结构的两个文档可能呈现相同的信息。如果您将文件放在三重存储中,然后将其导出回来,它看起来会有所不同。这就是为什么几乎不可能编写出适用于所有合法 RDF 输入的 XSLT。

标签: xslt rdf rdfs


【解决方案1】:

更新答案以反映问题的变化:

您更新后的样式表仍有一些问题:

  • clinic 的命名空间在您的 XSLT 中使用不正确的命名空间 uri 声明。它需要映射到 http://www.computing-services.co.il 才能使您的 XPath 语句匹配。
  • 我将在/rdf:RDF/clinic:Patient 上匹配的模板更改为在根节点/ 上匹配,以确保控制样式表的处理方式。否则,默认模板将在元素和文本节点(发出文本和空格)上匹配,直到它命中 clinic:Patent 元素并执行模板。
  • 如果您想要单独的“疾病”和“治疗”行,则需要关闭 tr 并在 &lt;tr&gt;Treatment&lt;/tr&gt; 之前创建一个新行。
  • clinic:isSicclinic:gets 的模板中,您不需要resource 变量和@rdf:Resourceisrdf:Bag/rdf:li 的路径/@rdf:resource`。

我对您的样式表进行了这些调整:

<?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>

顺便说一句,当前的逻辑似乎假设疾病和治疗之间存在一对一的映射。如果有多种治疗方法,您将如何区分或知道哪些治疗方法对应于哪种疾病?

【讨论】:

  • 疯子,谢谢!我知道我在命名空间中做错了什么。但它还不起作用,尽管我按照你的建议进行了更改。我只得到没有任何东西的白页:-(
  • @Michael Shaf - 我已经根据对 XML 和 XSLT 的更改更新了我的答案。
【解决方案2】:

如果您尝试将 RDF 显示为 HTML,我认为您应该使用 RDFa。您可以很容易地生成带有 RDFa 标记的 HTML,它还有一个额外的好处,即任何使用 RDFa 感知插件或代理查看页面的人都可以获取并使用 RDF。如果您只是使用 XSL 将其转换为 HTML,那么您将“丢失”RDF。

【讨论】:

    【解决方案3】:

    RDF 不得使用 XML 技术解析!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多