我需要获取 c:batch 节点的每个子节点。
<?xml version="1.0" encoding="UTF-8"?>
<c:batch xmlns:a="http://www.test.com/data/6/archive"
xmlns:i="http://www.test.com/data/6/archive/import"
xmlns="http://www.test.com/dos/asap"
xmlns:n1="http://www.test.com/dos"
xmlns:c="http://www.test.com/data/6/capture">
<s-20 file="00000001.pdf" checked="true">
<code>X12345</code>
<type>data</type>
</s-20>
<s-20 file="00000002.pdf" checked="false">
<code>X67890</code>
<type>data</type>
</s-20>
<s-20 file="00000003.pdf" checked="true">
<code>X87687</code>
<type>data</type>
</s-20>
</c:batch>
样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.test.com/data/6/archive"
xmlns:i="http://www.test.com/data/6/archive/import"
xmlns="http://www.test.com/dos/asap"
xmlns:n1="http://www.test.com/dos"
xmlns:c="http://www.test.com/data/6/capture">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Documents">
<xsl:value-of select="count(s-20)"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="s-20">
<xsl:value-of select="code"/>
</xsl:template>
</xsl:stylesheet>
最佳答案
您的 <s-20>
在命名空间中:
xmlns="http://www.test.com/dos/asap"
要匹配它们,请在 XSLT 中使用显式命名空间,例如添加行
xmlns:t="http://www.test.com/dos/asap"
给您的
<xsl:stylesheet>
XSLT 中的根元素,并在匹配规则前加上 t:
.也可以使用 xsl:copy-of
而不是 xsl:value-of
在您的 s-20
模板。那么所有s-20
元素将被复制到输出。<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.test.com/data/6/archive"
xmlns:i="http://www.test.com/data/6/archive/import"
xmlns:t="http://www.test.com/dos/asap"
xmlns:n1="http://www.test.com/dos"
xmlns:c="http://www.test.com/data/6/capture">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/c:batch">
<xsl:element name="Documents">
<xsl:value-of select="count(t:s-20)"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="t:s-20">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0"?>
<Documents>3
<s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000001.pdf" checked="true">
<code>X12345</code>
<type>data</type>
</s-20>
<s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000002.pdf" checked="false">
<code>X67890</code>
<type>data</type>
</s-20>
<s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000003.pdf" checked="true">
<code>X87687</code>
<type>data</type>
</s-20>
</Documents>
关于xml - 从 XSL 中的命名空间节点获取子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038834/