我正在尝试对大约100个XML文件运行批量转换,以将它们转换为CSV。某些文件不包含我正在使用的某个变量的值,因此我收到了提到的错误。有没有办法在XSLT中绕过它或继续运行,即使值的长度为零?如果长度为零,我可以设置虚拟值吗?我看过SO和其他论坛,但找不到任何有用的东西。此外,我是XSLT的新手,所以我可能不够了解我自己。下面是我的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2017-10-16">
<Record userId="1898739" username="50019019" termId="5923" dmd:surveyId="17683692">
<dmd:IndexEntry indexKey="COLLEGE" entryKey="College of Engineering" text="College of Engineering"/>
<dmd:IndexEntry indexKey="DEPARTMENT" entryKey="Civil, Architectural, & Environmental Engineering" text="Civil, Architectural, & Environmental Engineering"/>
<INTELLCONT id="151368386560" dmd:originalSource="MANAGE_DATA" dmd:lastModified="2017-10-03T10:47:54" dmd:startDate="2016-01-01" dmd:endDate="2016-12-31">
<REFEREED>Yes</REFEREED>
<CONTYPE>Journal Article</CONTYPE>
<CONTYPEOTHER/>
<STATUS>Published</STATUS>
<TITLE>Sample data</TITLE>
<TITLE_SECONDARY/>
<INTELLCONT_AUTH id="151368386563">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="151368386561">
<FACULTY_NAME>1898739</FACULTY_NAME>
<FNAME>sample</FNAME>
<MNAME>sample</MNAME>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<PUBLISHER>sample</PUBLISHER>
<PUBCTYST/>
<PUBCNTRY/>
<VOLUME>13</VOLUME>
<ISSUE>11</ISSUE>
<PAGENUM>117</PAGENUM>
<WEB_ADDRESS/>
<DOI>sample</DOI>
<ISBNISSN/>
<PMCID/>
<AUDIENCE/>
<PUBLICAVAIL/>
<ABSTRACT/>
<FULL_TEXT/>
<DTM_EXPSUB/>
<DTD_EXPSUB/>
<DTY_EXPSUB/>
<EXPSUB_START></EXPSUB_START>
<EXPSUB_END></EXPSUB_END>
<DTM_SUB/>
<DTD_SUB/>
<DTY_SUB/>
<SUB_START></SUB_START>
<SUB_END></SUB_END>
<DTM_ACC/>
<DTD_ACC/>
<DTY_ACC/>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB/>
<DTD_PUB/>
<DTY_PUB>2016</DTY_PUB>
<PUB_START>2016-01-01</PUB_START>
<PUB_END>2016-12-31</PUB_END>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
</INTELLCONT>
<INTELLCONT dmd:originalSource="IMPORT" dmd:lastModified="2017-10-03T11:17:33" dmd:startDate="2016-01-01" dmd:endDate="2016-12-31">
<REFEREED>Yes</REFEREED>
<CONTYPE>Journal Article</CONTYPE>
<CONTYPEOTHER/>
<STATUS>Published</STATUS>
<TITLE>Sample</TITLE>
<TITLE_SECONDARY/>
<INTELLCONT_AUTH id="141176756225">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756226">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756227">
<FACULTY_NAME>1898739</FACULTY_NAME>
<FNAME>sample</FNAME>
<MNAME>sample</MNAME>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756228">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME>sample</MNAME>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756229">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756230">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>sample</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756231">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756232">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756233">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME>sample</MNAME>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756234">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<INTELLCONT_AUTH id="141176756235">
<FACULTY_NAME/>
<FNAME>sample</FNAME>
<MNAME/>
<LNAME>sample</LNAME>
<INSTITUTION/>
<ROLE>Author</ROLE>
<STUDENT_LEVEL/>
</INTELLCONT_AUTH>
<PUBLISHER>sample</PUBLISHER>
<PUBCTYST/>
<PUBCNTRY/>
<VOLUME>23</VOLUME>
<ISSUE/>
<PAGENUM>2003-2013</PAGENUM>
<WEB_ADDRESS/>
<DOI>sample</DOI>
<ISBNISSN/>
<PMCID/>
<AUDIENCE/>
<PUBLICAVAIL/>
<ABSTRACT/>
<FULL_TEXT/>
<DTM_EXPSUB/>
<DTD_EXPSUB/>
<DTY_EXPSUB/>
<EXPSUB_START></EXPSUB_START>
<EXPSUB_END></EXPSUB_END>
<DTM_SUB/>
<DTD_SUB/>
<DTY_SUB/>
<SUB_START></SUB_START>
<SUB_END></SUB_END>
<DTM_ACC/>
<DTD_ACC/>
<DTY_ACC/>
<ACC_START></ACC_START>
<ACC_END></ACC_END>
<DTM_PUB/>
<DTD_PUB/>
<DTY_PUB>2016</DTY_PUB>
<PUB_START>2016-01-01</PUB_START>
<PUB_END>2016-12-31</PUB_END>
<USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
</INTELLCONT>
</Record> <!-- Added by edit -->
</Data> <!-- Added by edit -->
在此示例中,所有DTY_PUB标记都有一个值,但并非所有XML文件都有值。下面是我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv"
xmlns="http://www.w3.org/1999/xhtml" xmlns:dm="http://www.digitalmeasures.com/schema/data"
xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="array fn map math xhtml xs">
<xsl:output method="text" encoding="utf-8"/>
<xsl:variable name="delimiter" select="','"/>
<xsl:key name="Contype-Year" match="dm:Data/dm:Record/dm:INTELLCONT[dm:USER_REFERENCE_CREATOR='Yes']" use="concat(dm:CONTYPE, '|', dm:DTY_PUB)"/>
<xsl:variable name="input-doc" select="."/>
<xsl:template match="/">
<xsl:text>,</xsl:text>
<xsl:variable name="min-year" as="xs:integer" select="min(dm:Data/dm:Record/dm:INTELLCONT/dm:DTY_PUB/xs:integer(.))"/>
<xsl:variable name="max-year" as="xs:integer" select="max(dm:Data/dm:Record/dm:INTELLCONT/dm:DTY_PUB/xs:integer(.))"/>
<xsl:variable name="categories" select="distinct-values(dm:Data/dm:Record/dm:INTELLCONT/dm:CONTYPE)"/>
<xsl:variable name="years" as="xs:integer*" select="$min-year to $max-year"/>
<xsl:value-of select="$years" separator=","/>
<xsl:text> </xsl:text>
<xsl:for-each select="$categories">
<xsl:variable name="cat" select="."/>
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
<xsl:value-of select="for $y in $years return count(key('Contype-Year', concat($cat, '|', $y), $input-doc))" separator=","/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
它突破的是:
<xsl:variable name="min-year" as="xs:integer" select="min(dm:Data/dm:Record/dm:INTELLCONT/dm:DTY_PUB/xs:integer(.))"/>
非常感谢任何帮助。
答案 0 :(得分:2)
您可以使用其他支票<xsl:variable name="min-year" as="xs:integer" select="min(dm:Data/dm:Record/dm:INTELLCONT/dm:DTY_PUB[. castable as xs:integer]/xs:integer(.))"/>
。