首页 \ 问答 \ 没有使用默认命名空间的XSL输出XML没有前缀?(XSL output XML with no prefix without using the default namespace?)

没有使用默认命名空间的XSL输出XML没有前缀?(XSL output XML with no prefix without using the default namespace?)

我有一个XSL,我需要沿着这条线生成输出:

<moo xmlns="http://api.example.com">
    <foo>1358944586848</foo>
    <bar>
        <a>1</a>
        <b>2</b>
        <c>3</c>
    </bar>
</moo>

我可以这样做:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://api.example.com">

    <xsl:template match="/">
        <xsl:element name="moo">
            <!-- and so on -->

但是,我有点讨厌在我的xsl文件中使用xsl前缀,因为我觉得它让它变得很混乱。 无论如何,选择XPath很容易,因为你可以根据需要将xpath-default-namespace为你正在转换的任何东西。 但是就我所见,没有可用的element-default-namespace ,那么如何才能以良好的方式生成所需的输出呢?

我知道我可以这样做:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform">

    <template match="/">
        <element name="moo" namespace="http://api.example.com">
            <!-- and so on -->

但是我必须在我创建的每个元素上显式设置这个命名空间,否则它们最终将使用XSL命名空间。 那么是否有一种干净的方法来创建具有特定命名空间(没有前缀)的元素而不触及xsl文件的默认命名空间?


更新:

想象也许namespace-alias可以做一些事情,但无法弄清楚如何使用它。 试过这个,但输出似乎没有任何差别:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:out="http://api.example.com">

<namespace-alias stylesheet-prefix="out" result-prefix=""/>

    <template match="/">
        <element name="out:moo">
            <!-- and so on -->

namespace-alias可能不是我认为的那样:p

我使用的最终解决方案,基于JLRishe的答案

除去-prefixes.xsl

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <template match="/">
        <variable name="result">
            <next-match />
        </variable>
        <apply-templates select="$result" mode="remove-prefixes" />
    </template>

    <template match="*" priority="1" mode="remove-prefixes">
        <element name="{local-name()}" namespace="{namespace-uri()}">
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </element>
    </template>
    <template match="@*|node()" mode="remove-prefixes">
        <copy>
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </copy>
    </template>

</stylesheet>

subject.xsl

<!-- snip -->
<import href="remove-prefixes.xsl" />
<!-- snip -->

I have an XSL where I need to generate output along the lines of this:

<moo xmlns="http://api.example.com">
    <foo>1358944586848</foo>
    <bar>
        <a>1</a>
        <b>2</b>
        <c>3</c>
    </bar>
</moo>

I could do it like this:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://api.example.com">

    <xsl:template match="/">
        <xsl:element name="moo">
            <!-- and so on -->

However, I kind of hate using the xsl prefix in my xsl files cause I feel it clutters it up a lot. Selecting with XPath is easy anyways since you can set xpath-default-namespace to whatever you're transforming from if needed. But there is no element-default-namespace available as far as I can see, so how can I generate the wanted output in a good way?

I know I can do this:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform">

    <template match="/">
        <element name="moo" namespace="http://api.example.com">
            <!-- and so on -->

But then I have to set this namespace explicitly on every single element I create, or they will end up with the XSL namespace instead. So is there a clean way to create elements with a certain namespace (without a prefix) and not touching the default namespace of the xsl file?


Update:

Figured maybe namespace-alias could do something, but can't figure out how to use it. Tried this, but doesn't seem to make any difference in the output at all:

<stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:out="http://api.example.com">

<namespace-alias stylesheet-prefix="out" result-prefix=""/>

    <template match="/">
        <element name="out:moo">
            <!-- and so on -->

The namespace-alias thing probably isn't doing what I think it is :p

The final solution I used, based on JLRishe's answer

remove-prefixes.xsl

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <template match="/">
        <variable name="result">
            <next-match />
        </variable>
        <apply-templates select="$result" mode="remove-prefixes" />
    </template>

    <template match="*" priority="1" mode="remove-prefixes">
        <element name="{local-name()}" namespace="{namespace-uri()}">
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </element>
    </template>
    <template match="@*|node()" mode="remove-prefixes">
        <copy>
            <apply-templates select="@* | node()" mode="remove-prefixes" />
        </copy>
    </template>

</stylesheet>

subject.xsl

<!-- snip -->
<import href="remove-prefixes.xsl" />
<!-- snip -->

