我有一个xml文件,root.xml:
<?xml version="1.0" encoding="UTF-8"?>
<statuses>
<status topic-file="Procedure1">Complete</status>
<status topic-file="Procedure3">Draft</status>
</statuses>
请注意,我要跟踪4个程序。我想一次性改变2个程序的状态。我想要更改的内容在此XML文件statusByTitle.xml
中注明<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- set up the key -->
<xsl:key name="statusByTitle" match="status" use="/topic-file" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- use the key to set the attribute of the correct procedure -->
<xsl:template
match="item[key('statusByTitle', status, document('statusByTitle.xml'))]/@status">
<xsl:attribute name="status">
<xsl:value-of select="key('statusByTitle', ../status, document('statusByTitle.xml'))" />
</xsl:attribute>
</xsl:template>
我想改变一次性指示的过程1和3的状态,所以我创建了这个XSLT转换:
<script>
$(document).ready(function() {
var information_object = <?php echo json_encode($results); ?>;
$(".content").click(function() {
var info = information_object[$(this).data('id')];
if (info) {
var warning;
if(info.status == 3){
warning = "High Voltage Detected";
}else if(info.status == 4){
warning = "Caution is Recommended"
}else{
warning = "Ok";
}
Alert.render("temp: " + info.temp + "™" + "<br>" + "Address: " + info.address + "<br>" + "Date: " + info.date + "<br>" + "Status: " + warning);
} else {
alert("Invalid ID: " + id);
}
});
});
</script>
<script
function CustomAlert(){
this.render = function(dialog){
var winW = window.innerWidth;
var winH = window.innerHeight;
var dialogoverlay = document.getElementById('dialogoverlay');
var dialogbox = document.getElementById('dialogbox');
dialogoverlay.style.display = "block";
dialogoverlay.style.height = winH+"px";
dialogbox.style.left = (winW/2) - (550 * .5)+"px";
dialogbox.style.top = "100px";
document.getElementById('dialogboxhead').innerHTML = "temp Warning Information";
document.getElementById('dialogboxbody').innerHTML = dialog;
document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';
}
this.ok = function(){
document.getElementById('dialogbox').style.display = "none";
document.getElementById('dialogoverlay').style.display = "none";
}
}
var Alert = new CustomAlert();
</script>
我使用Saxon-HE 9.5.1.7在Oxygen中运行此转换,输出文件与输入文件相同。我已经盯着这一段时间,但却找不到错误。我以某种方式误解了键吗?
答案 0 :(得分:1)
<xsl:key name="statusByTitle" match="status" use="/topic-file" />
应<xsl:key name="statusByTitle" match="status" use="@topic-file" />
。然后你有错误的元素名称item
而不是procedure
:
<xsl:template
match="procedure[key('statusByTitle', @topic-file, document('statusByTitle.xml'))]/@status">
<xsl:attribute name="status" select="key('statusByTitle', ../@topic-file, document('statusByTitle.xml'))" />
</xsl:template>
所有更正我最终都是
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="status-url" select="'test2015080705.xml'"/>
<xsl:param name="status-doc" select="doc($status-url)"/>
<!-- set up the key -->
<xsl:key name="statusByTitle" match="status" use="@topic-file" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- use the key to set the attribute of the correct procedure -->
<xsl:template
match="procedure[key('statusByTitle', @topic-file, $status-doc)]/@status">
<xsl:attribute name="status" select="key('statusByTitle', ../@topic-file, $status-doc)" />
</xsl:template>
</xsl:stylesheet>