1
我包括用於產生錯誤的文件。xslt驗證/轉換工作在oXygen,但不是與lxml python腳本?
這些工作在oXygen;一個python lxml腳本不起作用。
更具體地說,轉換工作,但驗證錯誤無法正常工作。 (例如,如果id
改變爲ids
。)
toy.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="file:/randng.rnc" type="application/relax-ng-compact-syntax"?>
<?xml-model href="file:/toytest.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<report>
<title>Variant calling and annotation for Dr.Smith</title>
<date>July 1,1985</date>
<analysis id="NGS">
<method id="QC"></method>
<method id="GATK"></method>
</analysis>
<analysis id="genome_alignment">
<method id="bowtie2"></method>
</analysis>
</report>
test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<html>
<head>
<h2>Report for Dr Zoidberg</h2>
<div><xsl:value-of select="current-date()"/></div>
<div><xsl:apply-templates select="/report/date"></xsl:apply-templates></div>
</head>
<body>
<h2>Table of Contents</h2>
<ul><xsl:apply-templates select="//analysis" mode="toc"/></ul>
<h2>Analysis</h2>
<ul><xsl:apply-templates select="//analysis" mode="analysis"/></ul>
<!-- <xsl:apply-templates select="/report/analysis/method"/>-->
<h2>Citations</h2>
<ul><xsl:apply-templates select="//method" mode="cite"></xsl:apply-templates></ul>
</body>
</html>
</xsl:template>
<!-- list analysis -->
<xsl:template match="analysis" mode="analysis">
<li id="{@id}">
<xsl:apply-templates select="@id"/>
<ul><xsl:apply-templates select="/analysis/method"></xsl:apply-templates>
<xsl:apply-templates select="method"/>
</ul>
</li>
</xsl:template>
<!-- list methods -->
<xsl:template match="method">
<li>
<xsl:apply-templates select="@id"/>
<!--citation-->
<xsl:for-each select="document('contentconfig.xml')/cfg/analysis/method[@id=current()/@id]/citation">
<sup>
<a href="#cite{count(preceding::citation) + 1}">
<xsl:value-of select="count(preceding::citation) + 1"/>
</a>
</sup>
</xsl:for-each>
</li>
</xsl:template>
<!--TOC based on analysis -->
<xsl:template match="analysis" mode="toc">
<li>
<a href="#{@id}">
<xsl:apply-templates select="@id"/>
</a>
</li>
</xsl:template>
<!--Citations -->
<xsl:template match="method" mode="cite">
<xsl:for-each select="document('contentconfig.xml')/cfg/analysis/method[@id=current()/@id]/citation">
<li>
<cite id="cite{count(preceding::citation) + 1}">
[<xsl:value-of select="count(preceding::citation) + 1"/>]
<xsl:apply-templates/>
</cite>
</li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
randng.rng
<!--<?xml version="1.0" encoding="UTF-8"?>-->
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<ref name="report"/>
</start>
<define name="report">
<element name="report">
<optional>
<ref name="title"/>
</optional>
<optional>
<ref name="date"/>
</optional>
<oneOrMore>
<ref name="analysis"/>
</oneOrMore>
</element>
</define>
<define name="title">
<element name="title">
<text/>
</element>
</define>
<define name="date">
<element name="date">
<text/>
</element>
</define>
<define name="analysis">
<element name="analysis">
<attribute name="id"/>
<oneOrMore>
<ref name="method"/>
</oneOrMore>
</element>
</define>
<define name="method">
<element name="method">
<attribute name="id"/>
<text/>
</element>
</define>
</grammar>
toytest.sch
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="sum_equals_100_percent">
<title>Sum equals 100%.</title>
<rule context="Total">
<assert test="sum(//Percent)=100">Sum is not 100%.</assert>
</rule>
</pattern>
</schema>
但是,當我嘗試使用python lxml時,我得到一個RELAXNG_ERR_ELEMNAME
。 這裏是Python script:
import shlex, subprocess
from lxml import isoschematron
from lxml import etree
# _VALIDATE_
# parse sch,xml,relaxNG
parser_xml = etree.XMLParser()
sch_doc = etree.parse('toy.sch')
schematron = isoschematron.Schematron(sch_doc, store_report = True)
#xml_doc = etree.parse('toy.xsl', parser_xml)
xml_doc = etree.parse('toy.xsl')
##compact to relaxNG
#pytrang randng.rnc randng.rng
randNG_doc = etree.parse('randng.rng')
relaxng = etree.RelaxNG(randNG_doc)
# validate against schematron
validationResult = schematron.validate(xml_doc)
print '--------------------'
print validationResult
# validate against relaxNG
try:
validateRelaxng = relaxng.assert_(xml_doc)
print 'relaxNG worked'
except AssertionError as e:
error = relaxng.error_log.last_error
print relaxng.error_log
print error.type_name
if error.type_name == 'RELAXNG_ERR_ATTRVALID':
print 'invalid attribute'
# RELAXNG_ERR_ELEMNAME
# _TRANSFORM_
#just run it from shell
cmd = "java -cp /usr/share /java/saxonb.jar net.sf.saxon.Transform -xsl:test2.xsl -s:toy.xml -o:toy6.html"
args = shlex.split(cmd)
#subprocess.Popen(args)
改造工作,確認不會產生正確的錯誤。
我最終只是使用命令行調用進行轉換。 sch和rng文件是否符合xslt_1.0?爲什麼我不能使簡單的腳本工作,還有什麼其他問題?
最好