这是我的Xml文件...
<w:document>
<w:body>
<w:p>
<w:r>
<w:t>
Paragraph1
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
这是我的第二个XML文件......
<w:document>
<w:body>
<w:p>
<w:r>
<w:pict>
<v:shape>
<v:textbox>
<w:txbxContent>
<w:p>
<w:r>
<w:t>
Paragraph2
</w:t>
</w:r>
</w:p>
</w:txbxContent>
<v:textbox>
</v:shape>
</w:pict>
</w:r>
</w:p>
</w:body>
</w:document>
在这里,我写了一个xslt文件并在我找到// w:body / w:p / w:r / w:t时调用我的模板。
for example,
<xsl:apply-templates select="//w:body/w:p[w:r[w:t]]">
</xsl:apply-templates>
我自己的模板是
<xsl:template match="w:p">
Do something here
</xsl:template>
我的xslt正常使用我的第一个xml文档。但是它不能用于第二个,也有一些类似的场景。那么,如何通过在此处修改此查询来实现这两种情况......
<xsl:apply-templates select="?????"> <!-- how to find the case that also matching my second xml file -->
</xsl:apply-templates>
请指导我摆脱这个问题...
答案 0 :(得分:1)
使用强>:
<xsl:apply-templates select="//w:p[w:r/w:t]">
您可以将模板的匹配属性更改为更具体:
<xsl:template match="w:p[w:r/w:t]">
<!-- Processing here -->
</xsl:template>
完整代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="w:w">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="//w:p[w:r/w:t]"/>
</xsl:template>
<xsl:template match="w:p[w:r/w:t]">
<xsl:value-of select="w:r/w:t"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于第一个提供的XML文档(命名空间以使其格式正确):
<w:document xmlns:w="w:w">
<w:body>
<w:p>
<w:r>
<w:t>
Paragraph1
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
产生了正确的结果:
Paragraph1
当对第二个提供的“XML”应用相同的转换时(它严重格式错误,我花了很多分钟,直到我使其形成良好的形式!!!):
<w:document xmlns:w="w:w">
<w:body>
<w:p>
<w:r>
<w:pict>
<v:shape xmlns:v="v:v">
<v:textbox>
<w:txbxContent>
<w:p>
<w:r>
<w:t>
Paragraph2
</w:t>
</w:r>
</w:p>
</w:txbxContent>
</v:textbox>
</v:shape>
</w:pict>
</w:r>
</w:p>
</w:body>
</w:document>
再次生成想要的结果:
Paragraph2