【发布时间】:2014-02-18 17:59:01
【问题描述】:
如何从节点复制一些属性。例如。我只想从节点“Extn”复制“Srno”、“RollNo”、“right”。
<Main>
<SubMainLevel1 material="12" feature="range">
<SubMainLevel2 arg1="abc" arg2="123">
<Item name="hello" desc="one" />
<Detail long="high" short="wide" />
<Extn Srno="12" RollNo="12" weight="12" folds="2" right="Y" left="N" top="T" bottom="V" />
</SubMainLevel2>
<SubMainLevel2 arg1="cyz" arg2="123">
<Item name="hello2" desc="two" />
<Detail long="short" short="wide" />
<Extn Srno="" RollNo="" weight="" folds="1" right="Y" left="N" top="T" bottom="V" />
</SubMainLevel2>
</SubMainLevel1>
</Main>
我使用的Xsl如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*" />
<xsl:template match="/">
<Job>
<xsl:copy-of select="Main/SubMainLevel1/@*[name()='material']" />
<Lines>
<xsl:for-each select="/Main/SubMainLevel1/SubMainLevel2">
<xsl:if test="@arg2 = '123'">
<Line>
<xsl:copy-of select="@*" />
<xsl:copy-of select="node()[name() = 'Item']" />
<xsl:copy-of select="node()[name() = 'Detail']" />
<xsl:copy-of select="node()[name() = 'Extn']" />
</Line>
</xsl:if>
</xsl:for-each>
</Lines>
</Job>
</xsl:template>
</xsl:stylesheet>
我怎样才能用上面提到的值来限制节点“Extn”。
预期输出是
<?xml version="1.0" encoding="UTF-8"?>
<Job material="12">
<Lines>
<Line arg1="abc" arg2="123">
<Item name="hello" desc="one" />
<Detail long="high" short="wide" />
<Extn Srno="12" RollNo="12" right="Y"/>
</Line>
<Line arg1="cyz" arg2="123">
<Item name="hello2" desc="two" />
<Detail long="short" short="wide" />
<Extn Srno="" RollNo="" right="Y"/>
</Line>
</Lines>
</Job>
注意:这与“How not to copy some attributes?”不同
【问题讨论】:
-
我们能看看输出应该是什么样子吗?
-
你想把它们复制到哪里到是个问题。
-
为什么是
@*[name()='material']?这和@material有区别吗?