原文:https://stackoverflow.com/questions/14480173
更新时间:2023-06-25 20:06

最满意答案

找到任何未来googlers的解决方案!

继承人我是怎么做到的:

在我的片段中,我在onCreate()中实例化了mySurfaceView

然后我在onCreateView()期间将虚拟布局(LinearLayout)附加到实际视图

然后在onResume()中我用removeAllViews()清除了虚拟布局,然后调用addView(mySurfaceView)

这似乎允许ViewPager快速构建其布局,然后在用户实际需要查看任何内容之前弹出SurfaceView!


Found the solution for any future googlers!

Heres how I did it:

In my fragment I instantiated mySurfaceView in onCreate()

Then I attached a dummy layout (LinearLayout) to the actual view during onCreateView()

Then in onResume() I cleared the dummy layout with removeAllViews() and then called addView(mySurfaceView)

This seems to allow the ViewPager to build its layout quickly and then the SurfaceView is just popped in before the user actually needs to see anything!

相关问答

更多
  • 我只是放下变化。 用LinearLayout替换xml中的表面视图。 获取线性布局的实例 LinearLayout surface =(LinearLayout)findViewById(R.id.surf ...
  • 问题是向SurfaceView添加x和y字段,在onTouchEvent处更改它们并使用x和y-offset绘制画布中的所有元素。 奇怪的是我没有收到任何答案...... The issue was to add x and y fields to SurfaceView, change them at onTouchEvent and draw all the elements in canvas with x- and y-offset. It's strange that I didn't recie ...
  • 好吧,我找到了一个功能解决方案: 我认为问题来自MCustomDrawableView初始化。 我从onCreate方法创建了该类的新实例,而不是从xml文件创建。 然后,我将其设置为contentView : protected void onCreate(Bundle p_savedInstanceState) { super.onCreate(p_savedInstanceState); setContentView(R.layout.main_controlle ...
  • 根据您的错误判断,您可能忘记在清单中为新活动添加条目。 在您的AndroidManifest.xml您可能会看到MainActivity的条目如下所示: ...
  • 我通过完全忽略xml逻辑并通过addview动态添加元素解决了上述问题。 所以我能够实现我想要的。 glView = new GLSurfaceView(this); glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); glView.getHolder().setFormat(PixelFormat.TRANSLUCENT); glView.setRenderer(new GLClearRenderer()); cameraPreview = new Surfac ...
  • 好吧,我终于解决了这个问题。 我没有重新设置MediaPlayer的SurfaceHolder (调用mMediaPlayer.setDisplay(SurfaceHolder holder) )。 这是我的代码: @Override protected void onResume() { mPath = "rtsp://10.10.20.42/Viral.mp4"; // Create a new media player and set the listeners mMediaP ...
  • 您应该有一个自定义视图并在xml中使用它。 首先创建此视图 class MyListView extends SurfaceView implements Runnable { SurfaceHolder myHolder; Thread myThread = null; boolean isRunning = true; // My Variables long dT; int myRed; public MyListView(Context co ...
  • 在我的主题中,我从surfaceview的doTouch调用以下方法: public void doLose() { synchronized (mSurfaceHolder) { //quit to mainmenu ((Activity) mContext).finish(); } } 这似乎关闭了surfaceview和托管它的活动,把我丢回主菜单活 ...
  • 找到任何未来googlers的解决方案! 继承人我是怎么做到的: 在我的片段中,我在onCreate()中实例化了mySurfaceView 然后我在onCreateView()期间将虚拟布局(LinearLayout)附加到实际视图 然后在onResume()中我用removeAllViews()清除了虚拟布局,然后调用addView(mySurfaceView) 这似乎允许ViewPager快速构建其布局,然后在用户实际需要查看任何内容之前弹出SurfaceView! Found the solutio ...
  • 设置SurfaceView的背景并没有特定的方法。 如果你在onDraw设置一个背景然后在onDraw之外的某个地方绘制画布,那么它将调用onDraw并在刚绘制的任何内容上绘制背景。 假设您没有尝试在UI线程上执行此操作,每次重绘背景都不会占用大量内存。 另外,我不太清楚你的意思是“在左上角2-3dp被删除”和“左上方空白”。 你可以发布一个这样的图像,以及你尝试过的代码吗? There's not really a specific way to set a SurfaceView's backgroun ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。