我正在尝试编写一个XSLT转换,它将使用前一个兄弟的值替换一个节点中的值。然后返回整个文档转换:)
查找
的出现次数<package:packageDownloadLocator>http://myunwantedurl</package:packageDownloadLocator>
并替换为正在进行的兄弟的文本值
<package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>
感谢您的任何帮助或建议
注意:要找到的节点并不总是存在于每条记录中。
由此:
<?xml version="1.0"?>
<SRW:searchRetrieveResponse xmlns:SRW="http://www.loc.gov/zing/srw/" xmlns:DIAG="http://www.loc.gov/zing/srw/diagnostics" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:package="info:srw/extension/13/package-v1.0">
<SRW:records>
<SRW:record>
<SRW:recordData>
<dc:dc>
<dc:title xml:lang="en">Opportunities for involvement for service users and carers at the Open University in Scotland</dc:title>
<dc:description xml:lang="en">Booklet explaining the different ways service users or carers can assist</dc:description>
<dc:publisher>Open University</dc:publisher>
<dc:format>application/pdf</dc:format>
<dc:type>Narrative Text</dc:type>
<dc:rights xml:lang="en">Copyright Open University, 2009</dc:rights>
<dc:subject>health and health care, health care, nursing</dc:subject>
</dc:dc>
</SRW:recordData>
<SRW:extraRecordData>
<package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>
<package:packageDownloadLocator>http://myunwantedurl</package:packageDownloadLocator>
<record:record xmlns:record="http://srw.o-r-g.org/schemas/rec/1.0/">
<record:lastModified>2009-09-29</record:lastModified>
<record:created>2009-09-29</record:created>
</record:record>
</SRW:extraRecordData>
</SRW:record>
</SRW:records>
</SRW:searchRetrieveResponse>
对此:
<?xml version="1.0"?>
<SRW:searchRetrieveResponse xmlns:SRW="http://www.loc.gov/zing/srw/" xmlns:DIAG="http://www.loc.gov/zing/srw/diagnostics" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:package="info:srw/extension/13/package-v1.0">
<SRW:records>
<SRW:record>
<SRW:recordData>
<dc:dc>
<dc:title xml:lang="en">Opportunities for involvement for service users and carers at the Open University in Scotland</dc:title>
<dc:description xml:lang="en">Booklet explaining the different ways service users or carers can assist</dc:description>
<dc:publisher>Open University</dc:publisher>
<dc:format>application/pdf</dc:format>
<dc:type>Narrative Text</dc:type>
<dc:rights xml:lang="en">Copyright Open University, 2009</dc:rights>
<dc:subject>health and health care, health care, nursing</dc:subject>
</dc:dc>
</SRW:recordData>
<SRW:extraRecordData>
<package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>
<package:packageDownloadLocator>http://myurl.com</package:packageDownloadLocator>
<record:record xmlns:record="http://srw.o-r-g.org/schemas/rec/1.0/">
<record:lastModified>2009-09-29</record:lastModified>
<record:created>2009-09-29</record:created>
</record:record>
</SRW:extraRecordData>
</SRW:record>
</SRW:records>
</SRW:searchRetrieveResponse>
答案 0 :(得分:5)
此转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:package="info:srw/extension/13/package-v1.0" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="package:packageDownloadLocator/text()">
<xsl:value-of select="../preceding-sibling::*[1]/text()"/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时,生成所需结果。
请注意 :
使用身份规则按原样复制每个节点。
使用仅与所需文本节点匹配的特定模板覆盖标识规则。
答案 1 :(得分:0)
阅读“身份转型”(Google it,维基百科上有一个很好的页面)。您所需要的只是一个身份转换以及一个与您想要更改的标记相匹配的模板。您还必须决定是否需要任何之前的兄弟,或者特定的前一个标记(如果没有先前的兄弟,该怎么办)。