2014-06-25 80 views
0

我试图通过检查下列节点是否包含奇数或偶数来将节点值更改为两个不同的事物之一。XSLT XPath,需要根据同级节点的数值更改节点值

这是源XML。

<?xml version="1.0" encoding="UTF-8"?> 
<FILE> 
<CLAIM> 
<PERSON> 
<PROVIDER> 
<HEADER> 
<FLAG>PPON</FLAG> 
<IDNO>11612</IDNO> 
</HEADER> 
</PROVIDER> 
</PERSON> 
</CLAIM> 
</FILE> 

和我想要的XSLT是:

<?xml version='1.0' ?> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output 
     method="xml" 
     version="1.0" 
     encoding="UTF-8" 
     indent="yes" 
     omit-xml-declaration="no"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="FLAG/node()"> 
     <xsl:choose> 
      <xsl:when test="number(../IDNO) mod 2 = 0">EVEN</xsl:when> 
      <xsl:otherwise>ODD</xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

而我想要的输出为是

<?xml version="1.0" encoding="UTF-8"?> 
<FILE> 
<CLAIM> 
<PERSON> 
<PROVIDER> 
<HEADER> 
<FLAG>EVEN</FLAG> 
<IDNO>11612</IDNO> 
</HEADER> 
</PROVIDER> 
</PERSON> 
</CLAIM> 
</FILE> 

我知道我没有得到IDNO在我什么时候测试,因为我的代码一直给我ODD,同时也在调试时我试着把测试的值放到FLAG中,并且我得到了NaN。但是我不能拿出正确的语法来让IDNO进入测试。

是的,我是XSLT新手,所以也许这是一个愚蠢的问题,但我已经尝试了很多不同的东西,并且搜索了这个网站和其他人的答案,但没有运气。

在此先感谢您的帮助。

DWL

+0

是你的问题一个错字,但你的XSLT是在命名空间“http://www.mycompany.com/services/core/file”中查找元素,但是在XML中没有任何命名空间的标志,因此它是“a:FLAG”不会匹配任何内容! –

+0

谢谢。我编辑了这个问题(以及我的“真正的”XML和XSLT)并删除了名称空间引用。但我仍然得到相同的结果。 – DLoysen

回答

2

您当前模板上FLAG的孩子相匹配

<xsl:template match="FLAG/node()"> 

现在,当您使用..这是寻找父母,所以..将在这方面匹配FLAG ,所以../IDNO正在找一个孩子FLAGIDNO,它不存在。

你需要再上一层楼。试试这个

<xsl:template match="FLAG/text()"> 
    <xsl:choose> 
     <xsl:when test="number(../../IDNO) mod 2 = 0">EVEN</xsl:when> 
     <xsl:otherwise>ODD</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
+0

谢谢。那就是诀窍。特别感谢解释,而不仅仅是答案。 – DLoysen

0

可能更容易不检查节点的轴线,而是节点本身:我假设它

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.mycompany.com/services/core/file" exclude-result-prefixes="a"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="*"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="IDNO"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:choose> 
       <xsl:when test="number(.) mod 2 = 0">EVEN</xsl:when> 
       <xsl:otherwise>ODD</xsl:otherwise> 
      </xsl:choose> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
相关问题