XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="CongDB--Congregation Report.xsl"?>
<CongregationDatabaseReport>
<Settings>
<LanguageCode>en</LanguageCode>
<Direction>ltr</Direction>
<SortOrder>true</SortOrder>
<SortField>Congregation</SortField>
<ReportMode>Simple</ReportMode>
</Settings>
<CongregationList>
<Congregation>Cong1</Congregation>
<Congregation>Cong2</Congregation>
</CongregationList>
</CongregationDatabaseReport>
外部 CongDB XML
<?xml version="1.0" encoding="utf-8"?>
<CongregationDatabase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1" xmlns="http://www.publictalksoftware.co.uk/msa">
<Congregations>
<Congregation LocalCong="false">
<Name>Cong1</Name>
<LastInvited>2011-09-27</LastInvited>
</Congregation>
<Congregation LocalCong="false">
<Name>Cong2</Name>
<LastInvited>2011-09-27</LastInvited>
</Congregation>
<Congregation LocalCong="false">
<Name>Cong3</Name>
<LastInvited>2011-09-27</LastInvited>
</Congregation>
<Congregation LocalCong="false">
<Name>Cong4</Name>
<LastInvited>2011-09-27</LastInvited>
</Congregation>
<Congregation LocalCong="false">
<Name>Cong5</Name>
<LastInvited>2011-09-27</LastInvited>
</Congregation>
</Congregations>
</CongregationDatabase>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msa="http://www.publictalksoftware.co.uk/msa">
<xsl:output method="html" indent="yes" version="4.01"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
<xsl:variable name="CongDB" select="document('MSA_CongregationDatabase.XML')"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:text>Congregation Report</xsl:text>
</title>
<style type="text/css">
* {
font-family: PT Sans;
font-size: 12pt;
}
h1 {
font-size: 14pt;
text-align: center;
border: solid 1px black;
}
h2 {
text-align: center;
background-color: black;
color: white;
padding: 2mm;
}
table {
border: solid 1px black;
border-collapse: collapse;
}
td {
border: solid black 1px;
padding: 1mm;
}
</style>
</head>
<body>
<h1>
<xsl:text>Congregation Report</xsl:text>
</h1>
<xsl:variable name="strSortOrder">
<xsl:choose>
<xsl:when test="//Settings/SortOrder='true'">
<xsl:text>ascending</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>descending</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$strSortOrder"/>
<xsl:value-of select="//Settings/SortField"/>
<xsl:if test="//Settings/ReportMode='Simple'">
<table>
<xsl:if test="//Settings/SortField='Congregation'">
<xsl:for-each select="$CongDB/msa:CongregationDatabase/msa:Congregations/msa:Congregation">
<xsl:sort select="msa:Name" data-type="text" order="{$strSortOrder}"/>
<xsl:apply-templates select="."/>
<xsl:value-of select="count(CongregationDatabaseReport/CongregationList[Congregation=current()])"/>
<!--<xsl:if test="count(CongregationDatabaseReport/CongregationList[Congregation=current()]) = 1">
<xsl:apply-templates select="."/>
</xsl:if>-->
</xsl:for-each>
</xsl:if>
<xsl:if test="//Settings/SortField='Date'">
<xsl:for-each select="$CongDB/msa:CongregationDatabase/msa:Congregations/msa:Congregation">
<xsl:sort select="msa:LastInvited" data-type="text" order="{$strSortOrder}"/>
<xsl:apply-templates select="."/>
<!--<xsl:if test="count(CongregationDatabaseReport/CongregationList[Congregation=current()]) = 1">
<xsl:apply-templates select="."/>
</xsl:if>-->
</xsl:for-each>
</xsl:if>
</table>
</xsl:if>
</body>
</html>
</xsl:template>
<xsl:template match="msa:Congregation">
<tr>
<td>
<xsl:value-of select="msa:Name"/>
</td>
<td>
<xsl:value-of select="msa:LastInvited"/>
</td>
<td>
<xsl:value-of select="msa:TalkCoordinator/msa:Name"/>
</td>
<td>
<xsl:value-of select="msa:Time"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
我评论了我的
if
子句并添加了测试:
<xsl:value-of select="count(CongregationDatabaseReport/CongregationList[Congregation=current()])"/>
但这行不通。我有一份教会名单和更大的完整数据库。我首先对较大的数据库进行排序,然后迭代条目,如果该会众名称在我的其他列表中,则处理它。
但它不喜欢我的
count
电话。似乎 CongregationDatabaseReport
路径在这种情况下无效?
我确实更新了它并确认
current()/msa:Name
返回了会众名称。所以我知道我需要那个。但是要比较的教会列表......嗯。
使用 xmlstarlet(使用问题中的第一个 xml)计算
Congregation
,将返回 2
:
xmlstarlet sel -t -v "count(/CongregationDatabaseReport/CongregationList//Congregation)" input.xml
xmlstartlet 提供了一种输出 XSLT 模板的方式,(在上面添加
-C
时:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="count(/CongregationDatabaseReport/CongregationList//Congregation)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="value-of-template">
<xsl:param name="select"/>
<xsl:value-of select="$select"/>
<xsl:for-each select="exslt:node-set($select)[position()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但是在阅读问题时我迷失了,不知道你想数什么,以及你想用这个数做什么......
您能否提供“所需的输出”,并分享您使用哪些 XML(或多个 XML?)作为输入?