Xml 根据键选择(带命名空间的元素)

Xml 根据键选择(带命名空间的元素),xml,xslt,Xml,Xslt,我对XSLT比较陌生,我需要根据当前帖子使用pauthorid的帖子来选择author元素(key:aid) 作者的XML <a:authors> <a:author aid="a1"> <a:name>Brian Muscat</a:name> <a:username>bmuscat</a:username> <a:password>abc123</

我对XSLT比较陌生,我需要根据当前帖子使用pauthorid的帖子来选择author元素(key:aid)

作者的XML

<a:authors>
    <a:author aid="a1">
        <a:name>Brian Muscat</a:name>
        <a:username>bmuscat</a:username>
        <a:password>abc123</a:password>
        <a:email>bm661@live.mdx.ac.uk</a:email>
    </a:author>
</a:authors>

布莱恩·马斯卡特
B美国
abc123
bm661@live.mdx.ac.uk
post的xml

<posts>
    <post pid="p1">
        <ptitle>CLOUD COMPUTING</ptitle>
        <pfeatureimage>aaig.jpg</pfeatureimage>
        <ptext xml:lang="en">text</ptext>
        <pdate>25/06/2013</pdate>
        <pimg>cloud.jpg</pimg>
        <pimg>cloud.jpg</pimg>
        <pauthorid>a1</pauthorid>

    </post>
    </posts>

云计算
aaig.jpg
文本
25/06/2013
cloud.jpg
cloud.jpg
a1
到目前为止,我已经编写了xslt

    <xsl:for-each select="posts/post">
    <div class="post">
    <div style="margin-top:20px; margin-right:20px; float:right;"> <span class="text" style="float:right;">Posted On:<xsl:value-of select="pdate"/></span></div>

        <h3><xsl:value-of select="ptitle"/></h3>
        <div style="padding:10px; height:60px; margin-top:-20px;">
        <span class="text"><xsl:value-of select="ptext"/>

        <xsl:variable name="aid" select="pauthorid" />
      <!--The Problem is here-->
 <xsl:for-each select="//a:authors/a:author[@aid=$aid]">
                    <xsl:value-of select="a:name" />
        </xsl:for-each>     
          </span>
         </div>
    </div>
    <br />
</xsl:for-each>

张贴于:


我猜您指的是一个xml文件。 添加样式表和模板标记后,代码是正确的。您只需要声明“a”前缀对应的名称空间(这里是一个示例声明)

我已经将您的for each语句更改为“//posts/post”,以便进行测试,这完全取决于您的初始xml和xsd

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.tibco.com/schemas/test/XSL_test/localschema" version="1.0">
  <xsl:template match="/">
    <xsl:for-each select="//posts/post">
    <div class="post">
    <div style="margin-top:20px; margin-right:20px; float:right;"> 
        <span class="text" style="float:right;">Posted On:<xsl:value-of select="pdate"/>        
    </span></div>

    <h3><xsl:value-of select="ptitle"/></h3>
    <div style="padding:10px; height:60px; margin-top:-20px;">
    <span class="text"><xsl:value-of select="ptext"/>

    <xsl:variable name="aid" select="pauthorid" />
     <xsl:for-each select="//a:authors/a:author[@aid=$aid]">
        <xsl:value-of select="a:name" />
     </xsl:for-each>     
    </span>
   </div>
</div>
<br />
</xsl:for-each>
</xsl:stylesheet>

张贴于:


您的问题是什么?您遇到了什么错误?我需要先获取作者姓名,即a:name,然后选择文章,然后从作者id PAUTORID中选择作者